HVAC switch scripts

Hello,

I have 9 different “dual smart thermostats” custom components on two different air handlers (5 on one, 4 on the other). Each thermostat has a switch group for heater and cooler. Heater switch group is a damper, zone valve, and heat signal. Cooler switch group is a damper and compressor signal. The fan entity in the thermostats are to the respective switch to signal for that air handler. Inside the air handlers there are 3 wires that can connect to the fan motor that sets the air handler fan speed. 1 wire for each (low, med, high). Only one can be connected to the fan motor at a time. These 3 wires are on 3 different switches controlled in HA and all switches are connected to the fan motor wire. What I am trying to do set an automation, helper, scene, or script to determine if 1, 2, or 3 or more dampers are open. If only 1 damper is open then the switch for low is on, other 2 off. If 2 dampers are open then switch for med is on, other 2 off. If 3 or more dampers open then switch for high is on, other 2 off.

How do I do this? Helper can only group the switches but not decide. Automation can set conditions, but I would need to make a ton of automations for each possible outcome, is there a cleaner way to do this that I just don’t know about?

Hide the switches from the dashboard and use two template fans.

How can i hide the switches for this to work. The wires have to be connected via switch (m5stack4relay). I didn’t see anything in that templating that allows me to set this up the way i have to. I admit, I may not be reading this correctly.

Don’t put them on your dashboard.

        set_percentage:
          action: script.fans_set_speed

Do you logic in that script with a choose action.

I will try, I am going to have to try and figure out what that logic has to be because that is what i am struggling with.

I can help you with that if you tell me your damper and switch entity ids.

Thank you, I don’t intend for you to do the work, I am just trying to figure out how to start and I don’t know how with the given options.

switch.hot_water_tank_1_relay_17 #damper for zone in AH2
switch.hot_water_tank_1_relay_20 #damper for zone in AH2
switch.hot_water_tank_1_relay_19 #damper for zone in AH2
switch.hot_water_tank_1_relay_18 #damper for zone in AH2
switch.hot_water_tank_1_relay_24 #damper for zone in AH2
switch.core_sensor_hub_relay_30 #AH2 fan relay

switch.core_sensor_hub_relay_32 #AH2 fan speed low
switch.core_sensor_hub_relay_35 #AH2 fan speed med
switch.core_sensor_hub_relay_36 #AH2 fan speed high

switch.hot_water_tank_1_relay_13 #damper for zone in AH1
switch.hot_water_tank_1_relay_14 #damper for zone in AH1
switch.hot_water_tank_1_relay_15 #damper for zone in AH1
switch.hot_water_tank_1_relay_16 #damper for zone in AH1
switch.core_sensor_hub_relay_26 #AH1 fan relay

switch.core_sensor_hub_relay_28 #AH1 fan speed low
switch.core_sensor_hub_relay_33 #AH1 fan speed med
switch.core_sensor_hub_relay_34 #AH1 fan speed high

For AH1:

script:
  fans_set_speed:
    sequence:
      - variables:
          number_of_dampers_open: >
            {{ [ states('switch.hot_water_tank_1_relay_13')|bool,
                 states('switch.hot_water_tank_1_relay_14')|bool,
                 states('switch.hot_water_tank_1_relay_15')|bool,
                 states('switch.hot_water_tank_1_relay_16')|bool ] |sum }}
      - choose:
          - conditions: # off
              - "{{ number_of_dampers_open = 0 }}"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_28
                    - switch.core_sensor_hub_relay_33
                    - switch.core_sensor_hub_relay_34
          - conditions: # low
              - "{{ number_of_dampers_open = 1 }}"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_33
                    - switch.core_sensor_hub_relay_34
              - action: switch.turn_on
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_28
          - conditions: # med
              - "{{ number_of_dampers_open = 2 }}"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_28
                    - switch.core_sensor_hub_relay_34
              - action: switch.turn_on
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_33
          - conditions: # high
              - "{{ number_of_dampers_open > 2 }}"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_28
                    - switch.core_sensor_hub_relay_33
              - action: switch.turn_on
                target:
                  entity_id:
                    - switch.core_sensor_hub_relay_34
1 Like

Incredible. I did not know that you could add that in the way you did with variables. I have so much to learn. Thank you. This makes total sense now that I see it.

1 Like

So using that and this script:

script:
  fan_ah1_off:
    sequence:
      - action: switch.turn_off
        target:
          entity_id:
            - switch.core_sensor_hub_relay_28
            - switch.core_sensor_hub_relay_33
            - switch.core_sensor_hub_relay_34

You should be able to define a template fan like this:

