Service template help

Hi guys,

I’m trying to have a crack at tidying up some of my code using templates, but I can’t seem to get it right. I previously had 11 separate files to do the job of the one I’m trying to create. Basically I have an input_select with a list of scenes for the master bedroom. When I select an item I want it to launch the corresponding script. The script names and input_select state names are correct, but when I select one I get the following error:

ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service:

Also, if it’s relevant, I have another option in the input_select called ‘Select’ which if selected should do nothing.

Please can somebody tell me how to fix this, and also why it is wrong:

alias: trigger_scene_master_bedroom
trigger:
  platform: state
  entity_id: input_select.master_bedroom_scenes
action:
  service_template: >
     {% if is_state ('{{ trigger.to_state.state }}' ,'Spring Blossom') %} script.master_bedroom_spring_blossom
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Savanna Sunset') %} script.master_bedroom_savanna_sunset
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Relax') %} script.master_bedroom_relax
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Reading') %} script.master_bedroom_read
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Nightlight') %} script.master_bedroom_nightlight
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Energize') %} script.master_bedroom_spring_energize
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Dimmed') %} script.master_bedroom_dimmed
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Concentrate') %} script.master_bedroom_concentrate
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Bright') %} script.master_bedroom_bright
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Arctic Aurora') %} script.master_bedroom_arctic_aurora
     {% elif is_state ('{{ trigger.to_state.state }}' , 'Tropical Twilight') %} script.master_bedroom_tropical_twilight
     {% endif %}

If it helps, one of the 11 files I was using (which worked) was:

alias: trigger_scene_master_bedroom_arctic_aurora
trigger:
  platform: state
  entity_id: input_select.master_bedroom_scenes
  to: 'Arctic Aurora'
action:
  service: script.turn_on
  entity_id: script.master_bedroom_arctic_aurora

Although I’m led to believe that I didn’t need script.turn_on and just service:script.master_bedroom_arctic_aurora would have sufficed, hence putting it in the template like that.

Any help appreciated, thanks.

Have you tried it with:

action:
  service_template: >
     {% if is_state ('input_select.master_bedroom_scenes' ,'Spring Blossom') %} script.master_bedroom_spring_blossom
     {% elif is_state ('input_select.master_bedroom_scenes' , 'Savanna Sunset') %} script.master_bedroom_savanna_sunset
     ...

Not sure, but i think trigger.to_state.state returns only the state, but is_state needs an entity.

1 Like

I hadn’t but that makes perfect sense, I’ll try shortly and let you know, thanks :+1:

Yup, that’s done it - thanks again :smiley: