[Guide]Template Fan and Broadlink

@PatrickHASS.io , the variable percentage is supposed to be passed over from this part of the template fan over to the script in the service. There are some slight differences in the way we wrote the yaml though I don’t think it should affect the way it passes on the value.

Looks like you have to debug the script slowly to make sure the value in percentage is passed on over to the script.

Mine

        set_percentage:
          service: script.bedroom_fan_set_speed
          data:
            percentage: "{{ percentage }}"
        speed_count: 3

|

bedroom_fan_set_speed:
  alias: bedroom_fan_set_speed
  sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.bedroom_fan_percentage
    data:
      value: '{{ percentage }}'

Yours

      set_percentage:
        service: script.fan_speed
        data:
          percentage: "{{ percentage }}"

|

fan_speed:
  alias: Fan Speed
  sequence:
  - service: input_number.set_value
    data:
      value: "{{ percentage }}"
    target:
      entity_id: input_number.tower_fan_percentage

I made the changes in my script to match yours and then ran a trace

Executed: May 5, 2022, 09:20:13
Result:
params:
  domain: input_number
  service: set_value
  service_data:
    value: ''
    entity_id:
      - input_number.tower_fan_percentage
  target:
    entity_id:
      - input_number.tower_fan_percentage
running_script: false
limit: 10

It’s not passing percentage to the input_number.tower_fan_percentage

There is a value in the template for percentage I can see it change when I use the developer tools.

Here is the entire fan speed script

fan_speed:
  alias: Fan Speed
  sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.tower_fan_percentage
    data:
      value: '{{ percentage }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - choose:
    - conditions:
      - condition: state
        entity_id: input_number.tower_fan_percentage
        state: '100.0'
      sequence:
      - service: remote.send_command
        data:
          device: bedroom_fan
          command: speed
        target:
          entity_id: remote.rm4_mini_remote
    - conditions:
      - condition: state
        entity_id: input_number.tower_fan_percentage
        state: '66.0'
      sequence:
      - service: remote.send_command
        data:
          device: bedroom_fan
          command: speed
        target:
          entity_id: remote.rm4_mini_remote
    - conditions:
      - condition: state
        entity_id: input_number.tower_fan_percentage
        state: '33.0'
      sequence:
      - service: remote.send_command
        data:
          device: bedroom_fan
          command: speed
        target:
          entity_id: remote.rm4_mini_remote
    default: []
  mode: single
  icon: mdi:fan-chevron-up

It’s very odd @PatrickHASS.io, I’m unable to reproduce your issue in my setup.

This is how I’ve written it in config and scripts yaml. Somehow the variable value in your case isn’t passed over to the script for some reason but as you said appears to be updating after the template fan sets the value.

configuration.yaml

#include tracking entities#

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom Fan"
        unique_id: bedroom_fan
        value_template: "{{ states('input_boolean.bedroom_fan_state') }}"
        percentage_template: "{{ states('input_number.bedroom_fan_percentage') }}"
        oscillating_template: "{{ states('input_select.bedroom_fan_osc') }}"
        turn_on:
          service: script.bedroom_fan_on
        turn_off:
          service: script.bedroom_fan_off
        set_oscillating:
          service: script.fan_oscillating
          data:
            oscillating: "{{ oscillating }}"
        set_percentage:
          service: script.bedroom_fan_set_speed
          data:
            percentage: "{{ percentage }}"
        speed_count: 3

scripts.yaml

bedroom_fan_set_speed:
  alias: bedroom_fan_set_speed
  sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.bedroom_fan_percentage
    data:
      value: '{{ percentage }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - choose:
    - conditions:
      - condition: state
        entity_id: input_number.bedroom_fan_percentage
        state: '66.0'
      sequence:
      - service: script.bedroom_fan_inc
        data: {}
    - conditions:
      - condition: state
        entity_id: input_number.bedroom_fan_percentage
        state: '100.0'
      sequence:
      - service: script.bedroom_fan_inc
        data: {}
    - conditions:
      - condition: state
        entity_id: input_number.bedroom_fan_percentage
        state: '33.0'
      sequence:
      - service: script.bedroom_fan_inc
        data: {}
    default: []
  mode: single

The only thing I can think of at this point is I set up the script via the gui instead of a direct yaml entry.

If you’re copying it direct to script via GUI, hit edit in yaml and paste it in this way. The name and alias will match and that could be the trick.

alias: bedroom_fan_set_speed
sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.bedroom_fan_percentage
    data:
      value: '{{ percentage }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - choose:
      - conditions:
          - condition: state
            entity_id: input_number.bedroom_fan_percentage
            state: '66.0'
        sequence:
          - service: script.bedroom_fan_med
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.bedroom_fan_percentage
            state: '100.0'
        sequence:
          - service: script.bedroom_fan_med
            data: {}
      - conditions:
          - condition: state
            entity_id: input_number.bedroom_fan_percentage
            state: '33.0'
        sequence:
          - service: script.bedroom_fan_med
            data: {}
    default: []
mode: single

