Combine two switches into one fan with high/low/off on dashboard (again)

I already found out Combine two switches into one fan with high/low/off on dashboard and my problem is similar yet not equal.

My two switches control the fan differently as in the given example, but also I do not yet understand how to go from two switch entities towards the fan-mode-button-row. Do I have to create a template or can I do without?

Here is how my two switches work, and I have three of these fans (and six relais of course):

Switch 1 Switch 2 Fan Mode
Off Off Off
Off On Slow
On Off Fast
On On Fast

I decided to go via a template in configuration.yaml. As a start I simply copied the code for the state based fan from Template - Home Assistant

The UI for the helper looks exactly what I was striving for. Now I need to customize this for only two speeds and make it actually switch my switches.

I seem to fail managing state and connecting my switches. Here is the code I have:

template:                                                        
  - fan:                                                         
      - name: "fan_arbeitszimmer"                                
        state: "{{ states('fan.fan_arbeitszimmer') }}"           
        turn_on:                                                 
          - action: fan.turn_on                                  
            target:                                              
              entity_id: fan.fan_arbeitszimmer                                                                                                  
        turn_off:                                                                                                                               
          - action: fan.turn_off                                                                                                                
            target:                                                                                                                             
              entity_id: fan.fan_arbeitszimmer                                                                                                  
        percentage: >                                                                                                                           
          {{ state_attr('fan.fan_arbeitszimmer', 'percentage') }}                                                                               
        speed_count: 2                                           
        set_percentage:                                          
          - action: fan.set_percentage                           
            target:                                              
              entity_id: fan.fan_arbeitszimmer                   
            data:                                                
              percentage: "{{ percentage }}"                     
          - choose:                                              
              - conditions:                                      
                  - condition: template                          
                    value_template: "{{ is_state('fan.fan_arbeitszimmer', 'on') and state_attr('fan.fan_arbeitszimmer', 'percentage') == 100 }}"
                sequence:                                                                                                                       
                  - service: switch.turn_on                                                                                                     
                    target:                                                                                                                     
                      entity_id: switch.treppenhaus_hmip_drsi4_0025a409a1fbd1_lufter_manisha_schnell                                            
              - conditions:                                                                                                                     
                  - condition: template                                                                                                         
                    value_template: "{{ is_state('fan.fan_arbeitszimmer', 'on') and state_attr('fan.fan_arbeitszimmer', 'percentage') == 50 }}" 
                sequence:                                                                                                                       
                  - service: switch.turn_off                                                                                                    
                    target:                                                                                                                     
                      entity_id: switch.treppenhaus_hmip_drsi4_0025a409a1fbd1_lufter_manisha_schnell                                            
                  - service: switch.turn_on                                                                                                     
                    target:                                                                                                                     
                      entity_id: switch.treppenhaus_hmip_drsi4_0025a409a1fbd1_lufter_manisha_langsam                                            
              - conditions:                                                                                                                     
                  - condition: template                                                                                                         
                    value_template: "{{ not is_state('fan.fan_arbeitszimmer', 'on') or state_attr('fan.fan_arbeitszimmer', 'percentage') == 0 }}"
                sequence:                                                                                                                        
                  - service: switch.turn_off                                                                                                     
                    target:                                                                                                                      
                      entity_id:                                                                                                                 
                        - switch.treppenhaus_hmip_drsi4_0025a409a1fbd1_lufter_manisha_schnell                                                    
                        - switch.treppenhaus_hmip_drsi4_0025a409a1fbd1_lufter_manisha_langsam                                                    
        preset_modes:                                                                                                                            
          - "off"                                                                                                                                
          - "low"                                                                                                                                
          - "high"                                                                                                                               
        preset_mode: >                                                                                                                           
          {% if is_state('fan.fan_arbeitszimmer', 'on') %}                                                                                       
            {% if state_attr('fan.fan_arbeitszimmer', 'percentage') == 100  %}                                                                   
              high                                                                                                                               
            {% else %}                                                                                                                           
              low                                                                                                                                
            {% endif %}                                                                                                                          
          {% else %}                                                                                                                             
            off                                                                                                                                  
          {% endif %}                                                                                                                            
        set_preset_mode:                                                                                                                         
          - action: fan.set_percentage                                                                                                           
            target:                                                                                                                              
              entity_id: fan.fan_arbeitszimmer                                                                                                   
            data:                                                                                                                                
              percentage: >-                                                                                                                     
                {% if preset_mode == 'high' %}                                                                                                   
                  100                                                                                                                            
                {% elif preset_mode == 'low' %}                                                                                                  
                  50                                                                                                                             
                {% else %}                                                                                                                       
                  0                                                                                                                              
                {% endif %}                                                                          

