I have a TP-Link Casa switch running my outdoor lights and I have it setup in the native app to turn on at sunset and turn off at 23:15.
I wanted home assistant to turn it back on if I get home after 23:15, but before sunrise.
I setup the following automation, that seems to fail at the after sunset, before sunrise condition
alias: Welcome home
description: When we come home at night and outside lights are off, turn them on
trigger:
- platform: state
entity_id:
- device_tracker.iphone_ian_2
- device_tracker.wok_2
to: home
condition:
- condition: sun
after: sunset
enabled: true
before: sunrise
- condition: device
type: is_off
device_id: 838c487b0380cd5fc80542d5da2ee686
entity_id: switch.lumieres_ext_entree
domain: switch
action:
- type: turn_on
device_id: 838c487b0380cd5fc80542d5da2ee686
entity_id: switch.lumieres_ext_entree
domain: switch
mode: single
I also added the condition that if it was already on, I did not want it to turn on again (not sure this is needed).
Just curious, why do you want to trigger before sunrise?
Could it just be as simple as when your devices are marked at home (or approaching home) after 23:15 and before lets say 6 in the moning?
Triggers: device_tracker.iphone_ian_2 and device_tracker.wok_2 enters home Conditions: Time, after 23:15 and before 6 in the morning. Second Condition: switch.lumieres_ext_entree is off Action: switch.lumieres_ext_entree turn_on
I think it’s to do with crossing midnight (sunrise treated as same day, not next day).
rossk’s solution is nice and clean - I didn’t even think of using that.
I got around it using below with offsets to allow for dusk and dawn light:
- condition: or
conditions:
- condition: sun
after: sunset
after_offset: '0:15:00'
- condition: sun
before: sunrise
before_offset: '-0:30:00'