Nope that didn’t do the trick. Still not passing the percent variable.

I was looking at the helper input_number.bedroom_fan_percentage and it’s default is set to percent is that correct?

I don’t see percent as a default to an input number entity. You should have it as a regular number

yaml

input_number:
  bedroom_fan_percentage:
    name: Bedroom Fan Percentage
    min: 0
    max: 100

My mistake it is set as Type: Number

Just a quick thank you for taking the time to do this, it has been incredibly useful. Have been struggling to get speed settings working for ages and finally got them right with your help. They really need better documentation against the template fan.

2 Likes

Did you succeed? I have the same problem.
Fan Speed: Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data[‘value’]

@chrislimk Thanks for the helpful guide. I tried to update it for my fan. It has one button that switches speeds in loop 1, 2, 3, 1… And added the ability to adjust the speed both up and down and skip speeds. It was hard, but here is the result:

Fan speed script:
This script receives the percentage value, compares with the old value, directs it to another script that sends the actual command and updates it in the old value input_number entity.

alias: Fan Set Speed
sequence:
  - service: input_number.set_value
    data:
      value: '{{ percentage }}'
    target:
      entity_id: input_number.midea_fan_percentage
  - choose:
      - conditions:
          - condition: state
            entity_id: input_number.midea_fan_percentage
            state: '66.0'
        sequence:
          - if:
              - condition: state
                entity_id: input_number.midea_fan_percentage_old_value
                state: '33.0'
            then:
              - service: script.fan_speed_increase
                data: {}
              - service: input_number.set_value
                data:
                  value: 66
                target:
                  entity_id: input_number.midea_fan_percentage_old_value
            else:
              - if:
                  - condition: state
                    entity_id: input_number.midea_fan_percentage_old_value
                    state: '100.0'
                then:
                  - service: script.fan_speed_decrease
                    data: {}
                  - service: input_number.set_value
                    data:
                      value: 66
                    target:
                      entity_id: input_number.midea_fan_percentage_old_value
      - conditions:
          - condition: state
            entity_id: input_number.midea_fan_percentage
            state: '100.0'
        sequence:
          - if:
              - condition: state
                entity_id: input_number.midea_fan_percentage_old_value
                state: '33.0'
            then:
              - service: script.fan_speed_decrease
                data: {}
              - service: input_number.set_value
                data:
                  value: 100
                target:
                  entity_id: input_number.midea_fan_percentage_old_value
            else:
              - if:
                  - condition: state
                    entity_id: input_number.midea_fan_percentage_old_value
                    state: '66.0'
                then:
                  - service: script.fan_speed_increase
                    data: {}
                  - service: input_number.set_value
                    data:
                      value: 100
                    target:
                      entity_id: input_number.midea_fan_percentage_old_value
      - conditions:
          - condition: state
            entity_id: input_number.midea_fan_percentage
            state: '33.0'
        sequence:
          - if:
              - condition: state
                entity_id: input_number.midea_fan_percentage_old_value
                state: '66.0'
            then:
              - service: script.fan_speed_decrease
                data: {}
              - service: input_number.set_value
                data:
                  value: 33
                target:
                  entity_id: input_number.midea_fan_percentage_old_value
            else:
              - if:
                  - condition: state
                    entity_id: input_number.midea_fan_percentage_old_value
                    state: '100.0'
                then:
                  - service: script.fan_speed_increase
                    data: {}
                  - service: input_number.set_value
                    data:
                      value: 33
                    target:
                      entity_id: input_number.midea_fan_percentage_old_value
    default: []
mode: single
icon: mdi:fan-plus

script.fan_speed_increase - sends command once
script.fan_speed_decrease - sends command two times

and input_number.midea_fan_percentage

input_number.midea_fan_percentage_old_value is the same

3 Likes

You need to edit script in yaml mode and set value '{{ percentage }}'. You can’t set it in visual mode sadly

Very nice! I had a thought of doing that as an improvement to the basic sequential one but never got down to doing it. It was more habitual for me to increase the speed and leave it there before turning the fan off completely :laughing:

Thanks for sharing your innovation!

If I have an RF fan. and each of my 6 buttons are their only script. Each controller a different speed. I already have the power button working on a diff card so I dont need that. my goal is just a slider for the 6 speeds. Do i need anything more than this in my config file?

You’d need the script to receive the percentages so that each card/underlying script can be referenced to do the action.

Basically you’d redirect what’s in the template fan as well as the fan speed script into your existing scripts.

Why in the world is

  value: '{{ percentage }}'

and not

 value: "{{state_attr('fan.tower_fan','percentage')}}"

I found all this incredibly complicated for what i would like to do. I have a 3 speed fan controlled by a sonoff 4channel in interlock mode (tasmota).
I was thinking if i used the examples above to control the fan from HA it would work but if i pressed the physical buttons on the sonoff it would not update the input select.

Could i make 2 automations, 1 to switch the sonoff based on the position of a 4 state input select. And an another automation that could set the input select when triggered by the physical buttons on the sonoff,

or would this give me some sort of infinite loop issue