Cergon
January 22, 2024, 8:46am
1
Hi All - Been scratching my head over this one for a couple of days now so any help gratefully appreciated:
I have the following automation trigger template which works:
{{ states('sensor.temperature')|float(0) > 35 or
states('sensor.temperature')|float(0) < 1 }}
I then have a condition to determine if the value is above or below the trigger value. This always returns false irrespective of trigger value.
{{states('trigger.to_state.state') | float(0) > 35 }}
For some reason the trigger value is not being evaluated by the condition template.
tom_l
January 22, 2024, 9:23am
2
{{ trigger.to_state.state | float(0) > 35 }}
There’s no need for templates by the way. All of this can be done with numeric state triggers and conditions.
pedolsky
(Pedolsky)
January 22, 2024, 10:03am
3
Isn’t that just a matter of taste or has hardcoding advantages my tunnelvisioned mind cannot see? (making frequent use of templates out of lazyness).
tom_l
January 22, 2024, 10:10am
4
I don’t complicate things if I don’t have to.
Cergon
January 22, 2024, 10:15am
5
Thanks - fully aware there’s many ways to cut the cake with this however in true HA style I’m trying to overcomplicate as far as possible. For my wider learning I would be interested to understand why my proposed approach is failing. Furthermore, what would be ‘best practice’ with regards to achieving this? Is template usage more resource intensive as the system scales?
tom_l
January 22, 2024, 10:32am
6
states()
needs to be supplied an an entity id, not a value. You were not only supplying it with a state (trigger.to_state.state
) but you quoted the variable ('trigger.to_state.state'
)so it became a string.
Assume your trigger to state was 35, that would give this in your template:
{{states('trigger.to_state.state') | float(0) > 35 }}
If you had not quoted the trigger variable it would have given this (still wrong):
{{states(35) | float(0) > 35 }}
Where as this:
{{ trigger.to_state.state | float(0) > 35 }}
gives:
{{ 35 > 35 }}
Troon
(Troon)
January 22, 2024, 10:42am
7
Post the complete YAML for your automation.
Cergon
January 22, 2024, 10:57am
8
Thank you - understood and appreciate the clear explanation.
Cergon
January 22, 2024, 11:02am
9
Full automation as per below. Note this has been updated as per the solution response.
alias: _temperature test
description: ""
trigger:
- platform: numeric_state
entity_id:
- sensor.temperature1
below: 1
id: External Temperature1
- platform: numeric_state
entity_id:
- sensor.temperature2
above: 30
id: External Temperature2
- alias: Template Temperature Trigger
platform: template
value_template: |-
{{ states('sensor.temperature')|float(0) > 35 or
states('sensor.temperature')|float(0) < 1 }}
id: External Temperature3
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- External Temperature1
- condition: state
entity_id: binary_sensor.external_temperature1_threshold_rising
state: "off"
sequence:
- service: notify.all
data:
message: >-
Home {{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
trigger.to_state.state }}°c and decreasing.
- conditions:
- condition: trigger
id:
- External Temperature2
- condition: state
entity_id: binary_sensor.external_temperature2_threshold_rising
state: "on"
sequence:
- service: notify.all
data:
message: >-
ALERT! {{ state_attr(trigger.entity_id, 'friendly_name') }} is
{{ trigger.to_state.state }}°c and rising.
- if:
- condition: state
entity_id: binary_sensor.tablet
state: "on"
then:
- service: notify.tablet
data:
message: TTS
data:
tts_text: >-
ALERT! {{ state_attr(trigger.entity_id, 'friendly_name')
}} is {{ trigger.to_state.state }}°c and rising.
- conditions:
- condition: trigger
id:
- External Temperature3
sequence:
- if:
- condition: template
value_template: "{{ trigger.to_state.state | float(0) > 35 }}"
- condition: state
entity_id: binary_sensor.external_temperature3_threshold_rising
state: "on"
then:
- service: notify.all
data:
message: >-
{{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
trigger.to_state.state }}°c and rising.
else:
- condition: template
value_template: "{{ trigger.to_state.state | float(0) < 1 }}"
- condition: state
entity_id: binary_sensor.external_temperature3_threshold_rising
state: "off"
- service: notify.all
data:
message: >-
{{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
trigger.to_state.state }}°c and decreasing.
mode: single
Troon
(Troon)
January 22, 2024, 11:41am
10
If it works, it’s fine. Personally, I’d replace:
- alias: Template Temperature Trigger
platform: template
value_template: |-
{{ states('sensor.temperature')|float(0) > 35 or
states('sensor.temperature')|float(0) < 1 }}
id: External Temperature3
with
- platform: numeric_state
entity_id: sensor.temperature
above: 35
id: External Temperature3
- platform: numeric_state
entity_id: sensor.temperature
below: 1
id: External Temperature3
and it’ll work exactly the same but in theory need less evaluation each minute. If you give the second one a new id
you can shorten some of the logic further down.
You’re aware that you won’t get a notification if the temperature is rising but your binary sensor is off? and likewise for falling / on?
Cergon
January 22, 2024, 12:47pm
11
So it’s more resource intensive to evaluate value templates as triggers?
Yes - aware of this, thanks.
Troon
(Troon)
January 22, 2024, 1:11pm
12
I cannot definitively answer that, nor would it matter unless you had a lot on a very low-power system… It’s more about elegance and clarity of the coding: template triggers feel like a last resort.