HA Combine 2 Lights Into 1

Hi All,

Does anyone know if its possible to combine 2 lights into 1 in Home Assistant.

I have several rooms with 2 non dimmable switches/lights. For example:

Kitchen 1
Kitchen 2
Dining Room 1
Dining Room 2
Sunroom 1
Sunroom 2

In HA to reduce the clutter, I would like to have a single light switch with a 4 position switch a bit like a simple dimmer, So for each room:

Switch Position - Light 1 - Light 2
1------------------------off---------off
2------------------------on-------- off
3------------------------off---------on
4----------------------- on---------on

I think the light template might be able to do it, but I can’t figure out exactly how do the template configuration bit.

Hope someone can help, thanks.

Use groups. i.e. group.kitchen_lights. Then you can simply turn the group on/off.

1 Like

I think the simplest solution would be to have input_number variable created with values limited to 1 to 4 with step 1. Then you can link it in interface to slider to choose number corresponding with desired light configuration. The next step is to have 4 scripts created to switch on/off appropriate bulbs triggered by condition of input_number changing to.

Here’s one possible way of achieving your goal.

Create an input_select with four options.

#input_select:
  kitchen_lights:
    options:
      - 'Off'
      - Left
      - Right
      - All

Then create a single automation that is triggered by the input_select’s state-changes.

#automation:
- alias: 'Kitchen Lights'
  trigger:
    platform: state
    entity_id: input_select.kitchen_lights
  action:
    service_template: "light.turn_{{ 'off' if trigger.to_state_state == 'Off' else 'on' }}"
    data_template:
      entity_id: >
        {% if trigger.to_state.state in ['All', 'Off'] %}
          light.kitchen_1, light.kitchen_2
        {% else %}
          {{ 'light.kitchen_1' if trigger.to_state.state == 'Left' else 'light.kitchen_2' }}
        {% endif %}

EDIT
Modification. Reduced the template’s size.

2 Likes

Thanks for the replies guys! I used a combination of mirekmals and 123s ideas & code.

This is what I have and it almost works:

input_number:
  sunroom_light:
    name: Sunroom Light
    initial: 0
    min: 0
    max: 3
    step: 1
- alias: 'Sunroom Lights TEST'
  trigger:
    platform: state
    entity_id: input_number.sunroom_light
  action:
    service_template: "light.turn_{{ 'off' if trigger.to_state.state | int == 0 | int else 'on' }}"
    data_template:
      entity_id: >
        {% if trigger.to_state.state | int in [3 | int, 0 | int] %}
          light.shelly_shsw_1_943b0b, light.shelly_shsw_1_2470f2
        {% else %}
          {{ 'light.shelly_shsw_1_943b0b' if trigger.to_state.state | int == 1 else 'light.shelly_shsw_1_2470f2' }}
        {% endif %}

Just the slider positions 1 & 2 don’t work properly… I’m learning a lot from studying this code and I don’t think it can work properly for slide positions 1 & 2 without some changes.
I’ll keep studying it a for a few more hours…

Also I don’t think this slider will update when changing the physical switches.

Try this:

- alias: 'Sunroom Lights TEST'
  trigger:
    platform: state
    entity_id: input_number.sunroom_light
  action:
    service_template: "light.turn_{{ 'off' if trigger.to_state.state | int == 0 else 'on' }}"
    data_template:
      entity_id: >
        {% if trigger.to_state.state | int in [3, 0] %}
          light.shelly_shsw_1_943b0b, light.shelly_shsw_1_2470f2
        {% else %}
          {{ 'light.shelly_shsw_1_943b0b' if trigger.to_state.state | int == 1 else 'light.shelly_shsw_1_2470f2' }}
        {% endif %}

Keep in mind what fredkruger said, neither an input_select nor an input_slider will report the correct state if you directly change the state of the lights. So if one of the two lights is turned on manually (via a wall switch) or by another automation, the input_select (and input_number) will not be updated.

To achieve that, you have to create another automation, triggered by state-changes to the lights, to update the status of the input_select. However, it’s more complicated than that because what I just described creates a feedback loop.

This service_template works ok to turn both lights on/off (input position 0 & 3)
And it works ok to turn either light on if they are already off. (input position 1 & 2)
But if the lights are already on then the code can’t turn Light 1 or Light 2 off (for input positions 1 & 2)

