Template Fan for AC

I’m 100% positive what I said is why it does work. It’s very unlikely that it’s anything else. See the explanation below.

There’s nothing wrong with the integration. The templating is handled just like every other template field. Here are the 2 things wrong with your logic:

  1. You assume fan_mode returns an int. If it’s a string, those elif’s will never be hit. The likely hood of an attribute returning a string and an int based on the context is low. So changing those numbers to strings would probably work. This is why my template always works, I cast everything that’s not silence as an int. Which leads to #2
  2. You don’t have an else statement. So when 1 ultimately occurs the value gets set to nothing. I believe that defaults it to zero.

Here are the results after more testing:
1- preset_mode_template doesn’t get the value from the climate entity. It is null.
2- set_preset_mode does work but for some reason whenever you select a preset during the operation (like selecting Vertical) sets the percentage null. I don’t see any relation between them so why does setting preset to something affect the percentage?

Also for some reason, when you select a preset mode like Vertical or Off, preset_mode is reflected on the speed variable? Why?

speed_list: off, low, medium, high
preset_modes: Off, Vertical
speed: Vertical
percentage: null
percentage_step: 1
preset_mode: Vertical

I have been using home assistant for some time now, but this fan integration made me sweat.

1 Like

Have you found out why this doesn’t work? I’ve run into the same issue where preset_mode_template always sets preset_mode to null. The template works, so it’s not that.

what’s your template? It’s most definitely your template. That state is only pulled from the template and it will only be null if the template is wrong or the output does not match a preset mode.

Post your entire configuration for the template fan, not just the template.

I have similar issues with the fan_template
I think this thing is buggy or I don’t understand how it works.

preset_mode remains “null”

This is my template fan:

fan:
  - platform: template
    fans:
      mechanische_ventilatie:
        friendly_name: Mechanische ventilatie
        value_template: >
          {{ states('sensor.mechanische_ventilatie_status') }}
        availability_template: >
          {{ states('sensor.mechanische_ventilatie_status') }}
        percentage_template: >
          {{ state_attr('sensor.mechanische_ventilatie_status', 'percentage') }}
        preset_mode_template: >
          {{ state_attr('sensor.mechanische_ventilatie_status', 'preset') }}
        turn_on:
          service: script.ventilation_turn_on_off_dummy
        turn_off:
          service: script.ventilation_turn_on_off_dummy
        set_percentage:
          service: script.ventilation_set_voltage
          data:
            voltage: '{{ percentage / 10 }}'
        set_preset_mode:
          service: script.ventilation_set_preset_mode
          data:
            preset_mode: '{{ preset_mode }}'
        preset_modes:
          - 'lo'
          - 'med'
          - 'hi'
          - 'max'

Data looks like this:

speed_list:
  - 'off'
  - low
  - medium
  - high
preset_modes:
  - lo
  - med
  - hi
  - max
speed: medium
percentage: 35
percentage_step: 1
preset_mode: null
friendly_name: Mechanische ventilatie
supported_features: 1

Should we somehow do something with that “supported features” property?

I see quite some code in the template/fan.py doing the stuff we experience:

from line 305

    @property
    def preset_modes(self) -> list:
        """Get the list of available preset modes."""
        if self._preset_modes is not None:
            return self._preset_modes
        return preset_modes_from_speed_list(self._speed_list)

line 416:

self._speed = preset_mode

line 531:

 self._preset_mode = speed if speed in self.preset_modes else None

etc.

I also read this. Still I don’t understand what I’m doing wrong.

Deprecated Properties#

The fan entity model has changed to use percentages in the range from 0 (off) to 100 instead of the named speeds. The new model replaces speed and speed_list with percentage, preset_mode, and preset_modes. This change allowed us to expand the number of supported speeds to accommodate additional fan models in Home Assistant.

To maintain backwards compatibility with integrations that have not updated to the new model, the deprecated properties will remain until at least the end of 2021. Integrations must update their Turn on function to consume percentage or preset_mode instead of speed.

does sensor.mechanische_ventilatie_status have preset attribute? That’s the only reason why this would show up null.

