Turn on X when motion sensor is on, but only if it was off for X time

I’m trying to trigger an action if a motion sensor is tripped. But, because I don’t want it to happen more than once, I also want a condition to only trigger it if the same motion sensor previously was off for at least 2h30min.

The problem is when I add that last condition, it never triggers. For testing I have lowered the time in condition: state and increased before: in condition time. If I remove the last condition it works… but is then triggered again if I pass by that motion detector again.

Can I change my condition: state somehow to do this?

  - alias: Turn on the radio in the morning
    initial_state: True
    hide_entity: False
    trigger:
      platform: state
      entity_id: binary_sensor.motion_hall
      from: 'off'
      to: 'on'
    action:
      service: media_player.play_media
      data:
        entity_id: media_player.sony_x77
        media_content_id: http://sverigesradio.se/topsy/direkt/207-hi.mp3
        media_content_type: 'audio/mp3'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:30:00'
          before: '09:00:00'
        - condition: template
          value_template: '{{ states.media_player.sony_x77 != "playing" }}'
        - condition: state
          entity_id: binary_sensor.motion_hall
          state: 'off'
          for:
            hours: 2
			minutes: 30

I would create a separate input boolean, and control the behavior of that boolean with two other automations. The first one would set the boolean to true after the motion sensor has been off for two and a half hours. The second one would turn it low ten seconds after motion is detected. Then, the condition on your main automation would check this input boolean. There may be more elegant ways to do this, but that’s the best I can think of.

3 Likes

This works :slight_smile: Thanks, now I got to learn about bool inputs as well.

configuration.yaml:

 input_boolean:
   motion_hall:
     initial: off

automations.yaml:

# https://community.home-assistant.io/t/turn-on-x-when-motion-sensor-is-on-but-only-if-it-was-off-for-x-time/33509/2
# Part 1 of Play Radio
  - alias: input bool motion_hall on
    hide_entity: True
    trigger:
      platform: state
      entity_id: binary_sensor.motion_hall
      from: 'on'
      to: 'off'
      for:
        hours: 2
        minutes: 30
    action:
        service: input_boolean.turn_on
        entity_id: input_boolean.motion_hall
# Part 2 of Play Radio
  - alias: input bool motion_hall off
    hide_entity: True
    trigger:
      platform: state
      entity_id: binary_sensor.motion_hall
      from: 'off'
      to: 'on'
    action:
      - delay: '0:0:10'
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.motion_hall
# Part 3 of Play Radio
# http://sverigesradio.se/sida/gruppsida.aspx?programid=3756&grupp=10632
  - alias: God morgon!
    initial_state: True
    hide_entity: False
    trigger:
      platform: state
      entity_id: binary_sensor.motion_hall
      from: 'off'
      to: 'on'
    action:
      - service: media_player.play_media
        data:
          entity_id: media_player.sony_x77
          media_content_id: http://sverigesradio.se/topsy/direkt/207-hi.mp3
          media_content_type: 'audio/mp3'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '6:30:00'
          before: '9:00:00'
        - condition: template
          value_template: '{{ states.media_player.sony_x77 != "playing" }}'
        - condition: state
          entity_id: input_boolean.motion_hall
          state: 'on'
1 Like

I think that you shouldn’t have the for: in your last automation!

It works, but you are right, I think it’s redundant.

I think it’s actually a little worse than redundant. The boolean won’t go on till 2.5 hours of inactivity, and your condition won’t pass till 2.5 hours after that.

BTW, thank you for posting your full automation- it lets one know that their idea actually worked, and helps anyone who stumbles upon this thread in the future!

Of course, now I understand. It only triggers if input_boolean.motion_hall has been on for 2,5 hours. So it has to be idle for 5 hours. It worked fine because I sleep longer than that :slight_smile:

Fixed now. Will edit the post with the automation.

1 Like