Fan Template for 4-Speed Fan

Hi,
I’m totally newbie writing HA templates and I created the next script to use in a fan template entity. I tested the script and works fine changing the slider and executing it, but I don’t have idea how to pass the input_number like an argument to the script inside the fan template.

alias: Study fan speed action
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_selector
            state: '0.0'
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: switch.speed_1
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_1
              - conditions:
                  - condition: state
                    entity_id: switch.speed_2
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_2
              - conditions:
                  - condition: state
                    entity_id: switch.speed_3
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_3
              - conditions:
                  - condition: state
                    entity_id: switch.speed_4
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_4
            default: []
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.study_fan_state
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_selector
            state: '25.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_1
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_selector
            state: '50.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_2
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_selector
            state: '75.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_3
          - service: input_boolean.turn_on
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_selector
            state: '100.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_4
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
    default: []
mode: single

and this other script:

alias: Study fan speed set
sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.study_fan_speed_selector
    data:
      value: '{{ percentage }}'
  - service: script.study_fan_speed_action
mode: single

And I need to pass the input_number value to create an entity fan with this code, but in not working:

  - platform: template
    fans:
      study_fan:
        friendly_name: "Ventilador Estudio"
        value_template: "{{ states('input_boolean.study_fan_state') }}"
        percentage_template: "{{ states('input_number.study_fan_speed_selector') }}"
        turn_on:
          service: script.study_fan_speed_set
          data:
            value: 25.0
        turn_off:
          service: script.study_fan_speed_set
          data:
            value: 0.0
        set_percentage:
          service: script.study_fan_speed_set
          data:
            value: "{{ percentage }}"
        speed_count: 4
Logger: homeassistant.helpers.script.ventilador_estudio
Source: helpers/script.py:1334
First occurred: 3:40:10 PM (2 occurrences)
Last logged: 3:40:25 PM

Ventilador Estudio: Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data['value']

What I’m doing wrong? Thanks.

I’ tried another implementation based on this post: https://community.home-assistant.io/t/help-new-template-fan-integration-percentage/286823/12?u=jjmuriel , but the error is the same.

fan:
  - platform: template
    fans:
      study_fan:
        friendly_name: "Ventilador Estudio"
        value_template: "{{ states('input_boolean.study_fan_state') }}"
        percentage_template: "{{ states('input_number.study_fan_speed_selector') }}"
        turn_on:
          service: script.study_fan_on
        turn_off:
          service: script.study_fan_off
        set_percentage:
          service: script.study_fan_set_speed_percent
          data:
            value: "{{ percentage }}"
        speed_count: 4
script:
  study_fan_off:
    alias: Study Fan Off
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: switch.speed_1
                state: 'on'
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.speed_1
          - conditions:
              - condition: state
                entity_id: switch.speed_2
                state: 'on'
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.speed_2
          - conditions:
              - condition: state
                entity_id: switch.speed_3
                state: 'on'
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.speed_3
          - conditions:
              - condition: state
                entity_id: switch.speed_4
                state: 'on'
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.speed_4
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.study_fan_state
      
  study_fan_on:
    alias: Study Fan On
    sequence:
      - service: input_boolean.turn_on
        entity_id: input_boolean.study_fan_state
      - service: script.study_fan_set_speed_percent
    
  study_fan_set_speed_percent:
    alias: Study Fan Set Percentage
    sequence:
      - service: input_number.set_value
        entity_id: input_number.study_fan_speed_percentage
        data:
          value: "{{ percentage }}"
      - delay:
          milliseconds: 500
      - service: >
          {% if states("input_number.study_fan_speed_percentage") | int == 25 %}
            script.study_fan_low_speed            
          {% elif states("input_number.study_fan_speed_percentage") | int == 50 %}
            script.study_fan_medium_speed
          {% elif states("input_number.study_fan_speed_percentage") | int == 75 %}
            script.study_fan_medium_high_speed
          {% elif states("input_number.study_fan_speed_percentage") | int == 100 %}
            script.study_fan_high_speed
          {% else %}
            script.study_fan_off
          {% endif %}
      - service: >
          {% if states("input_number.study_fan_speed_percentage") | int == 25 %}
            input_boolean.turn_on
          {% elif states("input_number.study_fan_speed_percentage") | int == 50 %}
            input_boolean.turn_on
          {% elif states("input_number.study_fan_speed_percentage") | int == 75 %}
            input_boolean.turn_on
          {% elif states("input_number.study_fan_speed_percentage") | int == 100 %}
            input_boolean.turn_on
          {% else %}
            input_boolean.turn_off
          {% endif %}
        entity_id: input_boolean.study_fan_state
      
  study_fan_low_speed:
    alias: Study Fan Low
    sequence:
      - service: switch.turn_on
        entity_id:  switch.speed_1

  study_fan_medium_speed:
    alias: Study Fan Medium
    sequence:
      - service: switch.turn_on
        entity_id:  switch.speed_2

  study_fan_medium_high_speed:
    alias: Study Fan Medium High
    sequence:
      - service: switch.turn_on
        entity_id:  switch.speed_3
      
  study_fan_high_speed:
    alias: Study Fan High
    sequence:
      - service: switch.turn_on
        entity_id:  switch.speed_4

