I was trying to create new sensor with current day of the week and then use it in some automations/scripts or UI.
This is my sensor configuration
day_of_the_week:
entity_id: sensor.date
friendly_name: "Day of the week"
value_template: >
{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}
Unfortunatelly it looks like it’s not updating daily. It is read once during startup and then nothing happens. So today it’s set to Tuesday even it’s Wednesday already.
It’s part of the Time and Date component and you have to define it in your configuration file (or sensor.yaml file) before you can use it in template sensors, automations, etc.
I can’t think of anything in the sensor domain that’s in-built. If you’re assigning something to entity_id it must be an entity. Entities are defined manually or through discovery.
I have a similar sensor on my config - but it looks like it stopped updating a couple of days ago. sensor.date and sensor.time both seem to be updating fine, but sensor.dayoftheweek, sensor.dayoftheweek_tomorrow and sensor.isfirstthursday are not Any ideas please?
#----- Day of the week sensor
- platform: time_date
display_options:
- 'time'
- 'date'
- platform: template
sensors:
dayoftheweek:
value_template: "{{ ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'][now().weekday()] }}"
entity_id: sensor.time
dayoftheweek_tomorrow:
value_template: "{{ ['tuesday','wednesday','thursday','friday','saturday','sunday','monday'][now().weekday()] }}"
entity_id: sensor.time
isfirstthursday:
value_template: '{{(now().strftime("%d")|int>=1) and (now().strftime("%d")|int <=7) and (now().strftime("%w")|int==4)}}'
entity_id: sensor.time
Thank you so much - I’d missed that breaking change and the post you shared is super helpful.
Looks like this fixed the issue now. My new code looks like this:
- platform: time_date
display_options:
- 'time'
- 'date'
- platform: template
sensors:
dayoftheweek:
value_template: >
{% set x = states('sensor.time') %}
{{ ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'][now().weekday()] }}
dayoftheweek_tomorrow:
value_template: >
{% set x = states('sensor.time') %}
{{ ['tuesday','wednesday','thursday','friday','saturday','sunday','monday'][now().weekday()] }}
isfirstthursday:
value_template: >
{% set x = states('sensor.time') %}
{{(now().strftime("%d")|int>=1) and (now().strftime("%d")|int <=7) and (now().strftime("%w")|int==4)}}
Thanks for your suggestion - I tried it out and I can confirm it works. However can you explain this part of the code for me as I’m not sure why/how it works?