Help establishing no recent motion

I’ve got a head-scratcher. I have this template statement to evaluate whether 180 seconds has passed since my hue motion sensor last detected motion:

'{{ as_timestamp(states.binary_sensor.hue_motion_sensor_1_motion.last_changed) + 180 > as_timestamp(now()) }}'`

If I paste this directly into the template editor, it evaluates as true or false exactly as I would expect: it renders as true when the last time motion was detected was less than 180 seconds ago; false otherwise.

However, if I do this:

binary_sensor:
  - platform: template
      recent_motion:
        value_template: '{{ as_timestamp(states.binary_sensor.hue_motion_sensor_1_motion.last_changed) + 180 > as_timestamp(now()) }}'
        friendly_name: 'Recent motion'

I get a binary sensor that evaluates as ‘off’ from the time I restart HA until the first time the hue motion sensor detects motion. Then it stays permanently on.

Likewise, if I use the similar evaluation as a condition (now using <180 because I want the ‘true’ state in reverse for this condition):

      - condition: template
          - value_template: '{{ as_timestamp(states.binary_sensor.hue_motion_sensor_1_motion.last_changed) + 180 < as_timestamp(now()) }}'

The associated automation never triggers.

Any ideas what’s going on??

now() wont trigger a template update. You need to add a time sensor to update the template. This will update every minute:

binary_sensor:
  - platform: template
      recent_motion:
        entity_id: sensor.time
        value_template: '{{ as_timestamp(states.binary_sensor.hue_motion_sensor_1_motion.last_changed) + 180 > as_timestamp(now()) }}'
        friendly_name: 'Recent motion'

This assumes you have the time sensor set up.

1 Like

presumably you could also use time_pattern and seconds: ‘/30’ if you need it quicker.
but do you need that improved accuracy ?
if you need it to the second you need to trigger a timer on the sensor going dark (for: 180) then action what you want.

    for:
      minutes: 1
      seconds: 30

No, not in a template binary sensor.

duh !
I’m stupid, sorry
Did not read the full usage scenario

I believe you can now use for: if it is used in a template condition though.

Good to know, thanks! I ended up solving my problem by using an input boolean instead of a binary sensor. I had an automation turn the boolean on if the motion sensor remained “off” for a period of time; and another automation turn the boolean off after a one second delay, any time the sensor.

I’m using three minutes because that’s what I always intended, and I don’t know how I managed to think 180 seconds = 3 minutes.

- id: prolonged_stillness
  alias: 'Toggle  if 3 minutes no motion'
  trigger:
    - platform: state
      entity_id: binary_sensor.hue_motion_sensor_1_motion
      to: 'off'
      for: '00:03:00'
  action:
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.recent_motion
- id: reset_motion
  alias: 'Switch boolean after motion'
  trigger:
    - platform: state
      entity_id: binary_sensor.hue_motion_sensor_1_motion
      to: 'on'
  action:
  - delay:
      seconds: 1
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.recent_motion

I’ll see how I go with this, but keep your solution up my sleeve in case I end up liking it better.

There’s another advantage of doing it the way you have. It will not be affected by restarts. The last_changed attribute will be.