fan:
  - platform: template
    fans:
      ah1_fan:
        friendly_name: "AH1 Fan"
        value_template: "{{ not is_state_attr('fan.ah1_fan','preset_mode','off') }}"
        preset_mode_template: >
          {% if is_state('switch.core_sensor_hub_relay_28','on') %}
            low
          {% elif is_state('switch.core_sensor_hub_relay_33','on') %}
            med
          {% elif is_state('switch.core_sensor_hub_relay_34','on') %} 
            high
          {% else %}
            'off'
          {% endif %}
        turn_on:
          action: script.fans_set_preset_mode
        turn_off:
          action: script.fan_ah1_off
        set_preset_mode:
          action: script.fans_set_preset_mode
        preset_modes:
          - 'off'
          - 'low'
          - 'med'
          - 'high'

If you try to turn the fan on and there are no dampers open, it should immediately turn off.

Likewise, selecting a fan preset mode that is not compatible with the number of dampers open will revert the fan preset to the valid state immediately.

1 Like

if I copy this and add to yaml under scripts it gives the error Message malformed: extra keys not allowed @ data[‘script’]

So you have this?

script:
script:
  fans_set_speed:
    sequence:
      - variables: etc...

Can you see what the error is?

Delete the duplicate script:

I also do not see the fan set speed script in the fan template, there are preset scripts listed there, but I do not see a preset script example, is that supposed to be the set speed script?

Yes. That whole text box already has script: you don’t need it. And you will have to move everything else two spaces left.

Here copy this:

fans_set_speed:
  sequence:
    - variables:
        number_of_dampers_open: >
          {{ [ states('switch.hot_water_tank_1_relay_13')|bool,
                states('switch.hot_water_tank_1_relay_14')|bool,
                states('switch.hot_water_tank_1_relay_15')|bool,
                states('switch.hot_water_tank_1_relay_16')|bool ] |sum }}
    - choose:
        - conditions: # off
            - "{{ number_of_dampers_open = 0 }}"
          sequence:
            - action: switch.turn_off
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_28
                  - switch.core_sensor_hub_relay_33
                  - switch.core_sensor_hub_relay_34
        - conditions: # low
            - "{{ number_of_dampers_open = 1 }}"
          sequence:
            - action: switch.turn_off
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_33
                  - switch.core_sensor_hub_relay_34
            - action: switch.turn_on
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_28
        - conditions: # med
            - "{{ number_of_dampers_open = 2 }}"
          sequence:
            - action: switch.turn_off
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_28
                  - switch.core_sensor_hub_relay_34
            - action: switch.turn_on
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_33
        - conditions: # high
            - "{{ number_of_dampers_open > 2 }}"
          sequence:
            - action: switch.turn_off
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_28
                  - switch.core_sensor_hub_relay_33
            - action: switch.turn_on
              target:
                entity_id:
                  - switch.core_sensor_hub_relay_34

ok, that is what I was in the process of doing because I was assuming, thank you for retyping. Where do I add this set speed script into the fan template above?

odd, still doing it now it says fans_set_speed instead of script

fan:
  - platform: template
    fans:
...
        turn_on:
          action: script.fans_set_preset_mode  ####  <--- It is here  ####
        turn_off:
          action: script.fan_ah1_off
        set_preset_mode:
          action: script.fans_set_preset_mode ####  <--- And here  ####
...

Ok I don’t use the UI editor. I suspect you only need everything after sequence:

- variables:
    number_of_dampers_open: >
      {{ [ states('switch.hot_water_tank_1_relay_13')|bool,
            states('switch.hot_water_tank_1_relay_14')|bool,
            states('switch.hot_water_tank_1_relay_15')|bool,
            states('switch.hot_water_tank_1_relay_16')|bool ] |sum }}
- choose:
    - conditions: # off
        - "{{ number_of_dampers_open = 0 }}"
      sequence:
        - action: switch.turn_off
          target:
            entity_id:
              - switch.core_sensor_hub_relay_28
              - switch.core_sensor_hub_relay_33
              - switch.core_sensor_hub_relay_34
    - conditions: # low
        - "{{ number_of_dampers_open = 1 }}"
      sequence:
        - action: switch.turn_off
          target:
            entity_id:
              - switch.core_sensor_hub_relay_33
              - switch.core_sensor_hub_relay_34
        - action: switch.turn_on
          target:
            entity_id:
              - switch.core_sensor_hub_relay_28
    - conditions: # med
        - "{{ number_of_dampers_open = 2 }}"
      sequence:
        - action: switch.turn_off
          target:
            entity_id:
              - switch.core_sensor_hub_relay_28
              - switch.core_sensor_hub_relay_34
        - action: switch.turn_on
          target:
            entity_id:
              - switch.core_sensor_hub_relay_33
    - conditions: # high
        - "{{ number_of_dampers_open > 2 }}"
      sequence:
        - action: switch.turn_off
          target:
            entity_id:
              - switch.core_sensor_hub_relay_28
              - switch.core_sensor_hub_relay_33
        - action: switch.turn_on
          target:
            entity_id:
              - switch.core_sensor_hub_relay_34

I thought it was named script.fans_set_speed my apologies, I truly am gaffing this whole thing lol