Template sensor not reflecting template state

I have a sensor set up that changes after if a motion detector hasn’t been triggered for some time:
The template evaluates correctly however the sensor always shows the sensor to be on since hass version 29.6.

Any idea?

configuration.yaml:

binary_sensor
- platform: template
   sensors:
      hall_movement:
        friendly_name: Hall Movement
        value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.hall_motion.last_changed) <= 1800) and is_state("binary_sensor.hall_motion","off")}}'

1 Like

Its really strange:

{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_motion.last_changed) < 1800)}}

evaluates to FALSE in the dev tools of HASS however the sensor defined like this remains ON:

sensor:
 - platform: template
   sensors:
      bathroom_movement:
        friendly_name: Bathroom Movement
        value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_motion.last_changed) < 1800)}}'

I noticed the same behavior since I updated from 28.x.
The GUI is not updated in the same frequency as before it seems as the template renders to the expected value in the developer tools but on the GUI it remains on the same value.

I track the status of a MQTT device based on when the last update was done using:
{{ relative_time(states.sensor.study_heartbeat.last_changed) }}

This in the GUI always renders to 0 seconds but under the developer tools it shows the correct value.

@balloob is there something changed in the way the GUI is updated here?

This might help. I was using that too.



Cheers Daniel for pointing this out.
Not sure what the status is though as I see several opinions on this issue :slight_smile:

Adding a time sensor to make sure time sensitive sensors are updated regularly is hopefully an interim work around as it defeats the automation objective here…

The problem is not only with relative_time but with anything you do on the GUI with time functions, eg:
{% if states.switch.valve_1.state == ‘on’%}Opened {{((as_timestamp(now()) - as_timestamp(states.switch.valve_1.last_changed)) / 60) | round(0) }} minutes ago{% else %}Closed{% endif %}

Shows Closed or Opened based on based on the state but the as_timestamp(states.switch.valve_1.last_changed) will always result in zero in this case so it will always show “Opened 0 minutes ago”

ronvl I have noticed the exact same behaviour with “Opened 0 minutes ago” in the GUI, have you progressed this matter in the last month?