Template sensor, value dont update

Hi
I’m trying to setup template sensors based on my motion sensors. I’m planning on using them as input for Bayesian sensors.

platform: template
    sensors:
      template_sov_now_vs_motion_3min:
        friendly_name: 'Sov - Now vs Motion'
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 }}"
        # value_template: "{% if (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 %} true {% else %} false {%endif%}"

But the problem is that the sensor template_sov_now_vs_motion_3min don’t update. I tried both variants as shown above. If I test the value_template in the template editor, then it works fine.

This is the editor output. The result is False as it should be, be the template sensor is still true/on.
If I changed to > instead of <, its just off all the time. I tried using float instead of int, but no luck.

{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 }}
{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) }}
{{ as_timestamp(states.binary_sensor.sov_sen_motion.last_changed) }}
{{ as_timestamp(now()) }}

False
2101.6955749988556
1588323006.937521
1588325108.633439

Kind regards
/Lars

it can’t find an entity to update on. add the entity_id to the sensor:

  platform: template
    sensors:
      template_sov_now_vs_motion_3min:
        friendly_name: 'Sov - Now vs Motion'
        entity_id: binary_sensor.sov_sen_motion
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 }}"
        # value_template: "{% if (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 %} true {% else %} false {%endif%}"

That didn’t make any difference, it still don’t update.I thought the template sensor refreshed every x seconds, but do it need an entity state change to “refresh on”.?
I tried creating a second template sensor with a Hue motion sensor binary_sensor.stue_sen_motion, but with same result.
Can i use a different entity to force the update?

templates only update when a entity inside the template updates. If you want it to update every xx seconds, you need to find an entity in your sytem that updates every xx seconds and use that in the entity_id field. Otherwise you can get minutely updates by integrating the time integration and using sensor.time.

Can i use sensor.time like this? Because i still get double true, despite they should be false.

   - platform: template
    sensors:
      template_sov_now_vs_motion_3min:
        friendly_name: 'Sov - Now vs Motion'
        entity_id: sensor.time
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.sov_sen_motion.last_changed)) | int < 180 }}"
      template_stue_now_vs_motion_3min:
        friendly_name: 'Stue - Now vs Motion'
        entity_id: sensor.time
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.stue_sen_motion.last_changed)) | int < 180 }}"

yep, but you have to add the integration that makes sensor.time.

Ah, was a bit to quick. Thanks you very much for the help

sensor:
  - platform: time_date
    display_options:
      - 'time'
1 Like