Delbz
(Marcus)
October 16, 2021, 2:55am
1
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 …
tom_l
October 16, 2021, 5:22am
2
- 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
123
(Taras)
October 16, 2021, 10:47am
3
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 }}
tom_l
October 16, 2021, 10:58am
4
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.
Delbz
(Marcus)
October 16, 2021, 11:28am
5
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
123
(Taras)
October 16, 2021, 12:02pm
6
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
123
(Taras)
October 16, 2021, 12:18pm
7
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
tom_l
October 16, 2021, 12:19pm
8
Now you’re just showing off
123
(Taras)
October 16, 2021, 12:22pm
9
Nah, I just happened to recall this old post:
I found a clever python example for limiting a value to fall within a range and converted it for use in a Jinja2 template.
Let’s say you receive a value that must lie within the range of 0 to 100.
If the value is less than 0 it should be reported as 0.
If the value is greater than 100 it should be reported as 100.
Otherwise, report the value as-is.
The traditional way is to use arithmetic comparisons to check the value against the range-limits and, with some if-else involved, report the corr…