As you’ll notice, I have a preconfigured Dark Sky sensor, a Google Travel Time sensor and a datetime sensor.
Beware the Dark Sky sensor only works if you have enabled
If you use sensor.time to trigger the template sensor, it will trigger every minute. This is needlessly frequent for a sensor that only needs to recalculate once a day.
Use sensor.date which will trigger once at the beginning of each day. To simplify the templates, use strftime. It can provides the names of the day and month (exclusively in English in Home Assistant).
- platform: template
sensors:
dayoftheweek:
entity_id: sensor.date
value_template: "{{ now().strftime('%A') }}"
month:
entity_id: sensor.date
value_template: "{{ now().strftime('%B') }}"
dateofthemonth:
entity_id: sensor.date
value_template: >
{% set suffix = ['st', 'nd', 'rd'] %}
{% set day = now().day %}
{% set index = 3 if day // 10 == 1 or day % 10 == 0 else (day % 10) - 1 %}
{{ day~'th' if index > 2 else day~suffix[index] }}
EDIT
Added qualification that strftime reports names exclusively in English in Home Assistant.
Updated dateofthemonth template to properly support exceptions: 1st, 2nd, 3rd, 11th, 12th, 13th, 21st, 22nd, 23rd, 31st.
The only reason the list was used is for people who don’t use English. Yeah the 1st 2 templates ‘%A’ and ‘%B’ are great for english speakers, but any other language will not work.
I am sorry to raise an old thread, but I get this error when using your exact code. (I had amended it to my own, but then when I got the error I input yours but am still getting the error.
Can you help?
Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/config/scripts.yaml", line 363, column 17
Hi UIGUY, great you got it working. Can you share the code?
It works perfectly and has done for many versions with me.
Can you send an example of your faulty code?
now().weekday() returns a number representing the day of the week (0 for Monday and 6 for Sunday). Subtract that from 7 and calculate the remainder of the division by (modulo) 7 and you have the number of days until Monday.
Note that you have to refer to an entity that is updated from time to time (such as sensor.time or sensor.date - the latter changes once a day which should be enough) in order to update the sensor (the sensor only updates on state changes, and now() is a function and does not have a state, so the sensor won’t update unless you refer to an entity as shown in the example).
Wow - I have no idea how you came up with that algorithm for the st, nd, rd, and th - that’s absolutely brilliant! I had to plug it all into the developer template to figure it out. It’s now part of my sensors. Thanks, @123!
I’m trying to get the weekday and the next five weekdays on a weather forecast card. If today is Thursday, I want to have Friday to Tuesday displayed as the next five days.
I created 6 sensors, adding the desired number of days on each card:
This works, however only for days within the current week: Monday and Tuesday are not displayed. It makes sense, as weekday 8 and 9 do not exist. Any ideas how to resolve this?
Sorry to resurrect (again) an old thread, but is there a way to change the starting day of week to Sunday = 0, instead of Monday? Without going into the exhaustive historical reasons why this is accurate, suffice it to say that I’m struggling to code based on Monday being the first day, which should be Sunday. Maybe it’s just too late at night (CT, USA) and my brain can’t figure out the conversion… Thanks
Btw, unless you are a frequent time traveller or want to gain programming experience, displaying the current date in Home Assistant adds no value.
Value is in the eye of the beholder.
I missed a few hours of work the other day because I thought it was Tuesday, when it was Wednesday.
Its easy to loose track of the day of week when you work 8 days on, 6 days off. 14 day work schedule. Its all a blur.
Say that “day of week” has no value again.