Automation code for adjusting Air Conditioner temp

Hi everyone. I want to create an automation that follows this logic.
What’s the best way to code this in an automation?

If outside temp <= 22 then set AC to 22

If outside temp =23 to 23.9 then set AC to 23

If outside temp =24 to 24.9 then set AC to 24

Etc …

- id: whatever
  trigger:
    - platform: state
      entity_id: sensor.outside_temp
  condition:
    - "{{ trigger.from_state.state not in ['unknown', 'unavailable', 'none'] }}"
    - "{{ trigger.to_state.state not in ['unknown', 'unavailable', 'none'] }}"
    - "{{ trigger.from_state.state != trigger.to_state.state}}"
  action:
    - service: climate.set_temperature
      target:
        entity_id: climate.your_climate_device_here
      data:
        temperature: >
          {% if states('sensor.outside_temp')|float <= 22 %}
            22
          {% elif 23 <= states('sensor.outside_temp')|float <= 23.9 %}
            23
          {% elif 24 <= states('sensor.outside_temp')|float <= 24.9 %}
            24
...etc
          {% else %}
           # your default value here
          {% endif %}

Note that what you asked for has a gap between 22 and 23 that will end up selecting the else case.

1 Like
  action:
    - service: climate.set_temperature
      target:
        entity_id: climate.your_climate_device_here
      data:
        temperature: >
          {% set ot = states('sensor.outside_temp') | float %}
          {{ 22 if ot <= 22 else ot | int }}

He does not want the decimal part. So, this instead?

        temperature: >
          {% set ot = states('sensor.outside_temp') | int %}
          {{ 22 if ot <= 22 else ot | int }}

Also I would include the conditions to prevent both unnecessary execution of the actions and to deal with the possibility of a non numeric state.

Thank you @tom_l !
This is what I added and it works as expected so far.

alias: Auto Aircon Setting-Summer
description: ''
trigger:
  - platform: state
    entity_id: sensor.netatmo_village_high_indoor_outside_temperature
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state not in [''unknown'', ''unavailable'', ''none''] }}'
  - condition: template
    value_template: '{{ trigger.to_state.state not in [''unknown'', ''unavailable'', ''none''] }}'
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state}}'
action:
  - service: climate.set_temperature
    target:
      entity_id: climate.melview_a_c
    data:
      temperature: >
        {% if
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 22.9 %}
          22.5
        {% elif 23 <=
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 23.9 %}
          23
        {% elif 24 <=
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 24.9 %}
          24
        {% elif 25 <=
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 25.9 %}
          25
        {% elif 26 <=
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 26.9 %}
          26
        {% elif 27 <=
        states('sensor.netatmo_village_high_indoor_outside_temperature')|float
        <= 27.9 %}
          27
        {% else %}
         28
        {% endif %}
mode: single

The template can be reduced to 2 lines (as shown above).

That’s what the final int does but you made me realize that if int is used initially, instead of float, then there’s one less filter needed.

        temperature: >
          {% set ot = states('sensor.outside_temp') | int %}
          {{ 22.5 if ot < 23 else ot }}

If there’s a need to limit the maximum temperature to 28 then it can be done like this:

        temperature: >
          {% set ot = states('sensor.outside_temp') | int %}
          {{ 22.5 if ot < 23 else ot if ot <= 28 else 28 }}
1 Like

Just realized the template can be reduced to a single line:

    data:
      temperature: "{{ ([22.5, states('sensor.netatmo_village_high_indoor_outside_temperature')|int, 28]|sort)[1] }}"
2 Likes

Now you’re just showing off :slight_smile:

Nah, I just happened to recall this old post: