Changing domain from light. to fan

You need it set up in both places. Currently, your map in the speed template is getting values from brighness. Brightness is a int that spans 0 to 255. You set up your map as a flat list. It spans ints 0 to 7. So, when your brightness is set above 7, your speed template will fail. That will occur when you set your speed to any value other than off.

          {% set output = {0: "off", 120: "one", 140: "two", 160: "three", 180: "four", 200: "five", 220: "six", 240: "seven", 255: "eight"} %}
          {% set idx = state_attr('light.60600011ecfabcab8f89', 'brightness') %}
          {{ output[idx] }}

Log Details (ERROR)

Wed Dec 25 2019 04:10:38 GMT+1100 (Australian Eastern Daylight Time)

Received invalid speed: . Expected: [‘off’, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’].

changed back to my original post lights work fine. updates between apps well and i can set all speeds so thanks but if it works y fix it true?

Sure I guess but you said it didn’t work. Currently it shouldn’t work based on the configuration

said it worked using first code posted up that was reason for posting incase anyone still needed a working route. i tried every other bit of could i could find after a few “by the book” tries doin it myself this way didnt look right but still works pretty good

so due to ocd i got it to work as per ur suggestion and seems to work pretty well thank you
not sure why it was also working before definately some typo or uncleared setting

  - platform: template
    fans:
      one1in:
        friendly_name: "one1in"
        value_template: "{{ states('light.60600011ecfabcab8f89') }}"
        speed_template: >
          {% set output = {0: "off", 120: "one", 140: "two", 160: "three", 180: "four", 200: "five", 220: "six", 240: "seven", 255: "eight"} %}
          {% set idx = state_attr('light.60600011ecfabcab8f89', 'brightness') %}
          {{ output[idx] }}
        turn_on:
          service: homeassistant.turn_on
          entity_id: light.60600011ecfabcab8f89
        turn_off:
          service: homeassistant.turn_off
          entity_id: light.60600011ecfabcab8f89
        set_speed:
          service: homeassistant.turn_on
          entity_id: light.60600011ecfabcab8f89
          data_template:
            brightness: >
              {% set mapper = {'off': 0, 'one': 120, 'two': 140, 'three': 160, 'four': 180, 'five': 200, 'six': 220, 'seven': 240, 'eight': 255} %}
              {{ mapper[speed] }}
        speeds:
          - 'off'
          - 'one'
          - 'two'
          - 'three'
          - 'four'
          - 'five'
          - 'six'
          - 'seven'
          - 'eight'
2 Likes

Thanks for your OCD @pedzp14 :slight_smile:
I’ve got almost the same issue you’ve experienced. I’ve connected a bathroom fan to a zigbee dimmer and want to use that dimmer to regulate my bathroom fan. Deconz is propagting it to HA as a light, which in it;s essence is of course correct. I’ve searched the community’s and google for it and came across your post. I guess this fixes my problem. Especially the part with presets.
I do feel kind of a NooB now, because I can’t figure out where to put this code. Is it a sensor, is it anautomation, or??? Because when I adjust it to my own situation, no matter where I put in my config, I always get errors while checking the config.
So a slight push in the right direction would be greatly appreciated!

1 Like

hey just create a file called fan.yaml in ur config dir and in ur configuration.yaml put:
fan: !include fan.yaml

Hi @pedzp14 Thanks for the quick reply! It’s working now.
Keep up the good work!

Hi guys,

please help me.

I have Qubino dimmer 0-10V. I dont know, how to edit code.
The switch ON / OFF works correct.
But speed of fan doesnt work.

I am using qubino as controler Zehnder Q350 with Option box.

When i am using as light speed of fan works correct, but in homekit show dimmer light.

2 Likes

Im lost! I have tried this I feel like a million ways. I’m sure its something simple i am over looking here.

