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.
- 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…
- 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.
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.
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
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).
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.