Passing variables into conditional script execution

I’ve gone through many posts and tried a few times to fix my code, but to no avail.

I am really hoping that someone can point out where the following is failing when trying to pass variables between scripts. Fairly certain it’s in my IF ELSE statements:

fan_set_speed:
  alias: Fan (speed)
  sequence:
    - service: input_select.select_option
      target:
        entity_id: input_select.{{ room }}_fan_speed #Variable here
      data_template:
        option: "{{ percentage }}"
    - service: script.fan_on
      data_template:
        room: "{{ room }}"
fan_off:
  alias: Fan Off
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.rmpro_remote
        #Variables Below
        command: >
          {% if "{{ room }}" == "b1"  %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b2" %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b3" %}
          {{ 'b64: ' }}
          {% endif %}
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.{{ room }}_fan_state
fan_on:
  alias: Fan On
  sequence:
    - service: input_boolean.turn_on
      target:
        entity_id: input_boolean.{{ room }}_fan_state #Variable here
    #    - delay:
    #        seconds: 1
    - service: >
        {% if is_state('input_select.{{ room }}_fan_speed', '33') %}
          script.fan_33
        {% elif is_state('input_select.{{ room }}_fan_speed', '66') %}
          script.fan_66
        {% elif is_state('input_select.{{ room }}_fan_speed', '100') %}
          script.fan_100
        {% endif %}
      data_template:
        room: "{{ room }}"
fan_33:
  alias: Fan (min)
  sequence:
    - service: input_text.set_value
      target:
        entity_id: input_select.{{ room }}_fan_speed #Variable here
      data:
        value: "33"
    - service: remote.send_command
      data_template:
        entity_id: remote.rmpro_remote
        command: >
          {% if "{{ room }}" == "b1"  %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b2" %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b3" %}
          {{ 'b64: ' }}
          {% endif %}

