Problem with sensor not refreshed

Hello

Since a couple of weeks (maybe after release 0.115 but not sure as I am not using this sensor very often in my automations) the following sensor is not refreshed unless I am restarting HA… I added the “sensor.date” to have it refreshed once a day, no change… Anything to help me find what’s wrong ? I am running HA in a venv, python 3.7.3 and HA 0.116…

#
# weekday sensor
#
- platform: template
  sensors:
    week_day:
      value_template: "{% if now().weekday() in (0,) %}Lundi
                       {% elif now().weekday() in (1,) %}Mardi
                       {% elif now().weekday() in (2,) %}Mercredi
                       {% elif now().weekday() in (3,) %}Jeudi
                       {% elif now().weekday() in (4,) %}Vendredi
                       {% elif now().weekday() in (5,) %}Samedi
                       {% elif now().weekday() in (6,) %}Dimanche
                       {% else %}states.sensor.date.state
                       {% endif %}"
      friendly_name: 'Day of the Week'
      unit_of_measurement: "Days"

Look at this
https://community.home-assistant.io/t/heads-up-upcoming-breaking-change-in-the-template-integration/223715

Try this instead:

      value_template: >
        {% set update = states('sensor.date') %}
        {% set days = ['Lundi', 'Mardi', 'Mercredi','Jeudi', 'Vendredi', 'Samedi', 'Dimanche'] %}
        {{ days[now().weekday()] }}

@pollinolas @tom_l Thanks, I have updated the code… I will see tomorrow morning if the value has been updated…

I had a similar problem. If you use now() in a template what you get is not so much a timestamp as a snapshot of the timestamp as it was when configuration.yaml was loaded - so it doesn’t update without a restart.

My solution was to add unique_id: today to the template, which gave me a sensor sensor.today which could be updated from an automation. If you’re only after days of the week this is quite manageable - everyone must have an automation running at midnight.

While that is quite valid, there is no need for an automation. Adding this to the template:

{% set any_name_here = states('sensor.date') %}

Lets home assistant monitor the date sensor and updates the template if it changes (at midnight).

The same thing can be done with sensor.time if you want the template to update every minute.

1 Like

Good to know. My solution would not be much use if you wanted frequent updates.