Actions for a specific Amount of Time

I have a garage door sensor that when tripped after 2045 I would like certain lights to come on and stay on for 10 minutes. I didn’t see a previous post about this, so posting the code I have so far. Any help is appreciated

- id: '1640117523093'
  alias: Lights at Odd Hours
  description: Turn on lights after 2045
  trigger:
  - type: opened
    platform: device
    device_id: 88a19c87170706d5fe217531196f99ef
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
  condition:
  - condition: time
    after: '20:45:00'
  action:
  - service: light.turn_on
    target:
      entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom
  - duration: 10 minutes
  - service: switch.turn_on
    target:
      entity_id: switch.double_plug_left
  - duration: 10 minutes
  mode: single

So far, this hasn’t worked for the specific time, the lights come on, but then they stay on until I manually turn them off. What am I missing?

This would be the simplest way to achieve this course of actions:

action:
  - service: switch.turn_on
    target:
      entity_id: switch.plug_5
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id: switch.plug_5

The first action is to turn the switch on, the there’s a 10min delay, and then the second action is to turn the switch off - without the last piece, the switch will just stay on.

So all of that action will happen with the opening of the garage door sensor? Is this how it should be written?

- id: '1640117523093'
  alias: Lights at Odd Hours
  description: Turn on lights after 2045
  trigger:
  - type: opened
    platform: device
    device_id: 88a19c87170706d5fe217531196f99ef
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
  condition:
  - condition: time
    after: '20:45:00'
  action:
  - service: light.turn_on
    target:
      entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
    entity_id:
    - light.livingroom
    - light.pc
    - light.hallway
    - light.bedroom   
  - service: switch.turn_on
    target:
      entity_id: switch.double_plug_left
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
  target:
    entity_id: switch.double_plug_left
    mode: single

You need to match your service to your domain here:

  - service: switch.turn_off
    target:
    entity_id:
    - light.livingroom
    - light.pc
    - light.hallway
    - light.bedroom   

i.e. use light.turn_off instead of switch.turn_off

And indentations are not 100% either, especially in the last 3 lines.
Doing the whole automation in the visual editor will avoid these issues.

Made the switch but still getting an error about the light.turn_off. This was done in the visual editor…

- id: '1640117523093'
  alias: Lights at Odd Hours
  description: Turn on lights after 2045
  trigger:
  - type: opened
    platform: device
    device_id: 88a19c87170706d5fe217531196f99ef
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
  condition:
  - condition: time
    after: '20:45:00'
  action:
  - service: light.turn_on
    target:
      entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
    entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom  
  - service: switch.turn_on
    target:
      entity_id: switch.double_plug_left
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
  target:
    entity_id: switch.double_plug_left
    mode: single

If you compare the indentation after light.turn_on with the indentation after light.turn_off you’ll notice a difference.

The last 3 lines are still out of whack as well.

Now I see the issue, it was with the entity line above. This is why I never became a coder…my eyes start to hurt looking the screen, even with the rainbow tabs…

- id: '1640117523093'
  alias: Lights at Odd Hours
  description: Turn on lights after 2045
  trigger:
  - type: opened
    platform: device
    device_id: 88a19c87170706d5fe217531196f99ef
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
  condition:
  - condition: time
    after: '20:45:00'
  action:
  - service: light.turn_on
    target:
      entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id:
      - light.livingroom
      - light.pc
      - light.hallway
      - light.bedroom  
  - service: switch.turn_on
    target:
      entity_id: switch.double_plug_left
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
    entity_id: switch.double_plug_left
  mode: single

You might still have the same issue with the 2nd to last line, tough.

And yes, I had the same issues when I started - that’s why I mostly rely on the visual editor now and fiddle with the raw yaml only when absolutely necessary :grin:

I’ll make that change.

Appreciate the help greatly!

1 Like

Made the changes, however the event doesn’t trigger if I come through the door at say 2100, it appears to only take effect at 2045 on the dot?

We can now use sequences. The following turns a light on for a period of time.

alias: PIR Turn Turn Light On When Leaving In The Dark
triggers:
  - entity_id:
      - binary_sensor.pir1_motion
    from: "off"
    to: "on"
    trigger: state
conditions:
  - condition: sun
    before: sunrise
    after: sunset
actions:
  - sequence:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.plug2
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.plug2
mode: single