Changing domain from light. to fan

Good day all.

I have an Aeotec Nano Dimmer, which I’ve connected to a ceiling fan. Everything works as expected except for the fact that it registers it as a light.

I’ve tried the following configuration in an attempt to “convert” this into the fan domain:

fan:
  - platform: template
    fans:
      dining_room_fan:
        friendly_name: "Dining Room Fan"
        value_template: "{{ states('light.dining_room_fan_level') }}"
        speed_template: "{{ state_attr('light.dining_room_fan_level', 'brightness') // 85 }}"
        turn_on:
          service: light.turn_on
          entity_id: light.dining_room_fan_level
        turn_off:
          service: light.turn_off
          entity_id: light.dining_room_fan_level
        set_speed:
          service: light.turn_on
          entity_id: light.dining_room_fan_level
          data_template:
            brightness: "{{ speed * 85 }}"
        speeds:
          - 1
          - 2
          - 3

I have 2 issues with this configuration and hope someone here could perhaps point out what I am doing wrong.

  1. I am unable to set the speed (brightness), Doesn’t matter which speed I select, it always sets the “brightness” to 255
  2. The on and off toggle works, however, when I restart HA, it is in the off position. Toggling it will turn it in the ‘on’ position, trying to switch it off, however, the fan itself (and light.dining_room_fan_level) goes to ‘off’ but the fan template toggle automatically toggles back in the on position. (even though physically everything is switched off), and due to this, I cannot switch it back on with the fan template toggle anymore. (That’s a mouth full, hope I explained it well enough)

The reason I want to do this is that all other integrations where I use domain level automation messes with the fan (INcluding google home that switches the fan off when I only want to switch off all lights).

Got it working. Seems over-engineered now though :confused:

