Sensor when other sensor stops updating

This has been asked before a few times, but with the recent changes I am not sure how to update this sensor.

I have a MQTT sensor (bedroom temp/humidity), but rarely those sensors kinda hang and stop updating, or the battery just runs out, which is fine but I just want a notification when this happens.

So far I have this:

  - platform: mqtt
    name: "Master Temperature"
    state_topic: "homeassistant/rtl_433/sensor_15524"
    unit_of_measurement: '°C'
    value_template: "{{ value_json.temperature_C }}"
  - platform: mqtt
    name: "Master Humidity"
    state_topic: "homeassistant/rtl_433/sensor_15524"
    unit_of_measurement: '%'
    value_template: "{{ value_json.humidity }}"
  - platform: template
    sensors:
      master_no_activity:
        friendly_name: "Master Bedroom Sensor No Activity"
        value_template: >
          {{ (now().timestamp() - as_timestamp(states.sensor.master_temperature.last_changed)) | int //60 > 30 }}

But for some reason this no_activity sensor isn’t updating anymore since 0.115 and the template sensor changes.

It might be the ‘timestamp’ parts of your template, I’d a similar problem earlier today. Here’s the graciously solution to that:

This Template Sensor will be updated only when sensor.master_temperature changes state. That’s the way it has worked in all versions of Home Assistant, including the latest 0.115.

  - platform: template
    sensors:
      master_no_activity:
        friendly_name: "Master Bedroom Sensor No Activity"
        value_template: >
          {{ (now().timestamp() - as_timestamp(states.sensor.master_temperature.last_changed)) | int //60 > 30 }}

Home Assistant will create a “listener” for each entity it can identify in your template (and the listener monitors the entity for state-changes). There is only one identifiable entity in that template (sensor.master_temperature). The other part of it is now() and that’s a function, not an entity (so no listener is created for it).

If you have the Time & Date integration configured, you can use sensor.time in your template. Home Assistant will create a listener for sensor.time. It changes state every minute, thereby causing your template to be evaluated every minute.

  - platform: template
    sensors:
      master_no_activity:
        friendly_name: "Master Bedroom Sensor No Activity"
        value_template: >
          {% set x = states('sensor.time') %}
          {{ (now().timestamp() - states.sensor.master_temperature.last_changed.timestamp()) | int // 60 > 30 }}

Thanks @Taras, that works great again.

1 Like