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:
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.
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.
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
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.
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.
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?
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
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.
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.”
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.
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.