I have a Leviton wifi fan controller that shows up as a light and not a fan. so i tried to wrap fan config around it but I can only turn on/off the fan I cannot set any speed using the brightness. If I change the brightness of the light it does work but trying to change it on the fan entity fails.

this is my current config:

fan:
  - platform: template
    fans:
      bedroom:
        friendly_name: "Bedroom Fan"
        value_template: "{{ states('light.bedroom_fan') }}"
        speed_template: >
          {% set output = {0: 'off', 50: 'low', 128: 'medium', 255: 'high'} %}
          {% set idx = state_attr('light.bedroom_fan', 'brightness') %}
          {{ output[idx] }}
        turn_on:
          service: homeassistant.turn_on
          entity_id: light.bedroom_fan
        turn_off:
          service: homeassistant.turn_off
          entity_id: light.bedroom_fan
        set_speed:
          service: light.turn_on
          entity_id: light.bedroom_fan
          data_template:
            brightness: >
              {% set mapper = {'off' : 0, 'low': 50, 'medium': 128, 'high': 255} %}
              {{ mapper[speed] }}
        speeds:
          - 'off'
          - 'low'
          - 'medium'
          - 'high'

You’re lost because this thread is 2 years old and there have been changes to template fan. set_speed and speeds has been deprecated. You should use the examples in the documentation page.

You have a choice, use set_preset_modes with a list of presets or set_percentage with percentages 0, 33, 66, 100 mapped to light brightness.

Hi Petro, documentation page for what? Fans??

I was replying to the guy who was using a template, so it would be the template fan docs

1 Like

Thank you !

did you get this figured out? i am looking at the fan template page and see the changes from set_speed to set_percentage… tried changing the word speed to percentage in my config… it did not work… there is mroe to change i dunno what.

fan:
  -  platform: template
     fans:
      bath_fan_2:
        friendly_name: "Bath Fan"
        unique_id: 3dkhfk644vdg
        value_template: "{{ states('light.bath_fan') }}"
        percentage_template: >
          {% set output = {0: 'off', 50: 'low', 128: 'medium', 255: 'high'} %}
          {% set idx = state_attr('light.bath_fan', 'brightness') %}
          {{ output[idx] }}
        turn_on:
          service: homeassistant.turn_on
          entity_id: light.bath_fan
        turn_off:
          service: homeassistant.turn_off
          entity_id: light.bath_fan
        set_percentage:
          service: light.turn_on
          entity_id: light.bath_fan
          data_template:
            brightness: >
              {% set mapper = {'off' : 0, 'low': 50, 'medium': 128, 'high': 255} %}
              {{ mapper[speed] }}
        speed_count: 3

sorry man, I did not have any success, seems like it should be easier but I am lost

I am also here from the year 2022 trying to use the latest version of HA and not having much luck. It looks like low/med/high speeds are automatically calculated but I’m not even messing with that, I just want to convert to/from the number values. I think my fan entity is converting from the switch’s 255 value to the fan’s percentage correctly but when I set the percentage on the fan it doesn’t update my switch.

Edit: This works!

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room Fan"
        value_template: "{{ states('light.living_room_fan_switch') }}"
        percentage_template: >
          {{ (state_attr('light.living_room_fan_switch', 'brightness') / 255 * 100) | int }}
        turn_on:
          service: homeassistant.turn_on
          entity_id: light.living_room_fan_switch
        turn_off:
          service: homeassistant.turn_off
          entity_id: light.living_room_fan_switch
        set_percentage:
          service: light.turn_on
          entity_id: light.living_room_fan_switch
          data_template:
            brightness: >
              {{ ( percentage / 100 * 255) | int }} 
        speed_count: 3 
1 Like

data_template is deprecated, set percentage passes a percentage variable when used. And 255 is already an int. You need to apply int to the entire expression.

        set_percentage:
          service: light.turn_on
          entity_id: light.living_room_fan_switch
          data:
            brightness: >
              {{ (percentage / 100 * 255) | int }} 
1 Like