Is it possible to have conditions to do this in the Service/Data template?

I’ve been looking at this a a long time and can’t figure it out.

Here’s another approach that uses Scenes.

First, create this automation. It simply turns on a scene corresponding to the input_select’s option. For example, if the input_select is set to Left, the automation will turn on scene.sunroom_left.

- alias: 'Sunroom Lights TEST'
  trigger:
    platform: state
    entity_id: input_number.sunroom_light
  action:
    service: scene.turn_on
    data_template:
      entity_id: "scene.sunroom_{{ trigger.to_state.state | lower }}"

Second, create four scenes. Each scene corresponds to one of the input_select’s options.

scene:
  - name: Sunroom Off
    entities:
      light.shelly_shsw_1_943b0b: off
      light.shelly_shsw_1_2470f2: off
  - name: Sunroom On
    entities:
      light.shelly_shsw_1_943b0b: on
      light.shelly_shsw_1_2470f2: on
  - name: Sunroom Left
    entities:
      light.shelly_shsw_1_943b0b: on
      light.shelly_shsw_1_2470f2: off
  - name: Sunroom Right
    entities:
      light.shelly_shsw_1_943b0b: off
      light.shelly_shsw_1_2470f2: on

Thanks 123 taking the time to help, but I found a simpler way to make it work based on your earlier suggestions. Here’s the code:

#automations.yaml
- alias: 'Sunroom Lights TEST'
  trigger:
    platform: state
    entity_id: input_number.sunroom_light
  action:
    - service_template: >
        {% if trigger.to_state.state | int in [1, 3] %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
      entity_id: light.shelly_shsw_1_943b0b
    - service_template: >
        {% if trigger.to_state.state | int in [2, 3] %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
      entity_id: light.shelly_shsw_1_2470f2

Nice work extending the concept. FWIW, here’s the streamlined version it:

#automations.yaml
- alias: 'Sunroom Lights TEST'
  trigger:
    platform: state
    entity_id: input_number.sunroom_light
  action:
    - service_template: "light.turn_{{ 'on' if trigger.to_state.state | int in [1, 3] else 'off' }}"
      entity_id: light.shelly_shsw_1_943b0b
    - service_template: "light.turn_{{ 'on' if trigger.to_state.state | int in [2, 3] else 'off' }}"
      entity_id: light.shelly_shsw_1_2470f2

NOTE
The sole advantage of the scene-based solution is the ability to specify brightness, color, etc when turning on a light.

If you try to do that with the examples that employ service_template, you run into the problem that if the service is light.turn_off, it will complain about the brightness, color etc options (because turn_off doesn’t accept options).

Thanks for the info, I’m learning a lot. I like streamlined!

I decided to do this now with Template Light. This way the dimmer slider also reflects the switch positions.

Here’s my code with a bit of your streamlining added:

- platform: template
  lights:
    sunroom_lightscomb:
      friendly_name: "Sunroom Lights Combined"
      value_template: "{{ states('light.sunroom_lights') }}"
      level_template: >-
        {% if is_state('light.shelly_shsw_1_943b0b', 'off') %}
          {% if is_state('light.shelly_shsw_1_2470f2', 'off') %}
            0
          {% else %}
            2
          {% endif %}
        {% else %}
          {% if is_state('light.shelly_shsw_1_2470f2', 'off') %}
            1
          {% else %}
            3
          {% endif %}
        {% endif %}

      turn_on:
          service: light.turn_on
          entity_id: light.sunroom_lights
      turn_off:
          service: light.turn_off
          entity_id: light.sunroom_lights
      set_level:
        - service_template: "light.turn_{{ 'on' if brightness | int in [1, 3] else 'off' }}"
          entity_id: light.shelly_shsw_1_943b0b
        - service_template: "light.turn_{{ 'on' if brightness | int in [2, 3] else 'off' }}"
          entity_id: light.shelly_shsw_1_2470f2

It does work but the only problem I have here is that the dimmer slider range is from 0-255?
I only want it to be 0-3. Any way to have the dimmer slider restricted to 0-3?
I could probably combine the input_number…

Only via a custom Lovelace card. That’s why it was suggested to use either an input_select or an input_number because you can restrict them to handle four states and there’s no need for a custom card.