Template within template?

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 :frowning: This returns false:

{{ is_state("{{states.input_select.w_bed_light.state}}", 'on') }}

Without " it simply gives me errors.

Is there a special syntax to get the template to evaluate inner template first, then use output of that template to evaluate the whole statement?

Try this:

{{ is_state(states.input_select.w_bed_light.state, 'on') }}
1 Like

It works! Thank you!

I thought I had tried all bracket combinations, obviously have missed the simplest and most obvious.

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.

Any ideas?

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.

This is template switch. It’s very straight forward:

switch:
  - platform: template
    switches: 
      morning_routine_w:
        value_template: "{{ is_state(states.input_select.w_bed_light.state, 'on') }}"
        turn_on: 
          service: script.morning_routine_w
        turn_off:
          service: light.turn_off
          data_template:
            entity_id: '{{ states.input_select.w_bed_light.state }}'

This is the morning routine script, which turns on the light.

morning_routine_w:
  sequence:
  - data_template:
      entity_id: '{{ states.input_select.w_bed_light.state }}'
      transition: 0
      brightness: 1
    service: light.turn_on
  - data:
      entity_id: switch.ikea_plug_1
    service_template: '{% if states.automation.plug_start_timer.state == "on"
      %} switch.turn_on {%- else -%} script.do_nothing {% endif %}'
  - service_template: '{% if is_state("input_select.w_commute_car", "leaf") 
      %} script.leaf_start_climate {% else %} script.do_nothing {% endif %}'

(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)

What is the configuration for input_select.w_bed_light?

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.

input_select:
  w_bed_light:
    name: 'W bedside light'
    icon: mdi:lamp
    options:
      - light.master_bedroom_1
      - light.master_bedroom_2
      - light.bedroom_2_light_bulb

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.

Could be a bug?

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.

1 Like

Short answer No.

Long answer:

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):

binary_sensor:
  - platform: template
    sensors:
      w_bed_light_state:
        value_template: >-
          {% if is_state('input_select.w_bed_light', 'light.master_bedroom_1') %} states('light.master_bedroom_1') 
          {% elsif is_state('input_select.w_bed_light', 'light.master_bedroom_2') %}  states('light.master_bedroom_2') 
          {% elsif is_state('input_select.w_bed_light', 'light.bedroom_2_light_bulb') %}  states('light.bedroom_2_light_bulb')    
          {% endif %}

Might as well put the same template into the sensor itself?

switch:
  - platform: template
    switches: 
      morning_routine_w:
        value_template: >-
          {% if is_state('input_select.w_bed_light', 'light.master_bedroom_1') %} states('light.master_bedroom_1') 
          {% elsif is_state('input_select.w_bed_light', 'light.master_bedroom_2') %}  states('light.master_bedroom_2') 
          {% elsif is_state('input_select.w_bed_light', 'light.bedroom_2_light_bulb') %}  states('light.bedroom_2_light_bulb')    
          {% endif %}

I’d make the template like this:

binary_sensor:
- platform: template
  sensors: 
    bed_or_morning:
      entity_id:
      - light.master_bedroom_1
      - light.master_bedroom_2
      - light.bedroom_2_light_bulb
      - input_select.w_bed_light
     value_template: >
       {{ is_state(states.input_select.w_bed_light.state, 'on') }}

Then in your switch…

switch:
  - platform: template
    switches: 
      morning_routine_w:
        value_template: "{{ is_state('binary_sensor.bed_or_morning', 'on') }}"
        turn_on: 
          service: script.morning_routine_w
        turn_off:
          service: light.turn_off
          data_template:
            entity_id: '{{ states.input_select.w_bed_light.state }}'
1 Like

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.

switch:
  - platform: template
    switches: 
      morning_routine_w:
        entity_id:
        - light.master_bedroom_1
        - light.master_bedroom_2
        - light.bedroom_2_light_bulb
        - input_select.w_bed_light
        value_template: "{{ is_state(states.input_select.w_bed_light.state, 'on') }}"
        turn_on: 
          service: script.morning_routine_w
        turn_off:
          service: light.turn_off
          data_template:
            entity_id: '{{ states.input_select.w_bed_light.state }}'

Ah, yes that should work. Looks like they added that ability to the template switches so you don’t need the template sensor.

It works! The swith is showing on and off as I change state of any light selected by the input_select.

Thank you, petro and 123, for your help.

1 Like