Turn on lights when I come home after dark

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
  - platform: state
      - device_tracker.iphone_ian_2
      - device_tracker.wok_2
    to: home
  - condition: sun
    after: sunset
    enabled: true
    before: sunrise
  - condition: device
    type: is_off
    device_id: 838c487b0380cd5fc80542d5da2ee686
    entity_id: switch.lumieres_ext_entree
    domain: switch
  - 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).

The last trigger ended with this:

Executed: July 16, 2022, 1:02:15 AM

wanted_time_before: ‘2022-07-16T09:07:35.851989+00:00’ wanted_time_after: ‘2022-07-17T00:33:47.220128+00:00’ result: false

As you can see from the trace log, we’re well after sunset, and well before sunrise…

I’m not sure why it’s not working.

Use https://github.com/pnbruckner/ha-sun2 (with hacs) instead of the build in sun component and it’ll work.

Try changing this:

To this:

   -  condition: state
      entity_id: sun.sun
      state: below_horizon

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

Nothing to do with sunrise just time based.

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
              - condition: sun
                after: sunset
                after_offset: '0:15:00'
              - condition: sun
                before: sunrise
                before_offset: '-0:30:00'

That did the trick, thank you.

Glad it sorted it for you. Please mark my post as the solution to help others find it faster.

Other quick question, I want another automation to run 15 minutes after that one has triggered, how do I go about this?

You could add a call to trigger your next automation to the action of your existing automation:

  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: automation.trigger
    data: {}
      entity_id: automation.your_called_automation

I did just that right before you answered (I kept at it in case I could figure it out on my own), it works!

1 Like