Template fan driving me crazy

I was trying to integrate a mechanical ventilation using this Mechanische ventilatie in Home Assistant · GitHub and this GitHub - eelcohn/nRF905-API: API webinterface for the nRF905

Since the set preset mode didn’t work as I expected I tried from scratch but even a simple dummy template fan does not work as I expect. So either I don’t fully understand how it is supposed to work or the thing is buggy.

“Dummy” fan:

fan:
  - platform: template
    fans:
      simple_fan:
        friendly_name: Simple Fan
        value_template: >
          on
        availability_template: >
          on
        percentage_template: >
          {{ states('input_number.simple_fan_percentage') | int }}
        preset_mode_template: >
          {{ states('input_text.simple_fan_preset_mode') }}
        turn_on:
          service: script.ventilation_turn_on_off_dummy
        turn_off:
          service: script.ventilation_turn_on_off_dummy
        set_percentage:
          service: input_number.set_value
          data:
            value: '{{ percentage }}'
          target:
            entity_id: input_number.simple_fan_percentage
        set_preset_mode:
          service: input_text.set_value
          data:
            value: '{{ preset_mode }}'
          target:
            entity_id: input_text.simple_fan_preset_mode
        preset_modes:
          - Stand 1
          - Stand 2
          - Stand 3
          - Stand 4
          - Unknown

With 2 helpers:

  • input_number.simple_fan_percentage
  • input_text.simple_fan_preset_mode

And the script.ventilation_turn_on_off_dummy is just a delay

ventilation_turn_on_off_dummy:
  mode: single
  sequence:
    - delay: 1

Setting input_number.simple_fan_percentage to 10 and input_text.simple_fan_preset_mode to Stand 1, then restarting Home Assistant to have the template fan initialise and read these values, the template fan has following state:

speed_list:
  - 'off'
  - low
  - medium
  - high
preset_modes:
  - Stand 1
  - Stand 2
  - Stand 3
  - Stand 4
  - Unknown
speed: low
percentage: 10
percentage_step: 1
preset_mode: null
friendly_name: Simple Fan
supported_features: 1

It does NOT read the preset_mode??? Why, why, why…??? Upon initialization the fan CAN have both a preset activated and an associated percentage set.

If I then set the preset mode of the fan (via the lovelace gui) to Stand 2 this is the state:

speed_list:
  - 'off'
  - low
  - medium
  - high
preset_modes:
  - Stand 1
  - Stand 2
  - Stand 3
  - Stand 4
  - Unknown
speed: Stand 2
percentage: null
percentage_step: 1
preset_mode: Stand 2
friendly_name: Simple Fan
supported_features: 1

The behaviour is the same if I pick any other of the presets

Why is it also updating speed to Stand 2?
Why is it setting percentage to null? I expect it to either don’t touch it, or at least simply re-read it from the defined percentage_template. As said above, a preset_mode CAN (WILL in my case) have an associated percentage.

When I change the percentage, the preset_mode goes to null and the speed gets a speed out of the speed list. This behaviour I understand. E.g. setting percentage to 80 results in state:

speed_list:
  - 'off'
  - low
  - medium
  - high
preset_modes:
  - Stand 1
  - Stand 2
  - Stand 3
  - Stand 4
  - Unknown
speed: high
percentage: 80
percentage_step: 1
preset_mode: null
friendly_name: Simple Fan
supported_features: 1

But how do I prevent the template fan to decide for itself to set percentage to null when picking a preset_mode instead of reading that from a helper (a sensor attribute in a real world situation)?

I understand Just setting a percentage does not correspond with any preset_mode.
However, setting a preset_mode, may (in my case WILL) correspond with a certain percentage, so percentage must NOT go to null… Similarly as with the initialization discussed above.

@spudje

Were you able to get it working?

More or less yes, don’t remember exactly how anymore though, been a while.

Running into the exact same issue where changing the preset mode from the UI also nullifies the percentage…
This seems like a completely intentional behavior, see core/fan.py at 802adaf43c0d9ed344363b9b6bb6ee2e6234f7b4 · home-assistant/core · GitHub

but it did take me a while to realize it is “a feature not a bug”