Sensor based on 'today_at' isn't updating at day change

I’m really stuck at trying to understand why two sensor values that uses the ‘today_at’ function doesn’t update like it should - at least, if I understood the function right - which i may didn’t :smiley:

The sensors are configured in configuration.yaml this way:

    bsb_lan_zeitprogramm_heizbeginn:
      friendly_name: "BSB-LAN Zeitprogramm Heizbeginn"
      value_template: "{{ today_at((states('sensor.bsb_lan_zeitprogramm_heizkreis_montag_mqtt').split()[0]).split('-')[0]) | default (0, true)}}"
      device_class: timestamp
    bsb_lan_zeitprogramm_heizende:
      friendly_name: "BSB-LAN Zeitprogramm Heizende"
      value_template: "{{ today_at((states('sensor.bsb_lan_zeitprogramm_heizkreis_montag_mqtt').split()[0]).split('-')[1]) | default (0, true) }}"
      device_class: timestamp 

As you see, i’m using another sensor (sensor.bsb_lan_zeitprogramm_heizkreis_montag_mqtt) which is delivering a string, and extract the time values needed (which is in Format HH:MM).
To get the state of the sensors to the right day - that means TODAY - i’m using the ‘today_at’ function. This should - if i understood the documentation right - deliver the given time for the present day.

To use it in Automations, I then convert it to a datetime object, which works, generally.

BUT

what happens is this, copied as screenshot out of the developer tools/template:

As you see, the template itself is working fine, it delivers the right datetime object needed - today is the 29th March.
And as you also might see, the Sensor state is stuck 4 days ago - at 25th of March. It hasn’t updated till then.
If i now reload the Sensor configuration, it updates correctly to today’s state.

But shouldn’t it automatically update at every change of day? What am I doing wrong?

Any help appreciated, thank you very much :slight_smile:

My understanding is that template sensors render when one of the referenced entities changes. In your example, if your MQTT sensor doesn’t update, the secondary sensors won’t update. There are a few ways to address the issue:

  1. Create an automation to force update the sensors using homeassistant.update_entity and/or template.reload services.

  2. Change to a trigger-based template sensor. You can then define specific events that will will cause the template to be rendered anew. For you situation your would want to trigger on restart, on the state of the MQTT sensor, and at a set time of day.

  3. Switch to binary sensors comparing your today_at() to now(). The now() function will render the template at the start of each minute. Then in your automation you would use a simple state trigger instead of a time sensor.

  4. Instead of using a template sensor, use an automation to save the MQTT sensor data to to time-only Input Datetime helpers. You could then use the entity ids of the helpers in the to: variable of you time trigger.

2 Likes

Thanks for this comprehensive answer.

Change to a trigger-based template sensor. You can then define specific events that will will cause the template to be rendered anew. For you situation your would want to trigger on restart, on the state of the MQTT sensor, and at a set time of day.

This is the solution i went with. Sensors are now configurated like this …

  - trigger:
    - platform: event
      event_type: sensors_reloaded
    - platform: mqtt
      topic: "BSB-LAN/500"
    sensor:
      - name: bsb_lan_zeitprogramm_heizbeginn
        state: "{{ today_at((states('sensor.bsb_lan_zeitprogramm_heizkreis_montag_mqtt').split()[0]).split('-')[0]) | default (0, true)}}"
        device_class: timestamp
      - name: bsb_lan_zeitprogramm_heizende
        state: "{{ today_at((states('sensor.bsb_lan_zeitprogramm_heizkreis_montag_mqtt').split()[0]).split('-')[1]) | default (0, true) }}"
        device_class: timestamp 

and updated every time the value gets reported via mqtt, which is fine since the ‘BSB_LAN’ component i’m using for this posts a lot of topics every 30 seconds and there is no possibility to set a different time pattern for each topic. So also the needed values are posted and therefore my sensors seem to be updated now.

Great :slight_smile: Thank you very much!