Copy switch using Template

Hi all,

I am trying to copy a switch’s on-off state to another switch using Template. I know I can create two automations but trying to see if I can get this done using templates. I have the following code:

switch:

  - 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

The state of the template switch updates based on the source switch but the target switch is not operating. I also tried with having “target:” instead of “data:” but still its not working.

What am I doing wrong?

TIA

according to the documentation, it is target

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

So in your case,

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

EDIT: On the other hand, the source is not from the switch domain but a light entity, don’t know if it is important …

P.S.: You only need one automation and use the trigger variable to do the same.

Nope, data is correct, target is also correct.

Template switches will not do what you want it to do. You’ll need an automation to sync the switches together. I suggest looking in blueprints for an automation that already does this.

Here is an automation that syncs to lights

Every day is an opportunity to learn.

What is the purpose of that section?

I initially believed that the goal was to make a simulated switch that triggers turn_on and turn_off actions when activated within HA. Am I mistaken? However, I do understand that since the state is determined by another entity, it may not be logical to use turn_off/turn_on on this virtual switch since it will not affect the source switch and could lead to ambiguity in the status.

EDIT I’m trying to understand a use case for the copy switch

He wants to sync the state between 2 physical switches. He’s expecting a virtual switch to automatically update the physical switches state.

Thank you for the reply. The template copy switch in the documentation is very confusing, at least to me. I followed this discussion where another user was confused as me, but even after reading it, I couldn’t understand the purpose of the template copy switch example in the documentation. I know the template “virtual” switch follows the state of the source entity. What I can’t understand is why the target entity! It seems the target entity is not serving any purpose here. :persevere:

it creates a copy of the entity you point it to. It does not copy the state of that device to another device.

But, even if the target entity is the same switch on and off, it should still work, isn’t it?
In my example above the “copy” template (that’s the name of the switch) switch is turned on when the ceiling light turns on and it turns off when the ceiling light turns off.

image

image

Thanks. Can you kindly clarify the use of the “turn on” and “turn off” sections, and the impact on the target entity defined under “entity_id” of those sections?

TIA

The turn_on section is a set of actions that are performed when you turn the virtual switch on via the UI or the switch.turn_on service. The turn_off section is a set of actions that are performed when you turn the virtual switch off via the UI or the switch.turn_off service.

Thanks. So, if I understand correctly, if I define a service call “switch.turn_on” under the turn_on section and provide a target entity, the target switch should get a service call for turning itself on, or am I off base here?

Replying myself :slight_smile:
If you turn on the template switch via UI, the target switch turns on and probably, as @petro said, via a service call. But the target switch does not turn on if it gets turned on via the source entity’s state. Because the template switch is tied to the source entity’s state, it immediately turns off when you turn it on via UI while the source entity is off without changing the state of the target entity.

IMO, the example in the template seems to be a lousy use case of template. I guess templates are helpful when you want to turn a non-switch entity into a switch.

Thanks all for the replies.

The example is if you want to create a copy of your entity. I think what you’re not understand is the word entity. The entity is a representation of your switch. All you’re doing is creating a copy of the representation. It’s not copying the behavior from one device to another. That’s what automations are for.

Thanks a lot. Unfortunately I still can’t understand the use of it. My apologies. I created an automation, and it gets it done. Thanks for helping.

I understand I get a copy of another switch. I know an entity is a representation of a device. But you can apply a service call to an entity. So, if I define a service call to turn on an entity, it should turn on the device.

It works when turned on via the UI but not via the “copy” function of the state of the source entity. That’s confusing.

Also, the example doesn’t say “copy entity” but “copy switch.”

Anyway, thanks a lot.

Yes, I think you’re still miss understanding what a switch is in home assistant. In the case of the example, it’s copying the switch.source, which is the entity switch.source. You’re assuming it means the hardware switch, if that was the case, it would say “device” or reference the hardware itself.

Home assistant refers to hardware as devices. A switch device in home assistant will have multiple entities associated with it, one of them being a switch entity. For example, I have a bunch of zwave switches, they all have power monitoring, pinging abilities, and other entities in addition to the switch entity.

Thanks. That makes sense. The template switch copies the source entity’s state correctly and turns itself on and off based on the state of the source entity.
What I can’t get is why the target entity is not behaving as intended when the template switch’s state changes based on the source entity’s state. It does when I change the state of the template switch using UI. What’s the difference between the template switch turning on based on the source entity’s state vs. UI or service call?

Because it’s not an automation. When the template switch changes state, it’s just showing the state change. No actions are performed. The turn_on/turn_off actions are only performed when you Click the switch in the UI or call the turn_on/turn_off services directly.

Yes, because you’re using the virtual switch, turning it on or off.

This is a fundamental miss understanding on your part. There is no ‘turning on’ action when the switches state changes. The virtual switch’s state changes based on the template provided to the template switch.

For example, if I made a template switch that is always on between 1 and 2 am, the virtual switch would only be on between 1 and 2 AM. If I turned off the switch at 1:30 AM, the virtual switch would not change it’s state to off but the turn_off actions would run… because your template told it to stay on between 1 and 2 AM. When the state changes from on to off at 2AM, the turn_off actions are not executed. Turn_on and turn_off actions are only executed when the turn_on or turn_off service calls are performed. This is done through automations or user interaction on the UI.

Got it. Seems to be clear now!

One addition. When the virtual switch’s (template switch) state changes due to the source entity, it IS a turn ON. Because I just created an automation based on it. So, when the ceiling light turns on, the virtual switch turns on triggering an automation to turn on the sonoff switch.

Anyway, I think the example of “copy switch” is misleading, at least for anybody who hasn’t used templates before.

Thanks again!