Fan speed script help

I’m trying to create a script to increase my fan speed so I can trigger the speed with my harmony through emulated_roku. I’m on hassio 163 (0.92.2) and using a ifan02 flashed with tasmota, the fan control is working fine through lovelace and google assistant.

I’m not very familiar with scripts in homeassistant, I found the following to use for my lights and its working well.

‘1234567892’:

alias: MasterBedroom Brightness Increase Button

sequence:

service: light.turn_on

data_template:

entity_id: light.master_bedroom_ceiling

transition: ‘1’

brightness: "{% if not state_attr(‘light.master_bedroom_ceiling’, ‘brightness’)\

\ == None %} {% set n = state_attr(‘light.master_bedroom_ceiling’, ‘brightness’)\

\ + 25 %} {% if n > 255 %}\n 255\n{% else %}\n {{ n }}\n{% endif %} {%\

\ else %} 25 {% endif %}\n"

Looking to do the same with my fan, which has the following options for state, “off, low, medium, high”. I would like the script to set the speed to low if the fan is off, med if the fan is low, high if the fan is med, leave high if fan is high; at the time the script is executed.

Has anyone found a good write up for HA scripting they could share? I don’t find the components documentation very helpful for my current needs.

I’m pretty good at powershell scripting, but this is completely different.

Any help is appreciated!

- service: fan.turn_on
  data_template:
    speed: >
      {% if is_state('fan.YOUR_FAN' , 'off') %} low
      {% elif is_state('fan.YOUR_FAN' , 'low') %} medium 
      {% else %} high {% endif %}

That partially worked. State went from off to low after executing, then jumped to high after executing again.

This is what i placed in the scripts yaml.

'1234567896':
  alias: "fantest"
  sequence:
    service: fan.turn_on
    data_template:
      entity_id: fan.living_room_fan
      speed: >
        {% if is_state('fan.living_room_fan' , 'off') %} low
        {% elif is_state('fan.living_room_fan' , 'low') %} medium 
        {% else %} high {% endif %}

I also have tried the following, but it just goes from off to low.

 '1234567895':
      alias: "fan"
      sequence:
        service: fan.turn_on
        data_template:
          entity_id: fan.living_room_fan
          speed: "
            {% if is_state('fan.living_room_fan', 'off') %} low 
            {% elif is_state('fan.living_room_fan', 'low') %} medium 
            {% elif is_state('fan.living_room_fan', 'medium') %} high 
            {% endif %}"

Almost seems like the elseif statements are not processing. Also curious if I might need to specify ‘fan.living_room_fan.speed’ instead of just state based on the image, since “State” is only showing “on”
image

Yeah it’s going to need to use the attribute instead, as the state is only on/off looking at the picture :+1:

So I tried the following, and still same result. Goes to low if speed is off, then jumps to high if current speed is low. Any ideas?

'1234567896':
  alias: "fantest"
  sequence:
  - service: fan.turn_on
    data_template:
      entity_id: fan.living_room_fan
      speed: >
        {% if is_state('fan.living_room_fan', 'off') %} low
        {% elif is_state('fan.living_room_fan.speed', 'low') %} medium
        {% else %} high
        {% endif %}

Second line of the template should be

{% elif is_state_attr('fan.living_room_fan' , 'speed', 'low') %} medium
1 Like

Can the service fan.turn_on be used to set the fan’s speed? The fan component has another service called fan.set_speed.


EDIT

Based on the response below, I guess the answer is ‘yes’. :slight_smile:

That did the trick!

1 Like

So for anyone else that is trying to do the same thing.

'1234567896':
  alias: "fantest"
  sequence:
  - service: fan.turn_on
    data_template:
      entity_id: fan.living_room_fan
      speed: >
        {% if is_state('fan.living_room_fan', 'off') %} low
        {% elif is_state_attr('fan.living_room_fan' , 'speed', 'low') %} medium
        {% else %} high
        {% endif %}
1 Like