To further clarify:

preset_mode_template is what populates the attribute preset_mode in the state object.
set_preset_mode is what is called when you change the preset mode via the dropdown/combobox or the service for the entity.
preset_modes is the list of values that appear in the dropdown/combobox

1 Like

I actually created a separate thread where I go in more detail: Template fan driving me crazy

This is the configuration for the template fan. The preset_mode_template is just picking a state from preset_modes, as you said, but it still ends up as null. In fact, I can’t find any working configuration for this on the internet whatsoever, so it seems no one has gotten preset_mode_template to work properly.

    study_ceiling_fan:
      friendly_name: "Study Ceiling Fan"
      value_template: >-
        {{ states('fan.study_fan_switch') }}
      percentage_template: >-
        {% if state_attr('fan.study_fan_switch', 'percentage') > 66  %}
          100
        {% elif state_attr('fan.study_fan_switch', 'percentage') > 33 %}
          66
        {% elif state_attr('fan.study_fan_switch', 'percentage') > 0 %}
          33
        {% else %}
          0
        {% endif %}
      availability_template: >-
        {{ not is_state('fan.study_fan_switch', 'unavailable') }}
      turn_on:
        service: fan.turn_on
        entity_id: fan.study_fan_switch
      turn_off:
        service: fan.turn_off
        entity_id: fan.study_fan_switch
      set_percentage:
        service: fan.set_percentage
        entity_id: fan.study_fan_switch
        data:
          percentage: >-
            {% if percentage > 66 %}
              100
            {% elif percentage > 33 %}
              50
            {% elif percentage > 0 %}
              3
            {% else %}
              0
            {% endif %}
      speed_count: 3
      preset_modes:
        - 'high'
        - 'medium'
        - 'low'
        - 'off'
      set_preset_mode:
        service: fan.set_percentage
        entity_id: fan.study_fan_switch
        data:
          percentage: >-
            {% if preset_mode == 'high' %}
              100
            {% elif preset_mode == 'medium' %}
              50
            {% elif preset_mode == 'low' %}
              3
            {% else %}
              0
            {% endif %}
      preset_mode_template: >-
        {% if state_attr('fan.study_fan_switch', 'percentage') > 66  %}
          high
        {% elif state_attr('fan.study_fan_switch', 'percentage') > 33 %}
          medium
        {% elif state_attr('fan.study_fan_switch', 'percentage') > 0 %}
          low
        {% else %}
          off
        {% endif %}

Hello guys, I just implemented this fan template using the code suggested by Pedro.

This is what i’m using right now:

fan:
  - platform: template
    fans:
      daikin_studio:
        friendly_name: "Daikin Studio Fan"
        value_template: '{% if is_state("climate.daikin_studio", "off") %} off {% else %} on {%- endif %}'
        percentage_template: >-
          {% set attr = state_attr('climate.daikin_studio', 'fan_mode') %}
          {% if attr == 'Silence' %}
            0
          {% else %}
            {% set attr = attr | int %}
            {{ attr * 100 / 5 }}
          {% endif %}
        availability_template: >-
          {% if states('climate.daikin_studio') in ['fan_only', 'dry', 'cool', 'heat', 'heat_cool', 'off']  %}
            true
          {% else %}
            false
          {% endif %}
        turn_on:
          service: climate.turn_on
          target:
            entity_id: climate.daikin_studio
        turn_off:
          service: climate.turn_off
          target:
            entity_id: climate.daikin_studio
        set_percentage:
          service: climate.set_fan_mode
          target:
            entity_id: climate.daikin_studio
          data_template:
            fan_mode: >-
              {% set idx = (percentage / ( 100 / 5 )) | int %}
              {% if idx == 0 %}
                Silence
              {% else %}
                {{ idx }}
              {% endif %}
        speed_count: 6

The fan is working properly, but I want to go further: Is there a way to set the 100% as “auto” mode?

Do someone know if this fan can be grouped with the Climate Entity in HomeKit?
Something like this:
services

Right now I have two different devices in my Home app: one for the Climate and one for the Fan.

Thank you!

1 Like