* Ventilador Estudio: Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data['value']

I checked my code and I had a wrong input_number entity name. This is the fully working code:

# Scripts

study_fan_speed_action:
  alias: Study fan speed action
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: input_number.study_fan_speed_percentage
        state: '0.0'
      sequence:
      - choose:
        - conditions:
          - condition: state
            entity_id: switch.speed_1
            state: 'on'
          sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.speed_1
        - conditions:
          - condition: state
            entity_id: switch.speed_2
            state: 'on'
          sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.speed_2
        - conditions:
          - condition: state
            entity_id: switch.speed_3
            state: 'on'
          sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.speed_3
        - conditions:
          - condition: state
            entity_id: switch.speed_4
            state: 'on'
          sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.speed_4
        default: []
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.study_fan_state
    - conditions:
      - condition: state
        entity_id: input_number.study_fan_speed_percentage
        state: '25.0'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.speed_1
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.study_fan_state
    - conditions:
      - condition: state
        entity_id: input_number.study_fan_speed_percentage
        state: '50.0'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.speed_2
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.study_fan_state
    - conditions:
      - condition: state
        entity_id: input_number.study_fan_speed_percentage
        state: '75.0'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.speed_3
      - service: input_boolean.turn_on
    - conditions:
      - condition: state
        entity_id: input_number.study_fan_speed_percentage
        state: '100.0'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.speed_4
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.study_fan_state
    default: []
  mode: single
study_fan_speed_set:
  alias: Study fan speed set
  sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.study_fan_speed_percentage
    data:
      value: '{{ percentage }}'
  - service: script.study_fan_speed_action
  mode: single

# FAN TEMPLATE

  - platform: template
    fans:
      study_fan:
        friendly_name: "Ventilador Estudio"
        value_template: "{{ states('input_boolean.study_fan_state') }}"
        percentage_template: "{{ states('input_number.study_fan_speed_percentage') }}"
        turn_on:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 25 }}"
        turn_off:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 0 }}"
        set_percentage:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ percentage }}"
        speed_count: 4
2 Likes

I really wish this could just be fixed in HA. Thanks for this tho!

1 Like

Hi @jjmuriel I tried your lastest code but keeps giving me this error:Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data[‘value’].
I know you said that it was working for you but not for me. I even setup the helpers correctly but no luck. Regards

Hi,

This is my full code and now is working without any error:

alias: Study fan speed action
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_percentage
            state: '0.0'
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: switch.speed_1
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_1
                    data: {}
              - conditions:
                  - condition: state
                    entity_id: switch.speed_2
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_2
                    data: {}
              - conditions:
                  - condition: state
                    entity_id: switch.speed_3
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_3
                    data: {}
              - conditions:
                  - condition: state
                    entity_id: switch.speed_4
                    state: 'on'
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.speed_4
                    data: {}
            default: []
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.study_fan_state
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_percentage
            state: '25.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_1
            data: {}
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_percentage
            state: '50.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_2
            data: {}
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_percentage
            state: '75.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_3
            data: {}
          - service: input_boolean.turn_on
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.study_fan_speed_percentage
            state: '100.0'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.speed_4
            data: {}
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_fan_state
            data: {}
    default: []
mode: single

# __________________________________________________

alias: Study fan speed set
sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.study_fan_speed_percentage
    data:
      value: '{{ percentage }}'
  - service: script.study_fan_speed_action
    data: {}
mode: single

# _________________________________________________

#FAN TEMPLATE (fan.yaml)

  - platform: template
    fans:
      study_fan:
        friendly_name: "Ventilador Estudio"
        value_template: "{{ states('input_boolean.study_fan_state') }}"
        percentage_template: "{{ states('input_number.study_fan_speed_percentage') }}"
        turn_on:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 25 }}"
        turn_off:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 0 }}"
        set_percentage:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ percentage }}"
        speed_count: 4

ESPHome code:

esphome:
  name: fan_speed_controller
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pass

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fan Speed Controller"
    password: !secret ap_pass

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80

# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Study Smart Fan 1
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: Study Smart Fan IP
    ssid:
      name: Study Smart Fan SSID
    bssid:
      name: Study Smart Fan BSSID
      
# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: Study Smart Fan Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Study Smart Fan WiFi Signal
    update_interval: 60s

time:
  - platform: sntp
    id: sntp_time
    timezone: America/SomePlace
    servers: 192.168.X.X

switch:
  - platform: gpio
    pin: D0
    inverted: true
    name: "Speed 1"
    id: speed_one
    interlock: &interlock_group [speed_one, speed_two, speed_three, speed_four]
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D5
    inverted: true
    name: "Speed 2"
    id: speed_two
    interlock: *interlock_group
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D6
    inverted: true
    name: "Speed 3"
    id: speed_three
    interlock: *interlock_group
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D7
    inverted: true
    name: "Speed 4"
    id: speed_four
    interlock: *interlock_group
    interlock_wait_time: 500ms

And this is the helper:

I expect this information can help you.

1 Like

Hi my config looks pretty the same as mine but my esp code is simpler :slight_smile:
I copied all your code and done the input number but the script called study fan speed set keeps giving me the error expects a float :frowning: I feel quite that I can’t make it work even when I full copied your code. Thanks anyway

1 Like