I decided to make a temperature notification that lets me know when a threshold temperature has been crossed (e.g. when to close the windows when it’s heating up and vice versa when it’s cooling down).
The lovelace interface looks like this (with the slider named input_number.notification_temperature:
- id: 'notification_temperature'
alias: Notification - Temperature
description: Send a notification when a predefined temperature has been reached.
trigger:
- platform: template
value_template: '{{ state_attr(''weather.dark_sky'', ''temperature'')|int > -300 }}'
condition:
- condition: template
value_template: '{% set t1 = trigger.from_state.attributes.temperature|float %}
{% set t2 = trigger.to_state.attributes.temperature|float %}
{% set tn = states(''input_number.notification_temperature'')|float %}
{{ (t1 < tn and t2 >= tn) or (t1 > tn and t2 <= tn) }}'
action:
- data:
data:
apns_headers:
apns-collapse-id: temperature_threshold
push:
sound: temperature_notification.wav
message: The outside temperature is {{ state_attr('weather.dark_sky', 'temperature') }}˚C
service: notify.notify
- data: {}
entity_id: automation.notification_temperature
service: automation.turn_off
mode: single
The bizarre thing is that it works some of the the time, but other times the temperature crosses through the threshold, which is odd. I get no errors. Any ideas? Is this totally the wrong way to try to accomplish this? I thought it was kind of elegant but it turns out to not be reliable.
Yeah, the automation turning itself off is a bit unexpected. Why are you doing that?
But, besides that, this automation will trigger the first time that weather.dark_sky changes state and its temperature attribute is above -300 (which, unless you live somewhere extremely unpleasant) would always be true. After then, the temperature attribute would have to go below -300 (I’d even say uncomfortable!) and then above -300 again for the trigger to fire again.
I would recommend:
- id: 'notification_temperature'
alias: Notification - Temperature
description: Send a notification when a predefined temperature has been reached.
trigger:
- platform: template
value_template: >
{% set t = state_attr('weather.dark_sky', 'temperature')|float %}
{% set tn = states('input_number.notification_temperature')|float %}
{{ t > tn }}
- platform: template
value_template: >
{% set t = state_attr('weather.dark_sky', 'temperature')|float %}
{% set tn = states('input_number.notification_temperature')|float %}
{{ t < tn }}
action:
- data:
data:
apns_headers:
apns-collapse-id: temperature_threshold
push:
sound: temperature_notification.wav
message: The outside temperature is {{ state_attr('weather.dark_sky', 'temperature') }}˚C
service: notify.notify
mode: single
This will trigger whenever the temperature rises above the threshold, or falls below the threshold. (Remember, a template trigger will only re-trigger when the changes to false and then back to true.)
Remember, a template trigger will only re-trigger when it changes to false and then back to true.
This is a fundamental misunderstanding I’ve had with triggers. I thought that if any of the entities involved were to change, it would reevaluate the trigger and fire the automation if true (even if it were true the last time it was evaluated). I was disabling the automation since I wanted a one-time notification that I can readjust/reenable if desired.
It would appear that disabling the automation is what lead me to this misunderstanding since it has to fire once before it can go false then true again. So if I keep the automation enabled, I get a notification the first time the temperature changes (unintended side effect) but not again until the threshold is crossed (desired effect).
Thanks once again Phil for the lesson AND example solution. You’ve been incredible!
Depends on the trigger. This is the way it works for template triggers and numeric state triggers. Once the conditions are met it won’t fire again until they are not met then are met again.
But there are other things that can cause them to fire again when they normally wouldn’t. E.g., disabling & re-enabling the automation re-initializes the trigger. Reloading automations or restarting HA will also do this. Also, if there’s any “error”, such as the template not being able to be rendered properly, or a non-numeric state with a numeric state trigger, will also cause them to “forget” they’ve already triggered.