The returned value is meant to be used as an array index, which usually start at 0. So - as I already wrote - 0 is Monday, 1 is Tuesday … 6 is Sunday.
welcome. glad it worked
Thanks for the tip about the workday sensor (which is brilliant, btw), but I don’t see how the time trigger or conditions can be used to take the date into account. The documentation only refers to hours, minutes, and seconds.
For anyone it can help:
Today is {% if now().weekday() in (0,) %}Monday{% elif now().weekday() in (1,) %}Tuesday{% elif now().weekday() in (2,) %}Wednesday{% elif now().weekday() in (3,) %}Thursday{% elif now().weekday() in (4,) %}Friday{% elif now().weekday() in (5,) %}Saturday{% elif now().weekday() in (6,)%}Sunday{% endif %}.
Thats a super long way of writing it out. Could be handled much better with this:
Today is {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}
And while both options work in the template editor, neither of them is easily converted into a sensor - for me, at least, that is:
- platform: template
sensors:
dayoftheweek:
value_template: '{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}'
results in:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
in "/home/homeassistant/.homeassistant/sensors/day_of_the_week.yaml", line 4, column 7
expected <block end>, but found '<scalar>'
in "/home/homeassistant/.homeassistant/sensors/day_of_the_week.yaml", line 4, column 29
That’s what keeps driving me crazy about working with templates.
The fact that they work in the template editor means basically: nothing at all!
thats because you are using single quotes to encapsulate your template. You need to mix quotes. Single on the inside, double on the outside. Or double on the inside single on the outside.
- platform: template
sensors:
dayoftheweek:
value_template: "{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}"
The next thing to note is this will not update every day because it doesn’t have an entity to tether to. I suggest loading the time and date platform and using that. So your final solution would be:
sensor:
- platform: time_date
display_options:
- 'time'
- platform: template
sensors:
dayoftheweek:
value_template: "{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}"
entity_id: sensor.time
Also as a side note in regards to this comment:
You can ensure that it works from the template editor by using a second line instead of making it single like, like so:
- platform: template
sensors:
dayoftheweek:
value_template: >
{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}
This method does not require the outside quotes, so you can write the code as-is.
Thanks - appreciate the explanation and the background
I read a lot of posts from you over the time and learned a lot. But I still often fail at the basics.
E.g. mixing quotes makes sense so that the system doesn’t get confused about what belongs together, but discovering the reason of why it fails in my case remains a big hurdle for me. Especially transferring something that works in the template editor to a piece of working config.
The error reading can be painful. They are all python errors, so you need python experience to understand them. Best bet to start learning them is to take a few free python tutorials online.
Here you all go. I wrote these templates for a finetuning, focusing on the notify service.
I want it to be as Google Assistant as possible.
My templates:
- platform: template
sensors:
dayoftheweek:
value_template: "{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}"
entity_id: sensor.time
- platform: template
sensors:
month:
value_template: "{{ ['January','February','March','April','May','June','July','August','September','October','November','December'][now().month-1] }}"
entity_id: sensor.time
- platform: template
sensors:
dateofthemonth:
value_template: "{{ ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21th','22th','23th','24th','25th','26th','27th','28th','29th','30th','31th' ][ now().day-1] }}"
This can all be rewritten into my notify-script, that looks like this:
{% if now().strftime("%H")|int < 12 %}Good morning, {% elif now().strftime("%H")|int < 18 %}Good afternoon, {% else %}Good evening, {% endif %}{{ ["Master","Sir","Master Wayne","Sire"] | random }}.
The time is {{states.sensor.time.state}}.
Today is a {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }} and the date is the {{states.sensor.dateofthemonth.state}} of {{states.sensor.month.state}}.
It's currently {{states.sensor.dark_sky_summary.state}} and {{states.sensor.dark_sky_temperature.state}} degrees at home. Today will be {{states.sensor.dark_sky_hourly_summary.state|replace('.','')}}, with a high of {{states.sensor.dark_sky_daytime_high_temperature_0d.state|round(1)}} degrees.
{% if states.binary_sensor.workday_sensor %}It will take {{ states.sensor.drive_to_work.attributes.duration_in_traffic }} to drive to work today in the current traffic, since you're {{ states.sensor.drive_to_work.attributes.distance|replace('km','')|float }} kilometers away.{% endif %}
Have a{{ [" pleasant", " amiable", " charming", " cheerful", " delightful", "n enjoyable", " lovely", " pleasing", " satisfying"] | random }} day!
This results in a pleasant notification with a few randomly chosen names and titles after my pleasure:
Good afternoon, Master Wayne.
The time is 15:05.
Today is a Wednesday and the date is the 15th of May.
It’s currently Partly Cloudy and 14.0 degrees at home. Today will be Mostly cloudy until tomorrow afternoon, with a high of 14.0 degrees.
It will take 1 min to drive to work today in the current traffic, since you’re 0.8 kilometers away.
Have a** satisfying day**!
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
forecast:
- 0
in the dark sky sensor configuration.
Here is mine:
- platform: darksky
api_key: !secret dark_sky_api_key
latitude: !secret latitude
longitude: !secret longitude
units: si
forecast:
- 0
monitored_conditions:
- summary
- icon
- precip_type
- precip_intensity
- precip_probability
- temperature
- temperature_high
- apparent_temperature
- dew_point
- wind_speed
- wind_bearing
- cloud_cover
- humidity
- pressure
- visibility
- ozone
- hourly_summary
- daily_summary
- uv_index
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.
According to the documentation for strftime, they’ll be rendered using the locale’s setting.
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
%A
Weekday as locale’s full name.
Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE)
I haven’t tested it in Home Assistant. If it always defaults to English then I guess Home Assistant doesn’t support locale?
It’s always English in home assistant. It does not change the datetime locale.
OK. Then for all those who need a language other than English, they can continue to use the other examples that have been provided (using a list).
Hi,
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, I managed to solve this by moving “Good” to the front, and removing it from the morning, afternoon, evening…
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?