|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Templates, dates and times" |
| 4 | +description: "Using templates for seconds and years in Home Assistant." |
| 5 | +date: 2017-10-14 08:00:00 +0200 |
| 6 | +date_formatted: "October 14, 2017" |
| 7 | +author: Fabian Affolter |
| 8 | +comments: true |
| 9 | +categories: Community |
| 10 | +og_image: /images/blog/2017-10-template/social.png |
| 11 | +--- |
| 12 | + |
| 13 | +This [Pull Request](https://github.com/home-assistant/home-assistant/pull/9868) shows in a clear way what happens if the documentation is not as good as it should be. In short, it's about [Templating](/docs/configuration/templating/) and how people start to think about creative ways to solve it if it's not documented. Let's assume that we want the current year. There are a couple of options available to do that: |
| 14 | + |
| 15 | +- Query [JSON Test](http://date.jsontest.com/) with a [`rest` sensor](/components/sensor.rest/) and a `value_template:`. |
| 16 | +- Use a [`time_date` sensor ](/components/sensor.time_date/) and a template {% raw %}`{{ strptime(states('sensor.date'), '%Y-%m-%d').year }}`{% endraw %}. |
| 17 | +- Write a script in language X and use it with the [`command` sensor](/components/sensor.command_line/) or use `date +"%Y"` as a `command:`. |
| 18 | + |
| 19 | +<!--more--> |
| 20 | + |
| 21 | +We want it simpler, right? [Templating](/docs/configuration/templating/) offers `now()` and `utcnow()`. We will stick with `now()` in this blog post but it applies to `utcnow()` as well. Our documentation said: |
| 22 | + |
| 23 | +<blockquote> |
| 24 | + `now()` will be rendered as current time in your time zone. |
| 25 | +</blockquote> |
| 26 | + |
| 27 | +Hmmm, ...OK, that's a start. How to get the year? {% raw %}`{{ now() }}`{% endraw %} gives you `2017-10-14 20:27:23.700401+02:00` which is far more than we are looking for. As an user you don't want to dive into the code but there would you find the solution. You will get a [Python `datetime` object](https://docs.python.org/3.6/library/datetime.html#datetime.datetime) from {% raw %}`{{ now() }}`{% endraw %}. This means that you can access more than you think in a template: |
| 28 | + |
| 29 | +- For the time: `now().microsecond`, `now().second`, `now().minute` and `now().hour` |
| 30 | +- For the date: `now().day`, `now().month` and `now().year` |
| 31 | +- Misc: `now().weekday()` and `now().isoweekday()` |
| 32 | + |
| 33 | +For the year it would be: {% raw %}`{{ now().year }}`{% endraw %}. I guess that there are rare use cases for `now().resolution`, `now().min` and `now().max` too. |
| 34 | + |
| 35 | +[Hacktoberfest](/blog/2017/09/29/hacktoberfest/) is still running. Working on the documentation is pretty easy. If you know a nice [trick](/cookbook/), want to help improving the page of a platform or just fix typo then please do. Our [Website/Documentation section](/developers/documentation/) contains some requirements which are defined in the [Documentation Standards](/developers/documentation/standards/) and the "[Create a page](/developers/documentation/create_page/)" documentation for other useful details. |
| 36 | + |
| 37 | +Thanks to [Egor Tsinko](https://github.com/etsinko) for bringing this issue to our attention. |
| 38 | + |
0 commit comments