Template fan assistance

So i have a fan setup based on switches. switch.sonoff_100092da4f_2 is fan on and low speed. This switch is on no matter the speed of the fan.
switch.sonoff_100092da4f_3 is fan med speed
switch.sonoff_100092da4f_4 is fan high speed.

configuration.yaml

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Livingroom fan"
        value_template: "{{ states('input_boolean.livingroom_fan_state') }}"
        speed_template: "{{ states('input_select.livingroom_fan_speed') }}"
        turn_on:
          service: script.living_fan_on
        turn_off:
          service: script.living_fan_off
        set_speed:
          service: script.livingroom_fan_set_speed
          data_template:
            speed: "{{ speed }}"
        speeds:
          - '1'
          - '2'
          - '3'

scripts:

livingroom_fan_set_speed:
    alias: Livingroom Fan Set Speed
    sequence:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.livingroom_fan_speed
        option: '{{speed}}'
    - service: script.turn_on
      data_template:
        entity_id: script.livingroom_fan_speed_{{ speed }}



living_fan_off:
  alias: Livingroom Fan Off
  sequence:
    - service: switch.turn_off
      data:
        entity_id:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.livingroom_fan_state
living_fan_on:
  alias: Livingroom Fan Off
  sequence:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.livingroom_fan_state
    - delay:
        seconds: 1
    - service_template: >
        {% if is_state("input_select.livingroom_fan_speed", "1") %}
          script.living_fan_1
        {% elif is_state("input_select.bedroom_fan_speed", "2") %}
          script.living_fan_2
        {% elif is_state("input_select.bedroom_fan_speed", "3") %}
          script.living_fan_3
        {% endif %}

living_fan_1:
  alias: Living Fan Speed 1
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.sonoff_100092da4f_2

living_fan_2:
  alias: Living Fan Speed 2
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.sonoff_100092da4f_3

living_fan_3:
  alias: Living Fan speed 3
  sequence:
    - service: switch.turn_off
      data:
        entity_id: switch.sonoff_100092da4f_3
    - service: switch.turn_on
      data:
        entity_id: switch.sonoff_100092da4f_4

It doesnt seem to be working correctly. I can turn the fan on, but cannot change speeds. Also I cannot turn it off.

File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 370, in _handle_service_platform_call
await getattr(entity, func)(**data)
File “/usr/src/homeassistant/homeassistant/components/template/fan.py”, line 282, in async_turn_off
await self._off_script.async_run(context=self._context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 190, in async_run
await self._handle_action(action, variables, context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 274, in _handle_action
await self._actions[_determine_action(action)](action, variables, context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 357, in _async_call_service
context=context,
File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 97, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context
File “/usr/src/homeassistant/homeassistant/core.py”, line 1236, in async_call
await asyncio.shield(self._execute_service(handler, service_call))
File “/usr/src/homeassistant/homeassistant/core.py”, line 1261, in _execute_service
await handler.func(service_call)
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 142, in service_handler
await script.async_turn_on(variables=service.data, context=service.context)
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 214, in async_turn_on
raise err
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 209, in async_turn_on
await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 190, in async_run
await self._handle_action(action, variables, context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 274, in _handle_action
await self._actions[_determine_action(action)](action, variables, context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 357, in _async_call_service
context=context,
File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 97, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context
File “/usr/src/homeassistant/homeassistant/core.py”, line 1214, in async_call
processed_data = handler.schema(service_data)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 272, in call
return self._compiled([], data)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 594, in validate_dict
return base_validate(path, iteritems(data), out)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 432, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data[‘entity_id’]

It’s due to a naming error. If you check Developer Tools > States, you’ll see your speed-related scripts appear like this:

script.living_fan_1
script.living_fan_2
script.living_fan_3

Now compare that to how your code is referring to them:

    - service: script.turn_on
      data_template:
        entity_id: script.livingroom_fan_speed_{{ speed }}

It’s calling script.livingroom_fan_speed_1 which doesn’t exist so that’s why you aren’t able to control speed.

The alias option you’ve used here:

living_fan_1:
  alias: Living Fan Speed 1

serves to define the script’s friendly_name and is not used to define the script’s entity_id.

Example:

Script bla_bla defined here:
Screenshot%20from%202019-11-26%2013-24-50

How the script’s entity_id appears in the States page:
Screenshot%20from%202019-11-26%2013-24-28

1 Like

Thanks for the input; that fixed it!

1 Like