In a dashboard I have a card with three buttons, and I can click it and even see the presets in the popup. The UI reacts by hilighting the correct button, but the switches only get turned off.

When looking at Settings/Tools/States, it is ‘unknown’ and the attributes are

preset_modes:
  - 'off'
  - low
  - high
percentage: 0
percentage_step: 50
preset_mode: 'off'
friendly_name: fan_arbeitszimmer
supported_features: 57

These values never change when I click the UI. What may be missing?

Finally I found out what needs to be done. Maybe this post helps others, or the future me.

I installed a fresh HomeAssistant and simulated the switches I have. This alone took some effort, as just creating switches did not really work out. I failed setting their state. So I created to drop down lists that manage their state. Then I connected two switches to control the drop down lists to show it in the dashboard like my current switches.

After that I created the fan template to also manage the drop down lists, and all of a sudden everything clicked into place. So here is my final result on a clean HomeAssistant, which means anyone should be able to reproduce the result.


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - fan:
      - name: "two-power-fan"
        state: "{{ is_state('input_select.relais_low', 'on') or is_state('input_select.relais_high', 'on') }}"
        speed_count: 2
        percentage: >
          {% if is_state('input_select.relais_high', 'on') %}
            100
          {% else %}                                                                                                                           
            {% if  is_state('input_select.relais_low', 'on') %}                                                                   
              50
            {% else %}                                                                                                                           
              0                                                                                                                                
            {% endif %}                                                                                                                          
          {% endif %}     
        turn_on:
          - action: input_select.select_option
            target:
              entity_id: input_select.relais_low
            data:
              option: "on"
        turn_off:
          - action: input_select.select_option
            target:
              entity_id: 
                - input_select.relais_low
                - input_select.relais_high
            data:
              option: "off"
        set_percentage:
          - choose:                                              
             - conditions:                                      
                 - condition: template                          
                   value_template: "{{ percentage > 75 }}"
               sequence:
                 - action: notify.notify
                   data:
                     message: "Set fan to high"
                 - action: input_select.select_option
                   target:
                     entity_id: 
                       - input_select.relais_low
                       - input_select.relais_high
                   data:
                     option: "on"
             - conditions: 
                 - condition: template 
                   value_template: "{{ percentage > 25 and percentage <= 75 }}"
               sequence: 
                 - action: input_select.select_option
                   target:
                     entity_id:
                       - input_select.relais_low
                   data:
                     option: "on"
                 - action: input_select.select_option
                   target:
                     entity_id:
                       - input_select.relais_high
                   data:
                     option: "off"
             - conditions: 
                 - condition: template 
                   value_template: "{{ percentage <= 25 }}"
               sequence: 
                 - action: input_select.select_option
                   target:
                     entity_id:
                       - input_select.relais_low
                       - input_select.relais_high
                   data:
                     option: "off"
  - switch:
      - name: switch_low
        state: "{{ is_state('input_select.relais_low', 'on') }}"
        turn_on:
          - action: input_select.select_option
            target:
              entity_id: input_select.relais_low
            data:
              option: "on"
        turn_off:
          - action: input_select.select_option
            target:
              entity_id: input_select.relais_low
            data:
              option: "off"
      - name: switch_high
        state: "{{ is_state('input_select.relais_high', 'on') }}"
        turn_on:
          - action: input_select.select_option
            target:
              entity_id: input_select.relais_high
            data:
              option: "on"
        turn_off:
          - action: input_select.select_option
            target:
              entity_id: input_select.relais_high
            data:
              option: "off"

input_select:
  relais_high:
    name: "relais_high"
    options:
      - "on"
      - "off"
    initial: "off"
    icon: mdi:fan-speed-2
  relais_low:
    name: "relais_low"
    options:
      - "on"
      - "off"
    initial: "off"
    icon: mdi:fan-speed-1

I created a dashboard to visualize all these helpers: