Template Binary Sensor with Now()

I would like to create a template binary sensor that is on when a camera’s motion sensor entity is ‘on’ AND a light’s entity did not just turn on (or off) within the last 1 second. This is to avoid false triggers when the light turns on. Unfortunately, I am unable to refine the settings in the camera itself to avoid those false triggers. So to create a new binary sensor that simply disregards that brief period when the light last updates seems to be the best path forward.

When I use this code in the developer tools, it works fine. The binary sensor is on only when the camera’s motion sensor entity is on, but not when the light itself just turned on or off within the last second.

{{
  is_state('binary_sensor.side_door_camera_motion_0','on') and
  now() > states.light.mudroom_light.last_changed + timedelta( seconds = 1)
}}

but when I use this in a template sensor, it does not work. I think it may have something to do with the way now() works when a template sensor triggers. But I can’t figure it out. Here is the template sensor I have tried:

template:

  - binary_sensor:

      - name: Side Door Camera Motion Human
        state: >-
          # ignore camera motion when mudroom light is turned off or on
          {{
            is_state('binary_sensor.side_door_camera_motion_0','on') and
            now() > states.light.mudroom_light.last_changed + timedelta( seconds = 1 )
          }}

I’ve played around with using a trigger based sensor, but I can’t make anything work. Any thoughts?

Not sure if the new lines after,

template:

And after,

binary_sensor:

Are intentional or a copy / paste error, but they should not be there.
Also try moving your comment above state: so it is not part of the state block.

Just a thought.

My entire config file contains spaces and comments, so it’s not an obvious formatting issue from what I can see.

Just made i try with one of my template binary sensor. If i add a comment line after

state: >-

my sensor also don’t work anymore. Seems the # is ignored in this place. Does it work when you remove the line?

1 Like

Yes I did speak too soon, my apologies to @pcwii , it works without the comment line. It seams like I’m getting inconsistent results with the actual sensor, but I’ll keep working on that aspect of it. Thanks folks.

1 Like