Need help with fan template with the new options

I have a lutron caseta plug that is technically a light but it’s dimmable. I had my little window box fan working perfectly with it until all the changes, I can get it to turn on and off, but any preset calls or percentage calls do nothing. Attaching my code snippet. I would be grateful for help. Ignore the rem’d out lines, I left them behind so I could use them for reference when making the new sections.

fan:  
  - platform: template
    fans:
      jrs_bedroom_window_fan:
        friendly_name: "JR's Bedroom Window Fan"
        value_template: "{{ states('light.jr_s_bedroom_window_fan') }}"
        # speed_template: "{% if is_state('light.jr_s_bedroom_window_fan', 'on') %}high{% else %}low{% endif %}"
        preset_mode_template: "{% if is_state('light.jr_s_bedroom_window_fan', 'on') %}High{% else %}Low{% endif %}"
        turn_on:
          service: light.turn_on
          data:
            entity_id: light.jr_s_bedroom_window_fan
            brightness: 255
        turn_off:
          service: light.turn_off
          data:
            entity_id: light.jr_s_bedroom_window_fan
        set_preset_mode:
          service: light.turn_on
          entity_id: light.jr_s_bedroom_window_fan
          data_template:
            brightness: >
              {% set mapper = {'Off': 0, 'Low': 85, 'Medium': 170, 'High': 255} %}
              {{ mapper[speed] }}
        # set_speed:
        #   service: light.turn_on
        #   entity_id: light.jr_s_bedroom_window_fan
        #   data_template:
        #     brightness: >
        #       {% set mapper = {'off': 0, 'low': 85, 'medium': 170, 'high': 255} %}
        #       {{ mapper[speed] }}
        speed_count: 4
        # speeds:
        #   - 'off'
        #   - 'low'
        #   - 'medium'
        #   - 'high'
        preset_modes:
          - 'High'
          - 'Medium'
          - 'Low'

following along…

I understand things have to adapt, but I cant stand when they dont provide examples of the new methods.

With some help from a forum person I got it working only bug is when I turn it off I get an error in the log but it all works.

fan:  
  - platform: template
    fans:
      jrs_bedroom_window_fan:
        friendly_name: "JR's Bedroom Window Fan"
        value_template: "{{ states('light.jr_s_bedroom_window_fan') }}"
        speed_count: 3
        percentage_template: "{{ (state_attr('light.jr_s_bedroom_window_fan','brightness')/255*100) }}"
        turn_on:
          service: light.turn_on
          entity_id: light.jr_s_bedroom_window_fan
        turn_off:
          service: light.turn_off
          entity_id: light.jr_s_bedroom_window_fan
        set_percentage:
          service: light.turn_on
          entity_id: light.jr_s_bedroom_window_fan
          data_template:
            brightness_pct: "{{percentage}}"
1 Like

Thanks for sharing. I was stuck trying to convert my Inovelli LZW36 Fan/Dimmer switch to the new format and this works perfect for that device. I am also getting errors, I checked the {{ (state_attr('light.inovelli_lzw36_fan_dimmer','brightness')/255*100)}} using Developer Tools->Template and the three speeds didn’t round out but instead would be 33.33333333333333, 65.49019607843137 and 100. I filtered it through | int and it’s rounding the numbers out to 33, 65 and 100 but still throwing errors in the log.

The full line looks like this now:

percentage_template: “{{ (state_attr(‘light.inovelli_lzw36_fan_dimmer’,‘brightness’)/255*100) | int}}”

Hopefully someone else can chime in on what’s missing. Here’s the error:

TemplateError(‘TypeError: unsupported operand type(s) for /: ‘NoneType’ and ‘int’’) while processing template ‘Template(“{{ (state_attr(‘light.inovelli_lzw36_fan_dimmer’,‘brightness’)/255*100) | int}}”)’ for attribute ‘_percentage’ in entity ‘fan.living_room_fan’

Hey CG,

I also have many LZW36’s, would you mind tagging me once you get something working great? For now I just swapped back over to the default light entity.

And/or post up in the inovelli forums, I think that would be a great post over there.

This is working 100% functionally speaking it’s just the error in the log. It doesn’t seem to stop any function.

It looks like the error is occurring due to the off state, Dev Tools->Template gives same error when the device is off. If the device is on everything passes (the 33, 67 and 100 values)

I posted on GitHub for help:

GitHub issue was closed citing not an error but was told

Brightness is not available as an attribute for lights when lights are off:
You can use default to help with that:
"{{ ((state_attr('light.inovelli_lzw36_fan_dimmer','brightness') | default(0)) / 255*100 ) | int }}"

unfortunately still getting the same error.

Did you get the 1% brightness mapped to “breeze mode” at all?

Yeah, not sure where to go from here, I mean I think it’s a bug, they say it’s not, but wont provide a valid config to prove that. I guess I’ll just live with the errors. I’m all for progress and moving forward but when things like this come up it’s frustrating.

@kreene1987 Sorry no I don’t use breeze mode.

@cybergrimes I just stumbled upon this thread. I just found out I am in the same bucket as you in regard to the lzw36 fan/light switch. If you figure out a solution please post it back here.

It’s really anyone who uses a fan template based off a light switch device sadly. I guess positive side is that it actually works 100% just the error in the logs is all.

1 Like

There’s a couple fan changes coming in 2021.3.3, not sure if they’ll make a difference

Change your template to:

percentage_template: "{{ (state_attr('light.inovelli_lzw36_fan_dimmer','brightness') | int /255*100) | int}}"

that should take care of it.

Also would you mind posting up your entire fan template code, please?

1 Like

@finity That’s great, thank you so much!
Here’s the full template with your change

      living_room_fan:
        friendly_name: "Living room fan"
        value_template: "{{ states('light.inovelli_lzw36_fan_dimmer') }}"
        speed_count: 3
        percentage_template: "{{ (state_attr('light.inovelli_lzw36_fan_dimmer','brightness') | int /255*100) | int}}"
        turn_on:
          service: light.turn_on
          entity_id: light.inovelli_lzw36_fan_dimmer
        turn_off:
          service: light.turn_off
          entity_id: light.inovelli_lzw36_fan_dimmer
        set_percentage:
          service: light.turn_on
          entity_id: light.inovelli_lzw36_fan_dimmer
          data_template:
            brightness_pct: "{{ percentage }}"
3 Likes

Thank you, that coding works perfectly! I really wish there was some explanation as to why/how things like this are needed. As someone who is not a coder by trade these things make it really hard to be an “enthusiast”. I’m not an idiot either, I’m a systems engineer by trade.

The biggest thing to remember is that all states are strings. And some attributes are strings. Even tho they could look like something else.

So it’s always a safe bet to explicitly do the conversion to what you expect it to be.

but also the error gave you a hint of what the problem was:

unsupported operand type(s) for /: ‘NoneType’ and ‘int’

that means that you are trying to do a math operation (division) on two operands that don’t support it because one is an ‘int’ and the other one has no data type.

I just opened a PR to update the docs to clarify a few things

When converting a fan with 3 speeds from the old fan entity model, the following percentages can be used, the following percentages can be used:

0 - off
33 - low
66 - medium
100 - high

Would someone way more experienced than me assist with adding a 1% = breeze mode as a 4th speed (off, breeze, low, medium, high)?

I can make an attempt at it but am still learning templating.

You’re seeing 66? Mine is converting to 0, 33, 67 and 100