Triggering delay in automation

I am taking my first stab at automating one of my wemo insight switches to turn on when the Amcrest camera in the room senses motion. Once the light has been turned on if the camera doesn’t sense motion for 15 minutes the light turns off. I have the automations working, however, when the automation triggers to turn off the light after 15 minutes the camera senses it as motion and is triggering the light on again. Is there any way to disable the first animation or build in some sort of delay to prevent this “switching loop” from occurring?

- alias: Turn on Kid's light during the day when motion is sensed
  hide_entity: False
  trigger:
    platform: state
    entity_id: sensor.amcrest_motion_detected 
    from: 'False'
    to: 'True'
 #Trigger only between 10am and 7pm 
  condition:
    condition: time
    after: '10:00:00'
    before: '19:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.wemo_insight__kids_room_

- alias: Turn off Kid's light after 15 minutes without motion
  hide_entity: False
  trigger:
    platform: state
    entity_id: switch.wemo_insight__kids_room_
    state: 'on'
  condition: 
    condition: state
    entity_id: sensor.amcrest_motion_detected
    state: 'False'
  # Trigger only if state was this for last 15 minutes.
    for:
      minutes: 15  
  action:
    service: switch.turn_off
    entity_id: switch.wemo_insight__kids_room_

Thanks in advance.

1 Like

You could change the action for the “turn off” to the following (check the automation id, I’ve never used aliases with quotes, so don’t know how it translates)

  action:
    - service: homeassistant.turn_off
      entity_id: automation.turn_on_kids_light_during_the_day_when_motion_is_sensed
    - service: switch.turn_off
      entity_id: switch.wemo_insight__kids_room_
    - delay: '00:00:01'
    - service: homeassistant.turn_on
      entity_id: automation.turn_on_kids_light_during_the_day_when_motion_is_sensed

Basically, when it’s time to turn the light off, turn off the automation that turns the light on, turn off the light, pause for one second, then turn the automation back on. You may not need the delay, or it may need to be longer, depending on your motion sensor’s speed.

I use something very similar for a door sensor and lights, such that it’s the opening of the door that turns on the lights, but the second closing of the door that turns them off again.

5 Likes

Great! That’s what I was hoping, but I wasn’t sure if i would be able to invoke multiple actions off a single trigger. I’ll give it a shot when I get home from work and let you know.

worked like a charm. thank you.

Hi

I used your method to work my automation but surprisingly I cannot get it to work. What I mean is that after the sensor has detected motion and the lights are on via MQTT (433mhz) I want them to be on for 10 seconds before they turn off after the last motion. My automation is as follows:

automation:
- alias: Bedroom Lights Motion On
  trigger:
    platform: mqtt
    topic: home/433toMQTT
    payload: "15139978"
  action:
   service: switch.turn_on
   entity_id:
    - switch.bedroom_lights

- alias: Bedroom Lights Motion Off
  trigger:
    platform: mqtt
    topic: home/433toMQTT
    payload: "15139978"
  action:
    - delay:
        seconds: 10  
    - service: switch.turn_off
      entity_id: switch.bedroom_lights

Any help will be appreciated please.

Can I ask you what state is ‘false’ ? does that mean that when no motion is detected or is it the last motion which is detected?

It looks like the trigger is the same for both automations. I’ve not used MQTT triggers, but that doesn’t seem right to me.

You may, given the way you’re doing this, need to use scripts as shown here. That way every time you detect motion, you’re restarting that timer.

1 Like

so I done some changes in my code and remove the motion sensor to turn off the lights, here it is:

- alias: Bedroom Lights Motion Off
  trigger:
    platform: state
    entity_id: switch.bedroom_lights
    state: 'on'
  action:
    - delay:
        seconds: 10
    - service: switch.turn_off
      entity_id: switch.bedroom_lights

I am trying to switch off the lights after 10 seconds as soon as they are on but surprisingly it doesnt work and the lights remain on. Cannot understand what am I doing wrong?

Got it working here is my code:

automation:
- alias: Bedroom Lights Motion On
  trigger:
    platform: mqtt
    topic: home/433toMQTT
    payload: "15139978"
  condition:
    condition: time
    after: '00:45:00'
    before: '06:00:00'
  action:
   service: switch.turn_on
   entity_id:
    - switch.bedroom_lights


- alias: Bedroom Lights Motion Off
  trigger:
    platform: state
    entity_id: switch.bedroom_lights
    state: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: automation.bedroom_lights_motion_on
    - delay: '00:10:00'
    - service: switch.turn_off
      entity_id: switch.bedroom_lights
    - delay: '00:00:01'
    - service: homeassistant.turn_on
      entity_id: automation.bedroom_lights_motion_on

I have the same problem and tried using the same approach without any luck. When a zone is switched on the master relay must be turned on 5 seconds later. Where do I put the “Delay”?

  • alias: ‘Master on when Zone on’
    trigger:
    - entity_id: switch.Zone1
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone2
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone3
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone4
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone5
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone6
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone7
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone8
    from: ‘off’
    platform: state
    to: ‘on’
    - entity_id: switch.Zone9
    from: ‘off’
    platform: state
    to: ‘on’
    action:
    service: homeassistant.turn_on
    entity_id: switch.Master

try the following:

alias: 'Master on when Zone on’
trigger:
- entity_id: switch.Zone1
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone2
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone3
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone4
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone5
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone6
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone7
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone8
from: 'off’
platform: state
to: ‘on’
- entity_id: switch.Zone9
from: 'off’
platform: state
to: 'on’
action:
delay: ’00:00:05'
service: homeassistant.turn_on
entity_id: switch.Master

Thank you for the suggestion. I tried that but then the automation does not load.

Did you copy paste the code? The spacing and syntax is not correct.

No I realized the syntax and spacing was wrong but it still did not load the automation.

It looks like you are using different types of ‘. Try making sure it is all the same, at least at each state.

Otherwise you could try smal letter for Z and M in the entity_id

I tried changing the Z and M to lower case and made sure that all the ''s are correct. It still doesn’t work. I also tried adding a hyphen to the front but no luck.

I manage to get this working by adding a “for:” to each trigger entity. My code is as follow:

- alias: 'Master on when Zone on'
  trigger:
     - entity_id: switch.zone1
       from: 'off'
       platform: state
       to: 'on' 
       for: 
         seconds: 5
     - entity_id: switch.zone2
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5       
     - entity_id: switch.zone3
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone4
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone5
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone6
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone7
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone8
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
     - entity_id: switch.zone9
       from: 'off'
       platform: state
       to: 'on'
       for: 
         seconds: 5
  action:
     service: homeassistant.turn_on
     entity_id: switch.master

Please help with this automation. Check from yamllint is right but nothing happen even I manually turn on input boolean.

- id: away
  alias: "Away"
  initial_state: True
  hide_entity: False
  condition:
    condition: state
    entity_id: input_boolean.sunhome
    state: 'off'
  trigger:
    - platform: state
      entity_id: device_tracker.sun_iphone
      state: 'away'
      for:
        minutes: 1
    - platform: state
      entity_id: device_tracker.me_sun_iphone
      state: 'away'
      for:
        minutes: 1
    - platform: state
      entity_id: device_tracker.diana_diana_iphone
      state: 'away'
      for:
        minutes: 1
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.sunhome
- id: home
  alias: "Home"
  initial_state: True
  hide_entity: False
  condition:
    condition: state
    entity_id: input_boolean.sunhome
    state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.sun_iphone
      state: 'home'
      for:
        minutes: 1
    - platform: state
      entity_id: device_tracker.me_sun_iphone
      state: 'home'
      for:
        minutes: 1
    - platform: state
      entity_id: device_tracker.diana_diana_iphone
      state: 'home'
      for:
        minutes: 1
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.sunhome
    - delay: '00:00:10'
    - service: homeassistant.turn_on
      entity_id: automation.away
- id: office_camera_motion_on
  alias: "Office Camera Motion On"
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: input_boolean.sunhome
    from: 'off'
    to: 'on'
    for: 
      seconds: 5
  action:
    - service: switch.turn_on
      entity_id: switch.office_camera_motion
- id: office_camera_motion_off
  alias: "Office Camera Motion Off"
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: input_boolean.sunhome
    from: 'on'
    to: 'off'
    for: 
      seconds: 5
  action:
    - service: switch.turn_off
      entity_id: switch.office_camera_motion
    - delay: '00:00:05'
    - service: homeassistant.turn_on
      entity_id: automation.office_camera_motion_on

I’m no expert yet but from I can see there are some indentations that is wrong. An example

for:
seconds: 5

should be

for:
  seconds: 5

I’m not sure about the double quotes (") that you use. I use single quotes (’).

Thank you very much for your reply. I will try again this evening.