Automation Problem - light fails to turn off 3hrs after sunset

I have set up automation to turn my lights on at sunset and and off 3 hours later.

The on works fine but off trigger is not working for some reason

id: '1707087009419'
alias: Pergola Lights Off
description: If they are on after sunset then turn them off 3 hours later
trigger:
  - platform: device
    type: turned_on
    device_id: b693a2083fdad3599bac19658a82d9c1
    entity_id: c419333733866ae4df2d3e7c3e511bb6
    domain: light
condition:
  - condition: sun
    after: sunset
    after_offset: '03:00:00'
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      area_id: garden
      device_id: b693a2083fdad3599bac19658a82d9c1
      entity_id: light.pergola_strip
mode: single

Looking at the trace I am seeing that it confirms that it’s 3 hours after sunset, but nothing happens after that until I manually turn the lights off many hours later.

thanks.

That is not what your automation does…

As written it is more like:

"When the light is turned on 3 or more hours after sunset, turn it off.

Again, that is not what it says. Conditions are not Waits, they are checked immediately after the trigger event. The condition is testing if it is at least 3 hours after sunset. As the next line states, the condition failed and the script stopped without executing the action.

You should trigger on the Sun instead.

id: '1707087009419'
alias: Pergola Lights Off
description: 3 hours after sunset turn off the pergola lights
trigger:
  - platform: sun
    event: sunset
    offset: "03:00:00"
condition: []
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.pergola_strip
mode: single
1 Like

got it thanks… makes sense when you explain that way.

My on action is based on the sun but I did not think to do the same for the off action.

Worked perfectly last night thanks @Didgeridrew

1 Like