Two lights automation – IF/ELSE problem

Hi,
I have a hallway with two lights, one for day and other for night.
I have two automations that watch for either of the lights to turn ON.
Conditions determine what time of day it is, maybe look at the outside illumination levels and decide which light of the two to turn on.

My problem is determining in the action part if the correct light is already ON.
If so, I would turn both OFF, else I would turn ON just the right one.

I could split this into four automations and use conditions to determine this, but why if it can and should be determined in a simple IF ELSE statement in the action part.

This is the scheme:

Hallway Lights – Night

Trigger: Hallway OFF=>ON
Conditions: TIME 22:00–6:00 OR (TIME 15:00–22:00 AND Ilumination<500)
Action: IF (Hallway_Night==ON) THEN (Hallway=>OFF AND Hallway_Night=>OFF)
        ELSE (Hallway=>OFF AND Hallway_Night=>ON)


Hallway Lights – Day

Trigger: Hallway_Night OFF=>ON
Conditions: TIME 6:00–15:00 OR (TIME 15:00–6:00 AND Ilumination>500)
Action: IF (Hallway==ON) THEN (Hallway=>OFF AND Hallway_Night=>OFF)
        ELSE (Hallway=>ON AND Hallway_Night=>OFF)

And this is one of the automations working except I cannot figure out the action part with IF ELSE.
Note the action part of the code is just my intention – not an actual code I am trying.

- id: '1581701805691'
  alias: Hallway lights – night
  description: Use the night hallway light at night automatically
  trigger:
  - entity_id: light.hallway
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - after: '22:00'
      before: '6:00'
      condition: time
    - condition: and
      conditions:
      - after: '15:00'
        before: '22:00'
        condition: time
      - below: '500'
        condition: numeric_state
        entity_id: sensor.outside_ambient_light
  action:

IF ( light.hallway_night == ON ) { light.hallway => OFF, light.hallway_night => OFF  }
ELSE { light.hallway => OFF, light.hallway_night => ON  }

It looks like you need to convert this into actions, right?

First of all, are you sure it’s correct? Your else clause implies Hallway is OFF so there is no need to turn it off again.
And I can see inconsistency in your scheme (copy/paste?) and the action section of the code you provided - could you check it? I have a feeling that you didn’t formalise your goal well…

So we have 2 cases:

  1. Hallway ==ON:
    you need to call service: light.turn_off with entity_id: Hallway, Hallway_Night
  2. Hallway ==OFF:
    you need to call service: light.turn_on with entity_id: Hallway_Night

It can be done like this

action:
  service_template: light.turn_{{ 'off' if is_state('light.hallway', 'on') else 'on' }}
  data:
    entity_id:
      - light.hallway
      - light.hallway_night

For simplicity I decided not to exclude light.hallway from else clause (as it won’t do any harm to switch it off when it’s already off).
You can find more about service_template here and about templating in general here.

Sorry, there is indeed a typo in my first action scheme.
The IF should be asking about the other light, not the one that actually triggered the automation.
So in the first case, it should have been Hallway_Night.

Hallway Lights – Night

Trigger: Hallway OFF=>ON
Conditions: TIME 22:00–6:00 OR (TIME 15:00–22:00 AND Ilumination<500)
Action: IF (Hallway_Night==ON) THEN (Hallway=>OFF AND Hallway_Night=>OFF)
        ELSE (Hallway=>OFF AND Hallway_Night=>ON)

Maybe bit of HW background.
Both lights have my custom ESP modules behind old wall plates running ESPhome.
They are programmed to turn on a rele when toggled.
I want this functionality to be kept because if I have a Home Assistant problem, they are independent and still work.
But with HA running, I can look for their state change and decide based on time which light to actually turn ON. If the one that triggered the automation isn’t supposed to be ON, it gets turned OFF instantaneously.

I am turning two normal-switches into two-way switches and while doing so I also decide which of the two lights gets to be ON. So the two lights are newer on at the same time, but both switches control both light as if there is just one.

The switches are on opposite ends of the hallway.

Your code helped me to accomplish this partially.
I can turn both lights OFF if the IF light is ON.
But how do I turn one ON and the other OFF? Not both lights ON.

Light 1 triggers the automation by turning ON.
Light 2 is OFF but it is his time to be ON.
So my ELSE should turn the triggering Light 1 back OFF and instead turn ON Light 2.

- id: '1581701805691'
  alias: Hallway lights – night
  description: Use the night hallway light at night automatically
  trigger:
  - entity_id: light.hallway
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - after: '22:00'
      before: '6:00'
      condition: time
    - condition: and
      conditions:
      - after: '15:00'
        before: '22:00'
        condition: time
      - below: '500'
        condition: numeric_state
        entity_id: sensor.outside_ambient_light
  action:
  - data:
      entity_id:
      - light.hallway
      - light.hallway_night
    service_template: light.turn_{{ 'off' if is_state('light.hallway_night', 'on')
      else 'on' }}

I got it!
Since the light that triggers the automation always ends up being turned off I could put it after the IF action. Your suggestion to template the turn_on/off made it possible! Thank you!
I have an automation for the day switch and for the night switch, so I can do this.

Night automation triggered by the Day side switch:

- id: '1581701805691'
  alias: Hallway lights – Night
  description: Use the night hallway light at night automatically
  trigger:
  - entity_id: light.hallway
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - after: '22:00'
      before: '6:00'
      condition: time
    - condition: and
      conditions:
      - after: '15:00'
        before: '22:00'
        condition: time
      - below: '500'
        condition: numeric_state
        entity_id: sensor.outside_ambient_light
  action:
  - data:
      entity_id: light.hallway_night
    service_template: light.turn_{{ 'off' if is_state('light.hallway_night', 'on')
      else 'on' }}
  - device_id: dfa11b68e6cb458f952c201699706f4a
    domain: light
    entity_id: light.hallway
    type: turn_off

Day automation triggered by the Night side switch:

- id: '1581772775706'
  alias: Hallway lights – Day
  description: Use the day hallway light during day automatically
  trigger:
  - entity_id: light.hallway_night
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - after: '6:00'
      before: '15:00'
      condition: time
    - condition: and
      conditions:
      - after: '15:00'
        before: '6:00'
        condition: time
      - above: '500'
        condition: numeric_state
        entity_id: sensor.outside_ambient_light
  action:
  - data:
      entity_id: light.hallway
    service_template: light.turn_{{ 'off' if is_state('light.hallway', 'on') else
      'on' }}
  - device_id: 2b00f175a02f43169a9160d1f3a2c686
    domain: light
    entity_id: light.hallway_night
    type: turn_off

Now, the more interesting question is:

Can I check if any of the lights has been turned on/off/on in quick succession and based on this choose the other light source?

Meaning that by flipping any of the switches twice, I can turn on Day light at Night and vice versa?

Glad that your automation works now. It looks good!
Few comments:

  • you can use tod instead of hard-coded times
  • you can create a binary sensor like bright_enough_outside and use it in your conditions instead of condition: numeric_state - again, having more flexibility as 500 value will be in one place and if you decide to change it (or make configurable from UI, why not?), it should be pretty easy (and less prone to bugs).
  • you can create a third automation (which reacts to an event and turns off given light(s)) and fire events from your 2 existing automations passing appropriate data in event_data.
    It all would make it little bit easier to maintain (but would require some time and knowledge).
    Let me know if you need more details/help.

A wild guess - maybe esphome’s on_multi_click might help?
Sorry, don’t have enough info about your idea and/or have a head full of other stuff… :\