I have a template switch, based on the examples in the docs, but it’s not firing the turn_on or turn_off services. I can see that the template has changed state from the logbook and history, but the lights connected to the template switch never get turned on:
I’ll give that a go. The documentation doesn’t mention anything about services that are/aren’t available, the example just use switch and cover. I do have some other template switches that use switch.turn_on that aren’t working at the moment, I have commented those out while I debug this switch.
homeassistant.turn_on isn’t working either. I can see the physical switch and the template switch turning on and off fine, it’s just the service that’s failing to be called. Can I see an event list of service calls somewhere along with the result of the calls? That would make debugging this (and many other Home Assistant features wit poor documentation) a lot easier.
Just tried with the light template, still no event being fired (that I can tell). The template switch/light that I’m trying to create is just a middle man, so I don’t think it should be a light in it’s own right anyway. All I want to do is sync a physical switch with a physical light, without resorting to automations (I will need to make one to turn the light on and one to turn the light off, which seems clunky).
As far as I can tell from reading the docs the template switch should work for what I want to do (as I say, I pretty much copy/pasted one of the examples), but I don’t see the turn_off and turn_on services being called when the physical switch is toggled. Can I debug this somehow?
Well that’s odd. I can toggle the template switch and it will turn my lights on/off, but after a moment the template switch will set itself back to whatever state the physical switch is. Toggling the physical switch still doesn’t do anything to the physical lights, so I’m starting to suspect that there’s something wrong with how the template is being evaluated. Absolutely no idea how I debug that though. Though even saying that, the template switch is evaluating correctly, so it seems to be an issue with the template switch realising that the value has changed and calling the appropriate service (turn_on/turn_off).
Oh, that’s exactly what I wanted to use it for. Why does it not call the services? I suppose I’m going to have to write two automations to control turning the lights on and off then?
Hang on, that doesn’t make sense to me. The template switch is evaluating the state of my physical switch in the hopes of controlling a light. @francisp were you referring to my manual toggling in the UI? I couldn’t care less about being able to do that, I only care about being able to flick a switch on my wall to turn a set of lamps on.
Oh I get it. switch.living_room_soft_lights is a physical switch and you’re trying to use this switch to toggle these specific lights when that one does.
Yea unfortunately this isn’t really going to work the way you want it to here. When you toggle the physical switch it will change the state of this one but as francis said, it’s not going to call the services in that case, it will only do the state change. And if you toggle this switch then your services will be called but next time HA checks state its going to reset it back to the state of the physical switch (which hasn’t changed).
Think you’d be better off just building an automation around switch.living_room_soft_lights. Try this:
automation:
- id: toggle_living_room_soft_lights
alias: Toggle living room soft lights
description: Toggle the living room soft lights when the switch changes
trigger:
- entity_id: switch.living_room_soft_lights
platform: state
condition: []
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
data:
entity_id: light.living_room_lights
Then whenever the switch is toggled the lights will turn on/off
Also I assume since the physical switch is represented by a switch entity in HA then you can toggle the state of that switch from HA as well so you can just show switch.living_room_soft_lights in your UI if you want.
If for some reason HA can’t toggle that switch then you can use an input_boolean as a makeshift virtual switch and add another trigger for when that input_boolean toggles on or off. They’ll get out of sync though if you do that so hopefully you can just toggle the physical switch from HA as well.
The ‘unsupported action’ message is just letting you know that you can’t switch back to “Edit in UI” for that action. service_template is only supported in YAML so you have to edit that action in YAML. But otherwise yea the automation can be set up and edited via the UI.
That’s disappointing. It seems way more complicated than it should be to sync two entities in Home Assistant. I’ve now got an automation to keep things in check but it will frequently go out of sync if the lights are turned off individually. The light that the switch now controls is a group of 5 lights, each of which get turned on and off independently. This means that we could turn them all off individually and the switch would remain on. Using the physical switch while require two presses to turn them all back on again, which is a bit odd.
I feel like there should be a sync option somewhere that keeps two or more entities in the same state. That’s what I had assumed the template switch would be able to do.
To clarify what’s happening. When you start up, your device has a state of unknown. Because that’s the case, it doesn’t know which action to fire; turn on or turn off.
The turn on / off service is based on the state of itself. So if it never has a proper state it can never call the correct service.
Having the switch reference the state of the light will cause it to work. This is ultimately what @francisp was getting around to.