Help with a complex light automation

Hi, I have 2 sets of lights that are controlled by a smart button. I want to have an automation that turns on both sets of lights if both were off and only one if the other one was already on. This I managed to achieve. However, I want the second press to turn off both if both were off before the first press and only one if the other one was on before the first click.
So 1st click - turn on both if both were off and only one if one was on.
2nd click - turn off both if both were off and only one if one was on.
Currently, I have this automation set up:

trigger:
  - device_id: 529829b086708d782f5fcdb65891863c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
condition: []
action:
  - if:
      - condition: state
        entity_id: light.hue_lightstrip_plus_1
        state: "on"
    then:
      - service: light.toggle
        data:
          brightness: 255
          transition: 1
        target:
          entity_id:
            - light.kuche_1
            - light.kuche_2
    else:
      - condition: sun
        before: sunset
        before_offset: "30"
      - service: light.toggle
        data:
          transition: 1
          brightness: 255
        target:
          entity_id: light.kitchen

It works fine at the first click but will only turn off one of the lights on the second click as the state was already “on”. I feel like I should use from_state somehow but I can’t figure out the logic.

My gut says I’d ‘record’ the state of the two lights in a scene at the beginning of the automation and then turn one or both on with the 1st click and then ‘restore’ that scene on the 2nd click - but I’m mostly commenting to see what solutions the pros will actually come up with :wink:

1 Like

I feel like maybe helpers, and specifically input booleans could help? There must be a bunch of ways to do what you want, but I believe that’s how I would do it.

Would this work? I probably would use a text helper.
Sorry for the horrible pseudocode formatting. (I’d rather type in javascript tbh :stuck_out_tongue: )

When button triggered:
- IF light1 OR light2 is off:
    - IF light1 is off AND light2 is on - SET text helper = "light1"
    - IF light1 is on AND light2 is off - SET text helper = "light2"
    - ELSE clear text helper
    - Turn light1 AND light2 on
- ELSE:
    - IF text helper == 'light1' - turn off light1
    - ELSE IF text helper == 'light2' - turn off light2
    - ELSE turn both lights off
    - clear text helper

Finally found the link I was looking for earlier to ‘Creating scenes on the fly’ - including an example:

Ah, I didn’t know that was possible. That can work! Thanks!

You can do this without anything ‘super fancy’, but it still requires templates.

trigger:
  - device_id: 529829b086708d782f5fcdb65891863c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
condition: []
variables:
  lights:
  - light.kuche_1
  - light.kuche_2
  lights_off: >
    {{ expand(lights) | selectattr('state','eq','off') | map(attribute='entity_id') | list }}
  turn_on: >
    {{ lights_off | length == lights | length or lights_off | length > 0 }}
action:
- service: light.turn_{{ iif(turn_on, 'on', 'off') }}
  target:
    entity_id: "{{ iif(turn_on, lights_off, lights) }}"

You can also make an oldschool group using all: true, and then just create a simple automation without templates.

group:
  kuche_lights:
    name: Kuche Lights
    all: true
    entities:
    - light.kuche_1
    - light.kuche_2
trigger:
  - device_id: 529829b086708d782f5fcdb65891863c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
condition: []
action:
- choose:
  - conditions:
    - condition: state
      entity_id: group.kuche_lights
      state: 'on'
    sequence:
    - service: homeassistant.turn_off
      target:
        entity_id: group.kuche_lights
  - conditions:
    - condition: state
      entity_id: group.kuche_lights
      state: 'off'
    sequence:
    - service: homeassistant.turn_on
      target:
        entity_id: group.kuche_lights

Thanks! That’s a bit too advanced templating for me to understand but I tried it and nothing happens when I press the button. Any variables I need to update?

nope, should just work out of the box

That’s what I figured. The automation triggers when the button is pressed but no lights turn on or off. That’s the step trace output:

params:
  domain: light
  service: turn_on
  service_data: {}
  target:
    entity_id: []
running_script: false
limit: 10

Same with the groups solution. Automation triggers but the choose action chooses nothing.

did you create the group? Are those lights actually lights, e.g. I just copied light.kuche_1 from your automation. Usually there will be errors in the logs if there are issues with the automations

I did create the group and yes, they are all lights. There’s nothing in the logs either.

and the trace for the automation shows it choosing nothing? What is the state of the lights and does your automation use the correct group name?

All correct. When I replace group.kuche_lights with light.kuche_lights the two lights turn on but not the light strip and on second press nothing happens.

your initial explanation said just the 2 lights. So you want 3 lights? If yes, just add the 3rd light to the group.

As for the second press, the group has to turn on, and the group will turn on when all entities report their state as on. If your lights take a minute to respond w/ a state, your second press wont work if you click it too fast. You can most likely adjust that by changing your transition time so the state reports fast instead of reporting when it gets to the full brightness. FYI using scenes won’t correct this either. This is hardware related. So you’d have to correct that by changing your transition time to 0 if your hardware supports that.

Thanks a lot for your help! And sorry if my initial explanation was confusing. It’s 2 lights but one of the lights has 2 bulbs. I was able to solve it with a helper and multiple nested if statements in the end. Here is the script:

trigger:
  - device_id: 529829b086708d782f5fcdb65891863c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: state
            entity_id: light.hue_lightstrip_plus_1
            state: "on"
          - condition: state
            entity_id: light.kuche_lights
            state: "off"
    then:
      - service: input_text.set_value
        data:
          value: light_on
        target:
          entity_id: input_text.button_lights_helper
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.kuche_lights
    else:
      - if:
          - condition: state
            entity_id: light.kitchen
            state: "off"
        then:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen
          - service: input_text.set_value
            data:
              value: light_off
            target:
              entity_id: input_text.button_lights_helper
        else:
          - if:
              - condition: and
                conditions:
                  - condition: state
                    entity_id: light.kitchen
                    state: "on"
                  - condition: state
                    entity_id: input_text.button_lights_helper
                    state: light_on
            then:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.kuche_lights
            else:
              - if:
                  - condition: and
                    conditions:
                      - condition: state
                        entity_id: light.kitchen
                        state: "on"
                      - condition: state
                        entity_id: input_text.button_lights_helper
                        state: light_off
                then:
                  - service: light.turn_off
                    data: {}
                    target:
                      entity_id: light.kitchen

Really appreciate everyone’s help! This thing drove me crazy :smiley: .

1 Like

Haha welcome to the home automation rabbit hole we’re in. :laughing: