Automation Help - Lights on if home + off/on if leave/return

I’ve just about got there with Home Assist but I’m having problems understanding how to do some particular automation.

Basically I want my lounge lights to switch on between sunset and 11pm but only if I’m home. If I leave during that time they switch off, and if I return during that time they switch back on.

I appreciate this will prob require multiple automations and might be better via a script (?) but can anyone outline how best to do this?

Thanks in advance.

Hi @nickp, Welcome to HA, take a look at the automation examples.
It’s a good starting point.

Hey thanks - I did but still got a bit confused!

I originally had something like this (from the examples):

   trigger:
      - platform: sun
        event: sunset
      - platform: state
        entity_id: group.all_devices
        state: 'home'

But it seemed to act as an OR trigger instead of an AND trigger. So I then kept the sunset trigger but moved the detection of whether I was at home to a condition. Do I need device location in both trigger and condition?

Secondly, are the conditions in the example AND or OR? If AND then I’m guessing I could just amend the example to remove the “after” time and add a third condition template that checks the sun is below the horizon. But what would happen then if I arrived home at 2am? Surely it would still trigger (before 11pm and sun down!)

The triggers are always ‘or’.
Don’t know what`s default for conditions, but you can control to be OR or AND.

Edit: And please read the blue box on every page here. :slight_smile:

Hmmm… so I was hoping I’d fixed it but nope! Here’s what I’ve got:

- alias: 'Lounge lamp on at sunset'
  trigger:
    - platform: sun
      event: sunset
      offset: '00:00:05'
    - platform: state
      entity_id: device_tracker.nickp
      state: home
  
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.nickp
        state: home
      - condition: time
        after: '15:00:00'
        before: '23:00:00'
      - condition: sun
        after: sunset
      
  action:
    service: switch.turn_on
    entity_id: switch.lounge_lamp

I have a small offset in the trigger as there’s another light switched on at sunset (regardless of whether I’m home) and didn’t want both to fire at once. Unfortunately, whilst the other light came on just fine, the lounge one above did nothing despite me being home! Not sure what’s broken!

You’re doing too much in your trigger; your trigger should just be that the sun has set. It doesn’t matter if the automations fire at the same time or not as they won’t impact one another
trigger: 15 mins before sunset
condition: your tracker state is home and the light is off
action: turn the light on

In your condition you don’t need the time or the sunset, just that you’re home. You’ll need to add another automation to turn off the light at 11PM

You’ll have to have another automation that’s triggered by you leaving or coming home. This is pseudo code
trigger: your tracker state changed from home to away
conditions: after sunset, before 2300 hrs, light is on
action: turn the light off

trigger: your tracker state changed from away to home
conditions: after sunset, before 2300, light is off
action: turn the light on

trigger: 2300 hrs
conditions: light is on, you’re at home
action: turn the light off

Thanks quad - however the multiple triggers is exactly what’s shown in the automation examples that VDRainer linked to above and he suggested the triggers were OR.

I was trying to avoid having multiple different automations but guess that’s what its going to take!

If you want to cram it all in one you’d have to do something like this

trigger:
  - platform: sun
    event: sunset
    offset: '00:00:05'
  - platform: state
    entity_id: device_tracker.nickp
    from: 'not_home'
    to: 'home'
condition:
    condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.nickp
        state: home
      - condition: sun
        after: sunset
        after_offset: '00:00:05'
      - condition: time
        before: '23:00:00'
      - condition: state
        entity_id: switch.lounge_lamp
        state: off

That should do what you want, I think. The formatting might be slightly off since I’m typing it on the fly. You’ll have to have at least one more automation to turn the stuff off.

The triggers require that the sun has set and 5 minutes have passed OR that you weren’t home and have since come home.
The conditions require that you’re home, it’s at least 5 minutes after the sun has set but before 2300 hrs and that the light is currently off.

Just an update but the above “cram it all in” trigger just never worked for me. Not sure if there’s an issue with multiple triggers like that.

I’ve ended up splitting it into multiple automations now and its working fine.

Thanks for the help anyway.

Glad you got it all working.