Emulated Hue and Sleep Cycle app on a template switch to trigger morning routines as well as turning off the light when setting the app.
But I have an input_select that allows me to select the bedside light I’m using. (have toddler, so I regularly have to sleep in guest room).
But the value_template doesn’t reflect my actual light status, which is changed by input_select.
Current value_template:
{{ is_state('light.master_bedroom_1', 'on'}}
Input_select that allows me to select which light I’ll be using:
{{states.input_select.w_bed_light.state}}
This returns pre-populated entity ID, such as “light.bedroom_2_light_bulb”. I then use this in my morning routine script and turn_off command action the selected light.
Putting latter into the former doesn’t work This returns false:
Although you’ve answered my question, and in template tester, it works.
Unfortunately the template Switch does not reflect state of the light bulbs. I turn on or off the bulb, the switch remains showing Off. I change input_select, to on bulbs, the switch state remains off.
In a nutshell, if a Template Switch fails to report the correct state, it usually means its value_template is faulty. Post your Template Switch’s configuration so we can examine it.
(I have the automation toggle displayed on front end with a input time field for other purposes, instead of making another input boolean, I re-used on/off state of the automation for whether I want to start the kettle)
Just manually populated 3 possible bedside lights entity ID’s. The light.turn_on or _off works fine in this way. The 3 bulbs have never been powered off, so always available for state evaluation.
I think I’ve found a pattern.
If I restart HA while selected light is turned on, the switch is stuck in ON state.
If I restart HA while selected light is turned off, the switch is stuck in OFF state.
All while the same template is evaluated as expected (true when on, false when off) in the template editor.
To be honest, in this particular case, I’m not completely certain how startup affects the Template Switch’s value_template. It may have something to do with how Home Assistant restores the state of entities at startup.
Your value_template will ever update when you want it to update. It will only update when input_select.w_bed_light’s update. So if you don’t change the state of input_select.w_bed_light often, the state of the switch will not reflect the state properly.
Basically all value_templates on devices work the same way. They require entity_id’s to know what to watch so they can update accordingly. You are only giving it input_select.w_bed_light. Therefore, it will never update when the switch that is referencing updates.
To get around this, make a template binary_sensor that references the input_select and all selectable entities with in the enitty_id field. Then use that in the value_template field for your switch.
Ok, that makes sense. Sounds like value_template is only evaluated if the entity_id explicitly written inside the template changes. In this case, it’s only looking at input_select, not the actual light.
But I just ran a simple test: turn on bedroom 2 light. Template switch is stuck in Off position, as before.
Change input_select to one of the Master bedroom light, not lit. Switch still Off. Expected and correct.
Change input_select back to lit bedroom 2 light, Switch should now be On if the template is evaluated again, but it’s still Off.
So, it goes like this? (not syntax checked, just writing on here, will test tonight):
Ah okay! I see what you are saying. I should use the entity_id field.
entity_id
(string | list)(Optional)
A list of entity IDs so the switch only reacts to state changes of these entities. This can be used if the automatic analysis fails to find all relevant entities.
Look like it’s exactly what’s missing. By putting the lights into entity_id, any change in those entity should trigger another value_template evaluation.
The same configuration variable is also in template switch. I like to keep things compact, I’ll try just adding entity_id to switch first.