Multi-switch fan into one fan entity

Hi,

So I recently had some zigbee light and fan switches installed in my walls, and they are working fantastically (basically they are just relays replacing the normal switches that are networked via zigbee), the fan switches however are three separate entities (when integrating with HA, I changed the switch device type to fan, but the switch entities exist in HA too).

  • One for low - fan.bedroom_fan_low
  • One for medium - fan.bedroom_fan_medium
  • One for high - fan.bedroom_fan_high

I want to somehow make a new “parent fan” that groups these together, so I can leverage things like increasing speed and decreasing speed through automation.

After doing some research, I found the template fan docs,

which triggered a thought, I need almost a “virtual fan”, that has multiple speed presets, that when updated, passees the changes across to the real-world counterpart.

and from that, created this

fan:
  - platform: template
    fans:
      bedroom_fan_master:
        friendly_name: "Bedroom Fan Master"
        value_template: "{{ states('fan.percentage_fan') }}"
        turn_on:
          - service: fan.turn_on
            target:
              entity_id: fan.percentage_fan
        turn_off:
          - service: fan.turn_off
            target:
              entity_id: fan.percentage_fan
        percentage_template: >
          {{ state_attr('fan.percentage_fan', 'percentage') }}
        speed_count: 3
        set_percentage:
          - service: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: "{{ percentage }}"
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
        preset_mode_template: >
          {% if is_state('fan.percentage_fan', 'on') %}
            {% if state_attr('fan.percentage_fan', 'percentage') == 100  %}
              high
            {% elif state_attr('fan.percentage_fan', 'percentage') == 66 %}
              medium
            {% else %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}
        set_preset_mode:
          - service: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: >-
                {% if preset_mode == 'high' %}
                  100
                {% elif preset_mode == 'medium' %}
                  66
                {% elif preset_mode == 'low' %}
                  33
                {% else %}
                  0
                {% endif %}

And then created an automation that looks for a state change on bedroom_master_fan entity, which in turn, calls a script that checks the speed being set on bedroom_master_fan and turns on or off the appropriate switch for the physical fan. But the state of the bedroom_master_fan never seems to actually update when I include it in a dashboard with a fan card and interact with it, therefore, the automation is never being triggered.

Does anyone see where I am going wrong? or have any idea how to make a “multi-switch” fan entity? I believe reading other forum posts, in older versions of HA, before fan percentages were the default, speeds were… so I am sure I am just missing something super obvious.

what is fan.percentage_fan ? is that a real existing entity in your system? or did that come from the example in the docs?

the docs example is one that shows how to wrap an existing fan (named fan.percentage_fan that only has percentages into one that has modes.

and the actual work to change the subsequent actual fan should be done (in your case) in set_preset_mode. not in a separate automation.

i think you may have the incorrect mental model for how this all should work. but before i make too many assumptions, familiarity w/ coding? do you know/understand the code posted?

btw… if your fan has 3 separate switches that turn the real fan to low/med/high, then i think you are best of leaving them as switches. they are not 3 fans. there’s only 1 fan, with 3 switches. i presume, as switches, each one has an “on/off” state. and only 1 is “on” at any one time, and they are all “off” if the physical fan is off, is that right? please clarify. if my assumptions are right, it’s not too hard to solve this…

Hi Armed, your assumption is 100% correct, so the wallmounted control has 3 switches, when one is on, the other two turn off, and each has a unique on / off state, if all 3 are off, the fan is off.

Familiarity with coding, I have done some front-end web work in javascript and PHP, so I understand concepts like OOP, functions, variables, logic statements etc. (although I am probably a bit rusty), I am Not super familiar with YAML and HA modifications, so I think you’re spot on, I am misunderstanding the example.

fan.percentage_fan that you mention, is straight from the example, I assumed this was either a “this” style self-referencing statement or referencing some preset fan model. It’s not an entity I have created in my instance.

ah ok. so i’ll try to hit the right knowledge level. apologies if i’m off the mark.

the fan.percentage_fan that you have in your example is not a “this” entity. so that won’t work, and youre value_template is likely “unknown” as a result. in the documentation sample it’s using that as an example assuming you have a fan that reports only percentages and it’s showing how you can create preset_modes from it.

so in your case, i think something more like this is right… note that this is free hand coded,… ie, i didn’t test it. so i’m sure i have a few issues here and there. but since you’re generally familiar with coding, i’m hoping this will get you in the right direction. please read it to understand the approach. hit me with whatever questions you’ve got.

i’m making a bunch of assumptions that might or might not be right. e.g. if i turn on one of the 3 low/med/high switches, it automatically turns the others off … if it was low, and i turn on medium, i’m assuming low turns off automatically… i’m assuming to turn them all off, i have to turn off all 3… there’s no single “off” switch.

fan:
  - platform: template
    fans:
      bedroom_fan_master:
        friendly_name: Bedroom Fan Master
        value_template: >-
          {{ states('switch.bedroom_fan_low') or
          states('switch.bedroom_fan_medium') or
          states('switch.bedroom_fan_high') }}
        turn_on:
          - service: switch.turn_on
            target:
              entity_id: switch.bedroom_fan_medium
        turn_off:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.bedroom_fan_low
                - switch.bedroom_fan_medium
                - switch.bedroom_fan_high
        percentage_template: |
          {% if is_state('switch.bedroom_fan_low', 'on') %}
            33
          {% elif is_state('switch.bedroom_fan_medium', 'on') %}
            66
          {% elif is_state('switch.bedroom_fan_high', 'on') %}
            100  
          {% else %}
            0
          {% endif %}
        speed_count: 3
        preset_modes:
          - 'off'
          - 'low'
          - 'medium'
          - 'high'
        preset_mode_template: |
          {% if is_state('switch.bedroom_fan_low', 'on') %}
            low
          {% elif is_state('switch.bedroom_fan_medium', 'on') %}
            medium
          {% elif is_state('switch.bedroom_fan_hight', 'on') %}
            high  
          {% else %}
            off
          {% endif %}
        set_preset_mode:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'low' }}"
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id: swtich.bedroom_fan_low
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'medium' }}"
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id: swtich.bedroom_fan_medium
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'high' }}"
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id: swtich.bedroom_fan_high
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'off' }}"
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id:
                        - switch.bedroom_fan_low
                        - switch.bedroom_fan_medium
                        - switch.bedroom_fan_high