Some template sensors has stopped updating

Since 0.115 (afaik) some of my template sensors has stopped updating.

This example below is very odd as it updates when it switches to “night”.
When I manually trigger an update of template sensors from the config page it updates to the correct value, same goes if I run it through dev tools.

So something related to how template sensors are triggered in 0.115, or possible even 0.114 I’m not 100% sure.

Until we can find the root cause of this, is there any simple workaround to have this particular sensor updating every 1 minute?

    day:
      friendly_name: Period of the Day
      value_template: >-
        {% if now().strftime('%T') > strptime('22:30:00', '%T') or now().strftime('%T') < strptime('05:00:00', '%T') %}
          night
        {% else %}
          {% if now().strftime('%T') >= strptime('05:00:00', '%T') and now().strftime('%T') < strptime('08:30:00', '%T') %}
            morning
          {% elif now().strftime('%T') > strptime('12:00:00', '%T')
              and (
                (states.sun.sun.attributes.elevation | float < 11 and states.sensor.average_house_luminance.state | int < 50)
                or
                (states.sun.sun.attributes.elevation | float < 8 and states.sensor.dark_sky_cloud_coverage.state | int > 50)
                or
                states.sun.sun.attributes.elevation | float < 4
              ) %}
            evening
          {% else %}
            day
          {% endif %}
        {% endif %}

Maybe try like this?

  - platform: command_line
    name: actual_time
    command:  "date +%H:%M:%S"
    scan_interval: 60     

and then do the if statement in value_template.
Didn’t tryed it though

you need to add an entity to the template that it can “listen for” to cause the sensor to update.

try adding the sensor.time to the template (which requires you to also have the date & time integration set up which isn’t included by default)

Here is with the update sensor added to the first line of the template.

      value_template: >-
        {% set update = states('sensor.time') %}
        {% if now().strftime('%T') > strptime('22:30:00', '%T') or now().strftime('%T') < strptime('05:00:00', '%T') %}
          night
        {% else %}
          {% if now().strftime('%T') >= strptime('05:00:00', '%T') and now().strftime('%T') < strptime('08:30:00', '%T') %}
            morning
          {% elif now().strftime('%T') > strptime('12:00:00', '%T')
              and (
                (states.sun.sun.attributes.elevation | float < 11 and states.sensor.average_house_luminance.state | int < 50)
                or
                (states.sun.sun.attributes.elevation | float < 8 and states.sensor.dark_sky_cloud_coverage.state | int > 50)
                or
                states.sun.sun.attributes.elevation | float < 4
              ) %}
            evening
          {% else %}
            day
          {% endif %}
        {% endif %}

You can call the first variable what ever you want. I just find ot more obvious to call it “update” to make it explicit why I put it in there.

Thank you for your reply @finity, funny thing is I rewrote the template yesterday evening so it used sensor.time instead of now() and that made it work again!

Here’s my working version of it.

        {% if strptime(states('sensor.time'), '%T') > strptime('22:30', '%T') or strptime(states('sensor.time'), '%T') < strptime('05:00', '%T') %}
          night
        {% else %}
          {% if strptime(states('sensor.time'), '%T') >= strptime('05:00', '%T') and strptime(states('sensor.time'), '%T') < strptime('08:30', '%T') %}
            morning
          {% elif strptime(states('sensor.time'), '%T') > strptime('12:00', '%T')
              and (
                (states.sun.sun.attributes.elevation | float < 11 and states.sensor.average_house_luminance.state | int < 50)
                or
                (states.sun.sun.attributes.elevation | float < 8 and states.sensor.dark_sky_cloud_coverage.state | int > 50)
                or
                states.sun.sun.attributes.elevation | float < 4
              ) %}
            evening
          {% else %}
            day
          {% endif %}
        {% endif %}

Maybe the cleanest version would be setting a variable like you did and then use that in the code instead of getting the object each time it’s needed.