Is it possible to pass an entity as a parameter to a script that uses templates?

I am trying to write a script where I can pass in an entity and a input_select as parameters and use them inside. This would allow this script to be resused for multple entities.

Here’s the code snippet:

Script:

 save_study:
   sequence:
   - service: input_select.select_option
     data_template:
        entity_id: "{{ select }}"
        option: >-
          {%- if is_state('{{ light }}', 'on') -%}
            on
          {%- else -%}
            off
          {%- endif -%}

Caller:

 ...
   - service: script.save_study
     data:
       variables:
         select: input_select.study_ceiling_light_state
         light: light.study_ceiling
 ...

But it doesn’t seem to work… The version without the variables does, i.e. this code:

save_study_old:
  sequence:
  - service: input_select.select_option
    data_template:
       entity_id: input_select.study_ceiling_light_state
       option: >-
         {%- if is_state('light.study_ceiling', 'on') -%}
           on
         {%- else -%}
           off
         {%- endif -%}

Is it just to possible? or am I using the variables wrong/wrong syntax?
thx

In your caller script, remove the variables line. It is not needed.

 ...
   - service: script.save_study
     data:
       select: input_select.study_ceiling_light_state
       light: light.study_ceiling
 ...

Everything else looks fine.

Yep, variables is only needed if calling the script via script.turn_on. See the following for more details about the differences between calling a script “directly” (i.e., using its entity_id as the service) and “indirectly” (i.e., using the script.turn_on service):

Passing Variables to Scripts
Waiting for Script to Complete

As an FYI I needed to change it to the following:

save_study:
   sequence:
   - service: input_select.select_option
     data_template:
        entity_id: "{{ entityparam }}"
        option: >-
          {%- if is_state(lightparam, 'on') -%}
            on
          {%- else -%}
            off
          {%- endif -%}
 - service: script.save_study
    data:
      entityparam: input_select.study_ceiling_light_state
      lightparam: 'light.study_ceiling'

Thanks for the help! :slight_smile:

Another issue… :slight_smile:

I’m trying to do a similar thing in a service_template

 restore_light_state:
   sequence:
   - service_template: >
       {% if is_state(entityparam, "on") %}
          light.turn_on
       {% else %}
          light.turn_off
       {% endif %}
     data:
       entity_id: "{{ lightparam }}"

calling with:

  - service: script.restore_light_state
    data:
      entityparam: input_select.study_ceiling_light_state
      lightparam: light.study_ceiling

but I get the following error:

restore_light_state: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

Yet:

restore_light_state:
  sequence:
  - service_template: >
      {% if is_state(entityparam, "on") %}
         light.turn_on
      {% else %}
         light.turn_off
      {% endif %}
    data:
      entity_id: light.study_ceiling

Works fine. So there is somethere in the substiation of the line:

entity_id: "{{ lightparam }}"

It doesn’t like. What’s weird is that works in the previous example…

Ok, i fixed it… The data section needed to be a data_template:

restore_light_state:
  sequence:
  - service_template: >
      {% if is_state(entityparam, "on") %}
         light.turn_on
      {% else %}
         light.turn_off
      {% endif %}
    data_template:
      entity_id: "{{ lightparam }}""
1 Like