Control two lights with one button

How can I create this automation.
I have a switch when clicked:
If LightA=on
then LightA=off, LightB=on
else
LightA=on, LightB=off

And I cant use toggle because all lights can manually be controlled as well

I’m not 100% clear on what you want.

You want a smart switch to toggle the states of A and B such that, if A is on and you push the button, A turns off and B turns on.

This would also include A and B both on. In this case, only A would turn off and B would remain on.

Otherwise, you want it to turn on A and turn off B. So if both lights are off, it will only turn on A. If only B is on, it will turn off B and turn on A.

- alias: Toggle Lights
  trigger:
    platform: state
    entity_id: switch.my_switch
  action:
    # Need to do 2 actions. One for A and one for B. 
    # First, turn on or off A depending on the state of A. 
    # This might turn A off if it was already off. Oh well, nothing bad will happen.
    # We can't have an empty service, so we have to call SOMETHING no matter what. 
    # So turn the light off if it's off it is.
    - service_template: "light.turn_{{ 'off' if is_state('light.lightA', 'on') else 'on' }}"
      entity_id: light.lightA
    # Now control light B. It strictly follows light A (which has already been handled).
    # Thus, if light A is off, this must turn on. 
    # Otherwise, if light A is on, this must turn off. 
    - service_template: "light.turn_{{ 'on' if is_state('light.lightA', 'off') else 'on' }}"
      entity_id: light.lightB

That automation will do exactly as you asked.

But, to control 2 lights with one button…that could mean something else.

Specially, scroll down to the examples. You can have one switch control 2 lights, copycat, mirror, etc.

3 Likes

Thanks that was exactly what I wanted. Just one more question. If I want to use the same type of template to cycle through brightnesslevels on a bulb so that every click on a switch changes the brightness like 0 → 100 → 255 → 0… how chould I do that?

Sure. Setting a brightness of ‘0’ is the same as turning the light off, so we don’t have to template the service call. It will just be light.turn_on always.

I could just do a “if 0, set to 100, if 100, set to 255, if 255 set to 0” thing, but that isn’t flexible.

    - service: light.turn_on
      data_template:
        entity_id: light.lightA
        # Get the current brightness of the light and set the next value. 
        brightness: >
          {% set cb = state_attr('light.lightA', 'brightness') | int %}
          {% if cb < 100 %} 100
          {% elif cb < 255 %} 255
          {% else %} 0
          {% endif %}
      

I chose “<” in the event someone changed the light value manually. But keep in mind, if they set it to, say 200, it will step to the next closest 255. So it will have an extra step sometimes if a manual brightness was set.

To make it more flexible, I would create a script where you can pass in the step size you want to increment. This would work for all of your lights with their own custom step size.

# Expects data fields {{ light }} and {{ step }}
light_increment:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "{{ light }}"
        # Add the passed in step size to the light brightness. 
        # Few rules:
        #  1) If at max brightness (255), the next step will go to 0.
        #  2) If the next step is close to max, it is rounded to max. Not sure how to define
        #     'close'. I'll just say anything > 200 is rouned to 255? Should probably
        #      make it a function of step size honestly...
        brightness: >
          {% set cb = state_attr(light, 'brightness') | int %}
          {% set new_brightness = cb + (step | int) + 1 %}
          {% if cb == 255 %} 
            0
          {% elif new_brightness >= 200 %}
            255
          {% else %} 
            {{ new_brightness }}
          {% endif %}

It will handle all wrap around. So if you set a step of 500, it will just be on/off. Also no need to worry about the current brightness of the light, the script will handle it.

Now just call this with any step size you want. For example:

service: script.light_increment
data:
  light: light.lightA
  step: 100

Or in an automation with that switch:

- alias: switch A brightness
  trigger:
    platform: state
    entity_id: switch.switchA
  action:
    service: script.light_increment
    data:
      light: light.lightA
      step: 100

I use a side light for watching TV and a main light at other times. I have one coffee table button to switch between one or the other or have both lights on at once.

  1. Press once - side (secondary) light goes on - main light goes on
  2. Press again - side light goes off - main light stays on
  3. Press again - side light goes on - main light goes off
  4. Press again - side light goes off - main light stays off
  5. Press again - sequence above repeats

It’s done by linking the button to each light in a different way - and adding a few seconds delay to one of those ways so that the button is unresponsive for that time. (I shy away from code in curly brackets)

I’ve written it up using an RF button as a switch - but I hope there’s a clue in here for other buttons.

Roger Frost’s home automation
https://www.rogerfrost.com/use-one-switch-to-turn-on-two-lights-in-any-combination/

just make an automation that one button is pressed that they all switch on or off. That way you don’t need all these complex configurations. Basically switch on and off one button and make an automation that if this button turns on or off that the rest does that too.