fan_66:
  alias: Fan (med)
  sequence:
    - service: input_text.set_value
      target:
        entity_id: input_select.{{ room }}_fan_speed #Variable here
      data:
        value: "66"
    - service: remote.send_command
      data:
        entity_id: remote.rmpro_remote
        #Variables Below
        command: >
          {% if "{{ room }}" == "b1"  %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b2" %}
          {{ 'b64:' }}
          {%-elif "{{ room }}" == "b3" %}
          {{ 'b64: ' }}
          {% endif %}
fan_100:
  alias: Fan (max)
  sequence:
    - service: input_text.set_value
      target:
        entity_id: input_select.{{ room }}_fan_speed #Variable here
      data:
        value: "100"
    - service: remote.send_command
      data:
        entity_id: remote.rmpro_remote
        #Variables Below
        command: >
          {% if "{{ room }}"== "b1"  %}
          {{ "b64:" }}
          {%-elif "{{ room }}" == "b2" %}
          {{ "b64:" }}
          {%-elif "{{ room }}" == "b3" %}
          {{ "b64:" }}
          {% endif %}

Any help is greatly appreciated!

You can’t nest templates. Fairly common mistake. Try this:

    - service: >
        {% if is_state('input_select.' ~ room ~ '_fan_speed', '33') %}
          script.fan_33
        {% elif is_state('input_select.' ~ room ~ '_fan_speed', '66') %}
          script.fan_66
        {% elif is_state('input_select.' ~ room ~ '_fan_speed', '100') %}
          script.fan_100
        {% endif %}
      data_template:
        room: "{{ room }}"
1 Like

Thanks! One step closer (I hope) as I figured I was passing those variables incorrectly.

Still getting the following errors though:

021-06-29 23:04:36 ERROR (MainThread) [homeassistant.components.script.fan_33] Fan (min): Error executing script. Invalid data for call_service at pos 2: length of value must be at least 1 @ data['command'][0]
2021-06-29 23:04:36 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.fan_33, old_state=<state script.fan_33=on; last_triggered=2021-06-29T23:04:36.230592+10:00, mode=single, current=1, friendly_name=Fan (min) @ 2021-06-29T23:04:36.230641+10:00>, new_state=<state script.fan_33=off; last_triggered=2021-06-29T23:04:36.230592+10:00, mode=single, current=0, friendly_name=Fan (min) @ 2021-06-29T23:04:36.247049+10:00>>
2021-06-29 23:04:36 ERROR (MainThread) [homeassistant.components.script.fan_on] Fan On: Error executing script. Invalid data for call_service at pos 2: length of value must be at least 1 @ data['command'][0]
2021-06-29 23:04:36 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.fan_on, old_state=<state script.fan_on=on; last_triggered=2021-06-29T23:04:36.224099+10:00, mode=single, current=1, friendly_name=Fan On @ 2021-06-29T23:04:36.224141+10:00>, new_state=<state script.fan_on=off; last_triggered=2021-06-29T23:04:36.224099+10:00, mode=single, current=0, friendly_name=Fan On @ 2021-06-29T23:04:36.249360+10:00>>
2021-06-29 23:04:36 ERROR (MainThread) [homeassistant.components.script.fan_set_speed] Fan (speed): Error executing script. Invalid data for call_service at pos 2: length of value must be at least 1 @ data['command'][0]
2021-06-29 23:04:36 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.fan_set_speed, old_state=<state script.fan_set_speed=on; last_triggered=2021-06-29T23:04:36.178754+10:00, mode=single, current=1, friendly_name=Fan (speed) @ 2021-06-29T23:04:36.178812+10:00>, new_state=<state script.fan_set_speed=off; last_triggered=2021-06-29T23:04:36.178754+10:00, mode=single, current=0, friendly_name=Fan (speed) @ 2021-06-29T23:04:36.262393+10:00>>
2021-06-29 23:04:36 ERROR (MainThread) [homeassistant.core] Error executing service: <ServiceCall script.fan_set_speed (c:126688aab7650455199aebd97ac28162): percentage=33, room=b3>
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/core.py", line 1507, in catch_exceptions
await coro_or_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 278, in service_handler
await script_entity.async_turn_on(
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 382, in async_turn_on
await coro
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 404, in _async_run
return await self.script.async_run(variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1216, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 350, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 368, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 571, in _async_call_service_step
await self._async_run_long_action(service_task)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 532, in _async_run_long_action
long_task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 278, in service_handler
await script_entity.async_turn_on(
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 382, in async_turn_on
await coro
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 404, in _async_run
return await self.script.async_run(variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1216, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 350, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 368, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 571, in _async_call_service_step
await self._async_run_long_action(service_task)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 532, in _async_run_long_action
long_task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 278, in service_handler
await script_entity.async_turn_on(
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 382, in async_turn_on
await coro
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 404, in _async_run
return await self.script.async_run(variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1216, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 350, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 368, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 568, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 213, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 658, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 760, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 695, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/broadlink/remote.py", line 258, in async_send_command
kwargs = SERVICE_SEND_SCHEMA(kwargs)
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
return self._compiled([], data)
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
return base_validate(path, iteritems(data), out)
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: length of value must be at least 1 @ data['command'][0]

Well, I don’t use remotes, so I’m not exactly sure what you need, but maybe at least:

    - service: remote.send_command
      data_template:
        entity_id: remote.rmpro_remote
        command: >
          {% if room == "b1"  %}
          b64:
          {% elif room == "b2" %}
          b64:
          {% elif room == "b3" %}
          b64:
          {% endif %}
1 Like

Thank you SO much! Working perfectly now. Learning a lot about the formatting types of templating, even with the documentation provided by HA.

REALLY appreciate your help!

Are there only three options for each room’s input_select (33, 66, and 100) or are there more?

If there are only 33, 66, and 100 then the template can be reduced to:

  - service: "script.fan_{{ states('input_select.' ~ room ~ '_fan_speed') }}"

If there are other options then your existing template overlooks to take them into account and will produce no result when it encounters them (which will cause the service call to fail).

1 Like

Really great suggestion :slight_smile: thanks will implement this also!

Why are you using three separate scripts, one for each of the three speeds, when just one script can do the job? In fact, it doesn’t need to be a separate script; the actions can be part of the fan_on script.