fan:
  - platform: template
    fans:
      dining_room_fan:
        friendly_name: "Dining Room Fan"
        value_template: "{{ states('switch.dining_room_fan_switch') }}"
        speed_template: >
          {% if (state_attr('light.dining_room_fan_level', 'brightness') // 85) == 1 %}
            {{'Low'}}
          {% endif %}
          {% if (state_attr('light.dining_room_fan_level', 'brightness') // 85) == 2 %}
            {{'Medium'}}
          {% endif %}
          {% if (state_attr('light.dining_room_fan_level', 'brightness') // 85) == 3 %}
            {{'High'}}
          {% endif %}
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.dining_room_fan_switch
        turn_off:
          service: homeassistant.turn_off
          entity_id: switch.dining_room_fan_switch
        set_speed:
          service: light.turn_on
          entity_id: light.dining_room_fan_level
          data_template:
            brightness: >
              {% if speed == 'Low' %}
                {{'85'}}
              {% endif %}
              {% if speed == 'Medium' %}
                {{'170'}}
              {% endif %}
              {% if speed == 'High' %}
                {{'255'}}
              {% endif %}
        speeds:
          - 'Low'
          - 'Medium'
          - 'High'
1 Like

ing
Yeah, it seems over engineered, but it’s definitely correct because you are converting your light to a fan. If you were interested, here’s a different approach with the same outcome:

fan:
  - platform: template
    fans:
      dining_room_fan:
        friendly_name: "Dining Room Fan"
        value_template: "{{ states('switch.dining_room_fan_switch') }}"
        speed_template: >
          {% set output = ['Low','Medium','High'] %}
          {% set idx = state_attr('light.dining_room_fan_level', 'brightness') | int // 85 - 1 %}
          {{ output[idx] }}
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.dining_room_fan_switch
        turn_off:
          service: homeassistant.turn_off
          entity_id: switch.dining_room_fan_switch
        set_speed:
          service: light.turn_on
          entity_id: light.dining_room_fan_level
          data_template:
            brightness: >
              {% set mapper = {'Low': 85, 'Medium': 170, 'High': 255} %}
              {{ mapper[speed] }}
        speeds:
          - 'Low'
          - 'Medium'
          - 'High'
3 Likes

Awesome, didn’t know I could

  1. Type cast (to int), new to jinja
  2. Create a Lookup array
    This looks (and reads) much better now and working like a charm.

P.S. I changed from

{% mapper = {'Low': 85, 'Medium': 170, 'High': 255} %}

to

{% set mapper = {'Low': 85, 'Medium': 170, 'High': 255} %}
2 Likes

Yeah, my bad! I always forget set in at least 1 place. I made an edit to the post for future goers.

2 Likes

I am trying to do this exact same thing but I must be missing something obvious in the example… hoping for some help!

I recently moved my Z-wave devices off of the Aeotec Z-stick on HA and onto a Vera Plus connected into HA via the Vera component. Vera does not have a “fan” device type and as a result, my GE/Jasco fan switches are now showing as light entities in HA. For example, my ceiling fan is now light.bedroom_fan instead of fan.bedroom_fan.

In the example above, I’m seeing references to the light entity inside the fan template (that makes sense) but what’s up with the switch.dining_room_fan_switch in the example? It’s not in shark711’s first example but appears in the follow-up examples. How/where does that switch come into play?

Hope that makes sense…

the switch is what turns on and off this guys fan. and the light brightness is what determines his fan speed. He has an odd setup. In your case, the light state determines the on/off and the light brightness is the fan speed. So you’d have light.xxxx in both places.

1 Like

In my case I am using a z-wave dimmer (which is compatible with fans) to drive the fan. However, by default, HA thinks it’s a light switch. Once the fan speed is change it automatically switches on, however it doesn’t switch off (state) when speed is set to 0

2 Likes

A-ha, that makes total sense. It’s working beautifully now. Thank you both - hope this helps someone in the future!

@shark711 @petro

thank you for this
it has provided me fans instead of dimmers for my Jasco switches

1 thing i noticed is that

{% set output = ['Low','Medium','High'] %}
{% set idx = state_attr('light.dining_room_fan_level', 'brightness') | int // 85 - 1 %}
{ output[idx] }}

does not take into account that if the dimmer is at maximum 100% or 255
%by 85 has a result of 3
indexes start at 0

now i realize you are subtracting by 1
but this will be invalid
as the way fan dimmer modules work are that
the first 33% is low, then up to 66% medium, then up to 100% high
at 99% or 254 / 85 == 2.988 - the 1 is 1.98
this would set it to medium even though its at high

i tried leaving out the -1
this worked for all speeds up to 254
at 255 after -1 it gets a value of 3
but there are 3 indexes 0,1,2
so at max the speed did not reflect

also subtracting 1 before dividing works up to 100
but breaks on/off as when the dimmer is at 0 subtracting 1 is invalid because its not an int yet

there are a few options i tried that worked
assign a 4th index and also calling it ‘High’
{% set output = [‘Low’,‘Medium’,‘High’, ‘High’] %}
but i did not like the look of that

dividing by anything larger than 85 will work and not be as messy
even by a decimal point
i am currently dividing by 86 and this solves the issue at both ends

i suppose you could add an if statement that if its 255 divide by a different value
but this just complicates for no reason

the only other valid option i see is creating a 4th index called Max
and also adding it at the bottom to the speeds and possibly also an Off in the speeds

brightness: >
              {% set mapper = { 'Off': 0, 'Low': 85, 'Medium': 170, 'High': 255, 'Max': 255 } %}
              {{ mapper[speed] }}
        speeds:
          - 'Off'
          - 'Low'
          - 'Medium'
          - 'High'
          - 'Max'

What do you guys think?
I think best solution is simply dividing by 86 (34 if you are using brightness_PCT)

fan
  - platform: template
    fans:
      one1in:
        friendly_name: "one1in"
        value_template: "{{ states('light.60600011ecfabcab8f89') }}"
        speed_template: >
          {% set output = ['off','one','two','three','four','five','six','seven','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'
  

if you’re asking what’s wrong, your speed template should have a map that reverses this map.

{% set mapper = {‘off’: 0, ‘one’: 120, ‘two’: 140, ‘three’: 160, ‘four’: 180, ‘five’: 200, ‘six’: 220, ‘seven’: 240, ‘eight’: 255} %}

Then you’ll pull the word from the brightness level. i.e {{ mapper[idx] }}

actually isnt that what i have under set speed > brightness. how it is works flawlessly every other way ive tried it always produces a fault like misses a speed or on off command. this one hasnt skipped a beat and i tested between ios homekit, hass web, and hass ios app all at the same time and after controlling one albeit a few seconds late rthe rest follow to match

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