Script for heat/cool air conditioning

I’m trying to create a script where I can toggle my air conditioning. I try to pass the climate ID as a parameter/argument, but it doesn’t work.

This is what I currently have:

alias: Toggle mode
sequence:
  - variables:
      airco_id: "{{ input_airco_id }}"
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: "{{ airco_id }}"
            attribute: hvac_mode_state
            state: heat
          - condition: state
            entity_id: "{{ airco_id }}"
            attribute: hvac_mode_state
            state: cool
    then:
      - action: climate.set_hvac_mode
        target:
          entity_id: "{{ airco_id }}"
        metadata: {}
        data:
          hvac_mode: "off"
    else:
      - if:
          - condition: state
            entity_id: input_select.airco_s_mode
            state: cool
        then:
          - action: climate.set_hvac_mode
            target:
              entity_id: "{{ airco_id }}"
            metadata: {}
            data:
              hvac_mode: cool
        else:
          - action: climate.set_hvac_mode
            target:
              entity_id: "{{ airco_id }}"
            metadata: {}
            data:
              hvac_mode: heat
description: ""
icon: mdi:air-conditioner

I get a “Message malformed: Entity {{ airco_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘sequence’][1][‘if’][0][‘conditions’][0][‘entity_id’]” when trying to save the script. What am I doing wrong here?

Hi gefi,

Where is that coming from?

It’a a variable name not an entity_id.

This is the argument/parameter I want to pass from a custom card. I suspect that this is where I get the error from, but I don’t know how to properly write it. I want to pass a climateID as a parameter/argument, so I can reuse the script for my other air conditioners

You know what the entity is, do you need to pass it?
Just put it there and this will work.

I have 4 air conditioners. If I understand you correctly, I will need to create 4 different script with the same code, but a different climate ID for each of the airco’s?
Edit: I guess this will be my only option. Thank you for your input @Sir_Goodenough

There is no need to make multiple scripts… you are just using templates where they aren’t allowed, i.e. in a State condition.

Use a Template condition.

alias: Toggle mode
sequence:
  - variables:
      airco_id: "{{ input_airco_id }}"
  - if:
      - condition: template
        value_template: "{{ state_attr( airco_id, 'hvac_mode_state') in ['cool','heat'] }}"
    then:
      - action: climate.set_hvac_mode
        target:
          entity_id: "{{ airco_id }}"
        metadata: {}
        data:
          hvac_mode: "off"
    else:
      - if:
          - condition: state
            entity_id: input_select.airco_s_mode
            state: cool
        then:
          - action: climate.set_hvac_mode
            target:
              entity_id: "{{ airco_id }}"
            metadata: {}
            data:
              hvac_mode: cool
        else:
          - action: climate.set_hvac_mode
            target:
              entity_id: "{{ airco_id }}"
            metadata: {}
            data:
              hvac_mode: heat
description: ""
icon: mdi:air-conditioner

Or, more compact and UI friendly:

alias: Toggle HVAC Mode
fields:
  input_airco_id:
    name:  Target Air Conditioner
    description: "The climate entity you want to toggle"
    selector:
      entity:
        - filter:
            domain: climate
sequence:
  - action: climate.set_hvac_mode
    target:
      entity_id: "{{ input_airco_id }}"
    metadata: {}
    data:
      hvac_mode: |
        {% if state_attr(input_airco_id, 'hvac_mode_state') in ['cool','heat'] %}
          off
        {% else %}
          {{ states('input_select.airco_s_mode')}}
        {% endif %}
2 Likes

You are calling it from the dashboard.
I don’t really do that in my world, but if it were being called from an automation I know you can do it.
You need to have help from a dashboard person maybe.

Basically you need a valid entity name to be passed,then it should work.

Thank you for your code. I will try this later and report back later

@Didgeridrew the first solution works. Thank you for the code :slight_smile: