Template sensor not updating?

I have a template sensor which calculates the difference between a timestamp read from a MQTT sensor and current time.

My problem: the template sensor obviously only updates, when sensor.landroid_last_update_timestamp changes, resulting in the template sensor showing a difference of ‘0 min’ most of the times, except from when I restart HA.

  - platform: mqtt
    name: Landroid last update timestamp
    state_topic: "landroid/status/dateTime"

  - platform: template
    sensors:
      landroid_last_update:
        friendly_name: "Landroid last update"
        value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.sensor.landroid_last_update_timestamp.state))/60)|round|int }}"
        unit_of_measurement: "min"

There is a warning in the Templating doc regarding templates with time:

Rendering templates with time is dangerous as updates only trigger templates in sensors based on entity state changes.

I think, this is related to my problem, but while I think my understanding of english language is quite good, I have to admit, I don’t understand the meaning of this warning… :grinning:

Could someone please explain this warning to me in other/simple words or with a short example? How I’d have to change my config to circumvent this behaviour?

Template sensors only update based on actual sensors in your template equation/code. now() is not considered a sensor. So your template will only update when sensor.landroid_last_update_timestamp updates.

To have this update live, every minute, you need to use a home assistant entity that will update every minute. You can use the date time component:

it would look like this, mind you, you have to combine this with your current template sensors, don’t copy the top most level yaml sensor, platform: template, and sensors into your config.

sensor:
  - platform: time_date
    display_options:
      - 'date_time'

  - platform: template
    sensors:
      correct_hour:
        value_template: >
          {% set current_time = strptime(states.sensor.time.state, '%Y-%m-%d, %H:%M') %}
          {{ ((as_timestamp(current_time) - as_timestamp(states.sensor.landroid_last_update_timestamp.state))/60)|round|int }}
2 Likes

Thanks for the head up, @petro.

If somebody else wants to use this, below is my final configuration. I shortened it to a one-liner, ommiting the usage of the temporary variable current_time.

Please note, that the time_date sensor’s internal name will be sensor.date__time (yes, with two underscores), as it is derived from the friendly name “Date & Time”, which is hard coded in time_date.py and can’t be overridden by an own friendly_name or internal_id. Don’t know why, but anyways.

  - platform: mqtt
      name: Landroid last update timestamp
      state_topic: "landroid/status/dateTime"

  - platform: time_date
    display_options:
      - 'date_time'

  - platform: template
    sensors:
      sensor.landroid_last_update:
        value_template: "{{ ((as_timestamp(strptime(states.sensor.date__time.state, '%Y-%m-%d, %H:%M')) - as_timestamp(states.sensor.landroid_last_update_timestamp.state))/60)|round|int }}"
        unit_of_measurement: "min"

Just for the record: I also excluded both timestamp sensors (sensor.date__time and sensor.landroid_last_update_timestamp) from being recorded in the history database. Recording timestamps of the current time represented as strings isn’t very useful in most cases. :smile: U don’t get readable history graphs from them, just a lot of useless data filling up the database. If you want to configure something similar, you may also consider excluding your timestamp sensors:

recorder:
  exclude:
    entities:
      - sensor.landroid_last_update_timestamp
      - sensor.date__time
2 Likes