How to Create a Virtual Toggle Switch

I want to create a Virtual Toogle Switch. I want to use this switch to control 3 different switches. I also want this virtual switch status to show turned on if any one of the three switches are turned on. I know that I can create an Automation to turn on/off these three switches, but using automation doesn’t indicate the current state of the switches. Also I want this button to look like a toggle switch. Is there any simple way to do this. I created a Toggle by going to the Confioguration > Automations & Scenes > Helpers > Add Helper > Toogle. But I know understand how I can map these 3 switches to this toggle. And will this toggle be able to show ON state if any one of these 3 switches are turned on?

Currently, when we create an Entities Card, it creates a Toggle Switch besides the name of the Entities Card. This toggle switch remains off if all the entities within this card is off & it turns on if any one of the entity within this card is on. And this toggle can also control the state of all the entities within the card i.e. if we turn on this toggle all the entities within this card turn on & if we turn off this toggle it turns off all the entities within this card. So I want to create a switch that can do the above. Currently I can do this by creating the Entities Card, but I want to know if there is another way to create this type of switch.

I used the below code,

switch:
  - platform: template
    switches:
      living_room_1:
        value_template: "{{ is_state('switch.sonoff_xxxxxxxxxx_x', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.sonoff_xxxxxxxxxx_x
            entity_id: switch.sonoff_xxxxxxxxxx_x
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.sonoff_xxxxxxxxxx_x
            entity_id: switch.sonoff_xxxxxxxxxx_x

But it only works when I add only one entity. When I try to add a second entity to this template, its shows the below error

duplicated mapping key at line 134, column 13:
                entity_id: switch.sonoff_xxxxxxx ... 
                ^

Also, is there any GUI way to acheive this? i.e. without editting the configuration YAML file. I managed to create a Toggle Switch from Configuration > Automations & Scenes > Helpers > Toggle. How can I map my entities to this? I mean I can create automations & map them to this toggle switch, but it won’t update the status of my entities.

target:
  entity_id: 
    - switch.sonoff_xxxxxxxxxx_x
    - switch.sonoff_xxxxxxxxxx_x

no

1 Like

Yes, that worked. Thanks.

I have mapped three entites to this template, can you please tell me how I can add multiple entities to the below line,
value_template: "{{ is_state('switch.sonoff_xxxxxxxxxx_x', 'on') }}"

it depends on how you want the state to show up.

if you want the template switch to be ‘on’ only if all entities are ‘on’:

value_template: "{{ is_state('switch.sonoff_xxxxxxxxxx_1', 'on') and is_state('switch.sonoff_xxxxxxxxxx_2', 'on') and is_state('switch.sonoff_xxxxxxxxxx_3', 'on') }}"

if you want the template switch to be ‘on’ if any entity is ‘on’:

value_template: "{{ is_state('switch.sonoff_xxxxxxxxxx_1', 'on') or is_state('switch.sonoff_xxxxxxxxxx_2', 'on') or is_state('switch.sonoff_xxxxxxxxxx_3', 'on') }}"
1 Like

Yes, that worked. Thanks.

1 Like

Hi,

I am trying to do the same here. Here is my code:

  - platform: template
    switches:
      copy:
        value_template: "{{ is_state('light.guest_room_ceiling_light', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_plug_1
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.sonoff_plug_1

But the target switch is not turning on based on the source entity state. The state of the virtual switch is turning on and off based on the source entity. What am I doing wrong?

TIA