Fan template error: value should be a string for dictionary value

Keep getting an error with this admittedly ugly template, and am not seeing the issue:

2024-12-22 17:25:47.875 ERROR (MainThread) [homeassistant.config] Invalid config for 'fan' from integration 'template' at packages/air_quality.yaml, line 81: template value should be a string for dictionary value 'fans->air_purifier->set_preset_mode->0->data', got {'device': 'air_purifier', 'command': "{% if preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}\n  up\n{% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}\n  up\n{% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}\n  up\n{% if preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}\n  down\n{% elif preset_mode == 'high' and state_attr('fan.air_purif..., please check the docs at https://www.home-assistant.io/integrations/template

  - platform: template
    fans:
      air_purifier:
        friendly_name: "Air Purifier"
        value_template: >-
          {% if states('sensor.living_room_air_purifier_power_power')|int > 0 %}
            on
          {% else %}
            off
          {% endif %}
        turn_on:
          - action: remote.send_command
            target:
              entity_id: remote.universal_remote
            data:
              device: air_purifier
              command: power
        turn_off:
          - action: remote.send_command
            target:
              entity_id: remote.universal_remote
            data:
              device: air_purifier
              command: power
        preset_modes:
          - "low"
          - "medium"
          - "high"
          - "turbo"
        preset_mode_template: >-
          {% if states('sensor.living_room_air_purifier_power_power')|int > 40 %}
            turbo
          {% elif states('sensor.living_room_air_purifier_power_power')|int > 15 %}
            high
          {% elif states('sensor.living_room_air_purifier_power_power')|int > 7 %}
            medium
          {% elif states('sensor.living_room_air_purifier_power_power')|int > 6 %}
            low
          {% else %}
            off
          {% endif %}
        speed_count: 4
        set_preset_mode:
          - action: remote.send_command
            target:
              entity_id: remote.universal_remote
            data:
              device: air_purifier
              command: >-
                {% if preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  up
                {% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  up
                {% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  up
                {% if preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  down
                {% elif preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  up
                {% elif preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  up
                {% if preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  down
                {% elif preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  down
                {% elif preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  up
                {% if preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  down
                {% elif preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  down
                {% elif preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  down
                {% else %}
                  up
                {% endif %}
              num_repeats: >-
                {% if preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  1
                {% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  2
                {% elif preset_mode == 'turbo' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  3
                {% if preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  1
                {% elif preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  1
                {% elif preset_mode == 'high' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  2
                {% if preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  2
                {% elif preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  1
                {% elif preset_mode == 'medium' and state_attr('fan.air_purifier', 'preset_mode') == 'low'%}
                  1
                {% if preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'turbo'%}
                  3
                {% elif preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'high'%}
                  2
                {% elif preset_mode == 'low' and state_attr('fan.air_purifier', 'preset_mode') == 'medium'%}
                  1
                {% else %}
                  0
                {% endif %}

You can only have one if in an if- statement block… all the rest need to be elif. The extraneous if will cause the template to error out. Make sure to fix the ones in the num_repeats variable while your at it.

Thank you, you found what my tired eyes were skipping over.

Good to know that a template error will return as a generic invalid config. ideally it would at least tell you it’s a template error.

FWIW, since there are only 4 choices, you could simplify those templates by using a variable and a couple tricks:

              command: >-
                {% set current = state_attr('fan.air_purifier', 'preset_mode') %}
                {% if preset_mode == 'turbo' %}
                  up
                {% elif preset_mode == 'high' %}
                  {{ 'down' if current == 'turbo' else 'up' }}
                {% elif preset_mode == 'medium' %}
                  {{ 'down' if current != 'low' else 'up' }}
                {% elif preset_mode == 'low''%}
                  down
                {% else %}
                  up
                {% endif %}
              num_repeats: |
                {% set options = ['low','medium','high','turbo']%}
                {% set current = state_attr('fan.air_purifier', 'preset_mode') %}
                {{ (options.index(current) - options.index(preset_mode ))|abs}}
1 Like

Thanks. I was still on the POC step, hadn’t even started to think about cleanup. The simplification to num_repeats is especially clever.