Fan, speed and speeds dont match

Hello,

I’m trying to create a fan. I used the following template:

- platform: template        
  fans:
    badkamer_beneden_fan:
      friendly_name: "Badkamer ventilator"
      value_template: "{{ 'on' }}"
      speed_template: >
        {% if is_state('switch.fibaro_system_fgs222_double_relay_switch_2x1_5kw_switch', 'off')   %}
          {% if is_state('switch.fibaro_system_fgs222_double_relay_switch_2x1_5kw_switch_2', 'off')   %}
            '1' 
          {% else %}
            '2'
          {% endif %}
        {% else %} 
          '3'
        {% endif %}
      turn_on:
        service: script.badkamer_beneden_aan
        data_template:
          speed:   "{{speed}}"
      turn_off:
        service: script.badkamer_beneden_1
      set_speed:
        service: script.badkamer_beneden_aan
        data_template:
          speed:   "{{speed}}"
      speeds:
        - '1'
        - '2'
        - '3'

Now somehow, my speeds and my speed_template are not matching, since I get the following error line in my log files:

2020-06-14 12:59:15 ERROR (MainThread) [homeassistant.components.template.fan] Received invalid speed: '2'. Expected: ['1', '2', '3'].

Somebody any clue?

Thanks,

Bart.

I have fixed it. Replaced ‘1’ with 1 on the template code.

Do you know why?

Because the result of a template is always a string value. So when you defined the number 2 in single-quotes like this '2' the template produces that verbatim and not simply 2.

That’s why the error message complains about an invalid speed '2' (a string containing single-quotes and the character 2) failing to match any of the strings in the speed list (which contains strings 1, 2, and 3).

Thank you very much. Slowly learning the syntax.