Help with fan template controlling multiple light and switch entities

Hi,

I got 3 different entities involved in a fancoil-based heating/cooling system:

  • light.fancoil: Zigbee 0-10v regulator connected to 0-10 terminals of the fancoil, to regulate the fan speed.
  • switch.onoff_fancoil: Dry contact relay that controls on/off terminals of the fancoil
  • switch.valvula_fancoil: Controls the on/off state of the water pipe that warms/cools the air

I ended up with this config that has 2 main issues:

  1. It doesn’t trigger any of the switch entities when using the fan entity
  2. When changing the speed in the fan entity, the ‘light.fancoil.brightness’ is updated to 0-49% only.

What am I doing wrong? I’ve already tried to use action instead of service, but nothing changed. I’m lost!

This is my yaml:

template:
  - fan:
      - name: "FanCoil Virtual"
        unique_id: fancoil_virtual
        state: "{{ is_state('light.fancoil', 'on') }}"
        turn_on:
          - service: switch.turn_on
            target:
                entity_id:
                - switch.onoff_fancoil
                - switch.valvula_fancoil
          - service: light.turn_on
            target:
                entity_id:
                - light.fancoil
        turn_off:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.onoff_fancoil
                - switch.valvula_fancoil
          - service: light.turn_off
            target:
              entity_id:
                - light.fancoil
        speed_count: 3
        set_percentage:
          - action: light.turn_on
            target:
              entity_id: light.fancoil
            data:
              brightness: "{{ percentage }}"
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
        preset_mode: >
          {% if is_state('light.fancoil', 'on') %}
            {% if state_attr('light.fancoil', 'brightness') >= 130  %}
              high
            {% elif state_attr('light.fancoil', 'brightness') >= 20 %}
              medium
            {% else %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}
        set_preset_mode:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'off' }}"
                sequence:
                  - service: fan.turn_off
                    target:
                      entity_id: fan.fancoil_virtual
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode != 'off' }}"
                sequence:
                  - service: switch.turn_on
                    target:
                      entity_id:
                        - switch.onoff_fancoil
                        - switch.valvula_fancoil
                  - service: light.turn_on
                    target:
                      entity_id: light.fancoil
                    data:
                      brightness_pct: >
                        {% if preset_mode == 'high' %}
                          254
                        {% elif preset_mode == 'medium' %}
                          169
                        {% elif preset_mode == 'low' %}
                          84
                        {% else %}
                          0
                        {% endif %}

If the light’s brightness value is 150, what will this report?

            {% if state_attr('light.fancoil', 'brightness') >= 20  %}
              high
            {% elif state_attr('light.fancoil', 'brightness') >= 130 %}
              medium
            {% else %}
              low
            {% endif %}

HINT: It won’t report medium.

Perhaps you meant to do this?

            {% if state_attr('light.fancoil', 'brightness') >= 130  %}
              high
            {% elif state_attr('light.fancoil', 'brightness') >= 20 %}
              medium
            {% else %}
              low
            {% endif %}

thanks for the reply, Taras.

In any case using the right values it doesn’t solve the percentage issue, as it still goes from 0 to 49%.

You need logic here to take 33 and convert it to low’s value, 66 for mediums value and 100 for highs value. And 0 for off. i.e.

{% if percentage == 33 %}
...

What I pointed out is just one of several things wrong it (see petro’s post).

Thanks for your replies. I managed to set the “brightness” correctly by doing:

        set_percentage:
          - action: light.turn_on
            target:
              entity_id: light.fancoil
            data:
              brightness: "{{ (percentage * 254 / 100) | round(0) }}"

Now, I need to deal with the fact that is not acting over the 2 other switches, and I don’t know why…

What does this mean?

I am not certain this part is the correct way of handling the off preset mode. Normally it should issue a command to turn off the controlled device(s) and its new status (now off) is reported back to Template Fan (via its state option). You’re turning off the Template Fan (fan.fancoil_virtual) directly.

        set_preset_mode:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ preset_mode == 'off' }}"
                sequence:
                  - service: fan.turn_off
                    target:
                      entity_id: fan.fancoil_virtual
                                 ^^^^^^^^^^^^^^^^^^^

NOTE

My comment is based on what I learned while automating a range hood fan.


EDIT

You need to (minimally) turn off light.fancoil because that’s the entity you specified in the Template Fan’s state option.

when setting a percentage, I need to turn on 2 different switches (switch.valvula_fancoil and switch.onoff_fancoil), and turning them off if the percentage is zero or the fan is in off state.

Then you need to add those actions to the set percentage like you did for preset mode.

Thanks to your comments and some reading I ended up with a working config that I think is the most compact and self-contained:

template:
  - fan:
    - name: "FanCoil Virtual"
      unique_id: fancoil_virtual
      state: "{{ is_state('light.fancoil', 'on') }}"
      turn_on:
        - action: light.turn_on
          target:
            entity_id: light.onoff_fancoil
        - action: switch.turn_on
          target:
            entity_id: switch.valvula_fancoil        
      turn_off:
        - action: light.turn_off
          target:
            entity_id: light.onoff_fancoil
        - action: switch.turn_off
          target:
            entity_id: switch.valvula_fancoil
      speed_count: 3
      set_percentage:
        - choose:
          - conditions:
            - condition: template
              value_template: "{{ percentage == 0 }}"
            sequence:
            - action: light.turn_off
              target:
                entity_id: light.onoff_fancoil
            - action: light.turn_off
              target:
                entity_id: light.fancoil
            - action: switch.turn_off
              target:
                entity_id: switch.valvula_fancoil
          default:
            - action: light.turn_on
              target:
                entity_id: light.onoff_fancoil
            - action: switch.turn_on
              target:
                entity_id: switch.valvula_fancoil   
            - action: light.turn_on
              target:
                entity_id: light.fancoil
              data:
                brightness: "{{ (percentage * 254 / 100) | round(0) }}"

Thanks @petro and @123 for your help! Hope this will help others when configuring fans with multi-device actions.

If you’re interested, you can reduce the code to this:

template:
  - fan:
    - name: "FanCoil Virtual"
      unique_id: fancoil_virtual
      state: "{{ is_state('light.fancoil', 'on') }}"
      turn_on:
        - action: homeassistant.turn_on
          target:
            entity_id:
              - light.onoff_fancoil
              - switch.valvula_fancoil        
      turn_off:
        - action: homeassistant.turn_off
          target:
            entity_id:
              - light.onoff_fancoil
              - switch.valvula_fancoil
      speed_count: 3
      set_percentage:
        - if: "{{ percentage == 0 }}"
          then:
            - action: homeassistant.turn_off
              target:
                entity_id:
                  - light.onoff_fancoil
                  - switch.valvula_fancoil
          else:
            - action: homeassistant.turn_on
              target:
                entity_id:
                  - light.onoff_fancoil
                  - switch.valvula_fancoil
            - action: light.turn_on
              target:
                entity_id: light.fancoil
              data:
                brightness: "{{ (percentage * 254 / 100) | round(0) }}"

NOTE

I noticed that the only place where the Template Fan sets the value of light.fancoil is in set_percentage and only if percentage is greater than zero.

Shouldn’t light.fancoil also be set to off when percentage is zero? The state of light.fancoil is used to determine the Template Fan’s state.

Or is light.onoff_fancoil somehow related to light.fancoil?