4 Speed Template Fan Possible?

I have a Hampton Bay 99432 Zigbee fan controller with 4 speeds (low, medium, high, on). However the preset modes are not being exposed properly from Z2M to HA. So I thought a template fan would do the trick to transform percentages into modes to put on a dashboard.

Can we not make a 4 speed template fan? I took the PRESETS MODE TEMPLATE from the template fan documentation and altered it to rename and add a 4th speed. But I get errors when I check my configuration.

fan:
  - platform: template
    fans:
      preset_mode_fan:
        friendly_name: "Girl's Ceiling Fan Modes"
        value_template: "{{ states('fan.girl_s_ceiling_fan_light') }}"
        turn_on:
          - action: fan.turn_on
            target:
              entity_id: fan.girl_s_ceiling_fan_light
        turn_off:
          - action: fan.turn_off
            target:
              entity_id: fan.girl_s_ceiling_fan_light
        percentage_template: >
          {{ state_attr('fan.girl_s_ceiling_fan_light', 'percentage') }}
        speed_count: 4
        set_percentage:
          - action: fan.set_percentage
            target:
              entity_id: fan.girl_s_ceiling_fan_light
            data:
              percentage: "{{ percentage }}"
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
          - "max"
        preset_mode_template: >
          {% if is_state('fan.girl_s_ceiling_fan_light', 'on') %}
            {% if state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 100  %}
              max
            {% if state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 75  %}
              high
            {% elif state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 50 %}
              medium
            {% elif state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 25 %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}
        set_preset_mode:
          - action: fan.set_percentage
            target:
              entity_id: fan.girl_s_ceiling_fan_light
            data:
              percentage: >-
                {% if preset_mode == 'max' %}
                  100
                {% if preset_mode == 'high' %}
                  75
                {% elif preset_mode == 'medium' %}
                  50
                {% elif preset_mode == 'low' %}
                  25
                {% else %}
                  0
                {% endif %}

The error messages are due to two places where you used if instead of elif.

Here:

{% if state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 75  %}

and here:

{% if preset_mode == 'high' %}

Here’s the corrected version (it passes config check on my system).

fan:
  - platform: template
    fans:
      preset_mode_fan:
        friendly_name: "Girl's Ceiling Fan Modes"
        value_template: "{{ states('fan.girl_s_ceiling_fan_light') }}"
        turn_on:
          - action: fan.turn_on
            target:
              entity_id: fan.girl_s_ceiling_fan_light
        turn_off:
          - action: fan.turn_off
            target:
              entity_id: fan.girl_s_ceiling_fan_light
        percentage_template: >
          {{ state_attr('fan.girl_s_ceiling_fan_light', 'percentage') }}
        speed_count: 4
        set_percentage:
          - action: fan.set_percentage
            target:
              entity_id: fan.girl_s_ceiling_fan_light
            data:
              percentage: "{{ percentage }}"
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
          - "max"
        preset_mode_template: >
          {% if is_state('fan.girl_s_ceiling_fan_light', 'on') %}
            {% if state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 100  %}
              max
            {% elif state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 75  %}
              high
            {% elif state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 50 %}
              medium
            {% elif state_attr('fan.girl_s_ceiling_fan_light', 'percentage') == 25 %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}
        set_preset_mode:
          - action: fan.set_percentage
            target:
              entity_id: fan.girl_s_ceiling_fan_light
            data:
              percentage: >-
                {% if preset_mode == 'max' %}
                  100
                {% elif preset_mode == 'high' %}
                  75
                {% elif preset_mode == 'medium' %}
                  50
                {% elif preset_mode == 'low' %}
                  25
                {% else %}
                  0
                {% endif %}

Thank you! I figured it was something simple.

However, I was hoping that it would create fan speed buttons instead of a slider with a select menu. I want it to be like the top Ceiling Fan example, not the bottom one.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.


Which card is being used by the two examples?

The new tile card. I just figured out you can change the preset modes from a select dropdown to icons, but I’m not sure where to specify what icons to use for each mode. Right now they just show up as dots for every mode. Although they do work.

I get the same result as you; buttons with no icons.

Looking into it …

Based on the documentation, it gives the impression that the icons are automatically assigned (but it’s not happening for you or me).

Fan Preset Modes

The only time I see “low”, “medium”, etc is when the mouse pointer hovers over the button.

Please post the YAML code for the top card. It may help to explain its appearance.

The top card is just a normal tile card with a different fan selected that already supports preset modes.

features:
  - type: fan-speed
type: tile
entity: fan.mbr_ceiling_fan
features_position: bottom
vertical: false
name: Ceiling Fan
grid_options:
  columns: 9
  rows: 2
hide_state: true

Which integration produced fan.mbr_ceiling_fan?

Is it Zigbee like (I assume) fan.girl_s_ceiling_fan_light?

Just trying to pin down if the Tile card works better with fan entities based on certain integrations (Zigbee) compared to others (Template Fan).

fan.mbr_ceiling_fan Is connected via the bond integration.

fan.girl_s_ceiling_fan_light Is a Z2M zigbee device.

Another data point. If I use the example from the template fans page that has 3 speeds instead of 4, the icons appear. So something about deviating from 3 speeds in the template causes this issue.

I suggest you submit it as an Issue in Home Assistant’s Github Frontend repository for the Tile card. Post a simplified Template Fan configuration with four modes and a screenshot of a Tile card showing the resulting 4 buttons having no icons. You may also wish to include the link to the Fan Preset Modes documentation which clearly shows a Tile card displaying 4 modes and 4 icons.

I submitted this issue. Thanks for your help.

1 Like