Automation condition - within X time ago

Hi All,

This might be a bleedingly obvious one but I just haven’t found anything in my search.

Ultimately I’m trying to create a difference between opening a door to leave vs opening a door to enter.

I have a lamp that turns on when the front door opens based on some basic ‘sunset’ conditions. But of course it will turn on when I leave at night also.

Id like to use my Xiaomi motion sensor to help simulate the difference between entry and exit. i.e, if there was motion in my living room within 1 minute of opening the door, then I’m mostly likely exiting and don’t want the lamp to turn on.

So, is there was a way I can call a condition for say: sensor state was ‘on’ (motion detected) within 1 minute ago? May also turn if the sensor state was ‘off’ for less than 1 minute?

Thanks a bunch :slight_smile:

Lint

Try like this:

- alias: Motion Detection - Frontdoor Lamp
  trigger:
    platform: state
    entity_id: binary_sensor.front_door
    to: 'on'
  condition:
    condition: template
    value_template: "{{ states.binary_sensor.motion_sensor.attributes['No motion since']  > "60"  }}"
  action:
    service: light.turn_on
    entity_id: light.porch_light  

Wow bob that looks great thank you :slight_smile: makes total sense now that I see it.

I’ll be able try it out and report back in a little while :slight_smile:

@Bob_NL I’ve been trying to get this sucker up and running but it keeps throwing me errors:

ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/automations.yaml”, line 69, column 9
expected , but found ‘’
in “/config/automations.yaml”, line 70, column 91

I’ve tried changing ‘/’’ etc but no tangible dice.

Here is my code back to how you suggested it:

- alias: dusk_entry_lamp
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001ad5593
    to: 'on'
  condition:
    condition: and
    conditions:
 
      - condition: sun
        after: 'sunset'
        after_offset: '-01:15:00'
  
       - condition: state
         entity_id: media_player.tv
         state: 'off'
       - condition: template
         value_template: "{{ states.binary_sensor.motion_sensor_158d0001ad4b4a.attributes['No motion since']  > "60"  }}"

  action:
    service: homeassistant.turn_on
    entity_id: switch.plug_158d0001dca301 

Line 69 is condition template
Line 70 is value template

(my indenting may not be perfect with this embedded code)

I feel like this is might be a simple formatting error?

I use something like this

Thanks for the input Rob, I’ll give that a shot also :slight_smile: good to have a few ways of doing things under my belt

@LintHart

Your errors are with the value template and the quotes being used. If you open it with " then you have to use ’ inside the expression otherwise it gets confused.

1 Like

@LintHart try this:

value_template: "{{ (states.binary_sensor.motion_sensor.attributes['No motion since'])  > 60  }}"

or this:

value_template: "{{ (states.binary_sensor.motion_sensor.attributes['No motion since']) | int  > 60  }}"

Your indentation is wrong indeed, check if your yaml is formatted the same.

Thanks heaps Bob. Looks like the integer did it :smiley:

Seems to be working as I’d hoped but I also realised that some of my testing way flawed as the ‘no motion since’ on the dashboard doesn’t update as fast as I’d like sometimes but thats another story :slight_smile:

Thankfully my indentation was all good, just poor first use of the code embed in the thread :slight_smile:

Hopefully this will help someone else’s search. Here is my now (and hopefully forever) working automation. I’ll check back in if I break it :stuck_out_tongue:

Intent - If I walk in the front door (Xiaomi door sensor) at dusk or later ~ 1.25 hours before sunset, turn on my lamp (Xiaomi smart plug).
But only we are coming home after a while. This would mean the TV is off. But to make sure its not triggered when I leave the apartment, make sure there has been no motion inside the apartment for at least a minute (which will probably be a little longer by the time it updates on default ping settings).

I’m leaving this unedited so that other people new to the space like me can see all the random digits and sensor names they might recognise in their own setup.

Thank you all for you help. Now onto the next drama :stuck_out_tongue:

- alias: dusk_entry_lamp
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001ad5593
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: 'sunset'
        after_offset: '-01:15:00'
      - condition: state
        entity_id: media_player.tv
        state: 'off'
      - condition: template
        value_template: "{{ (states.binary_sensor.motion_sensor_158d0001ad4b4a.attributes['No motion since']) | int  > 60  }}"
  action:
    service: homeassistant.turn_on
    entity_id: switch.plug_158d0001dca301
2 Likes

Nice to hear that it works now and great of you to report back with a detailed explanation for fellow readers (Y)

Hi I’m Trying the same thing but instead of “No Motion since” of my sensor I want to get the last updated time from my sensor.

So I’m taking something like below
“{{ (states.binary_sensor.movement.attributes.last_updated) | int > 60 }}”

But I’m getting Message malformed: not a valid value for dictionary value @ data[‘condition’][0][‘condition’] Error, Could any one please help.

Note: I’m doing the automation from the Home Assistant Automation Editor.

Never mind !! Got it to work. Thanks Everyone :slight_smile:
My Automation looks like the following.

- id: '1516907547299'
  alias: Turn on things when I come home
  trigger:
  - entity_id: binary_sensor.front_door
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: '18:30:00'
    before: 05:00:00
    condition: time
  - condition: state
    entity_id: binary_sensor.movement
    for:
      minutes: 1
    state: 'off'
  action:
  - data:
      entity_id: switch.cool_led
    service: switch.turn_on
  - data:
      entity_id: switch.drawing_room
    service: switch.turn_on
  - data:
      entity_id: switch.fan
    service: switch.turn_on