New to HA, but not new to programming. Trying to write an automation script that will compare two temp values, and notify only when there is a change.
For instance, at the start of the day, every 5 min or so compare temp A with temp B, if temp A is greater than temp B, send a notification, then don’t do anything until temp A is smaller than temp B, and so on.
So far, I have only managed to do the first comparison and I’m stuck at how to wait, need a while loop probably.
Here’s the code so far:
Your description and example do not match, please clarify what it is you are trying to do…
Your two triggers will cause the automation to fire once every hour on the 5 minute mark, and when the state of either humidity sensor changes in such a way that temperature_4 goes from being less than temperature to being greater than temperature.
Consider changing your technique from polling every x minutes to the more standard event-driven approach of Home Assistant. Your sensors will update whenever they change value - that should be your trigger. When either of them changes, do your comparison and take action as desired.
Have one template where one is > the other.
And one template where it’s the other way around.
Then as condition
{{ ((now().minute | string)[-1]) | int % 5 ==0}}
However this will trigger each time there is a change, and ONLY at the minute ending with 5 or 0 does it do the action.
So if the change happened at xx:39 then it will not do the action at xx:40.
Is this what you expect or should it do it’s thing at xx:40?
It depends on your intention for the “every 5 minutes” part of your current process. If that was just how often you wanted to check to see if anything has changed using the “polling loop” approach, then you can get rid of the time pattern completely and just use the “one sensor or the other has changed” events as the triggers. But if there is some other reason that you want to limit changes to no more than once per five minutes or something, then you can keep it per @Hellis81’s example.
my aim with the times was not to overload the HA. but if that only triggers on status change (i.e. temperature changes) then I can leave that out. However, my main question still stands, how can I send a notification when temp A is greater than temp B, then not send any notification until temp B is greater than temp A? Maybe I can better explain with one example what my aim is. At the start of the day, the temperature inside is greater than outside, then the temperature outside starts rising to the point when it is greater than the temperature inside. This is where I want the first notification. But then, later on the day the temperature outside drops to the point where it is less than the temperature inside, this is when I want the second notification. Does it make sense?
You state above that you are not new to programming, so you should totally understand when I say there are many different ways you could approach doing this. What follows is just one, that happens to seem logical to me. Use / modify / discard as best suits your way of thinking!
I would create a “toggle helper” (aka input_boolean) called “Outside Higher than Inside”, or something similar.
One automation triggers on Outside Temp changing. When it fires, set “Outside Higher than Inside” to be True or False based on the results of your comparison. (Note: no need to check if it was True or False previously; it won’t hurt anything to set an already True helper to be True.)
Frankly, I don’t think you need to have a second automation based on Inside Temp changing, but you could, and it’s logic would be the same: set the value of “Outside Higher than Inside” as appropriate.
Finally, your notification automation should be triggered by any change to the “Outside Higher than Inside” Toggle helper.
A refinement will likely be some element of timing, so you don’t get too many notifications when the temps fluctuate right around the cutover point. But you don’t need to mess with this at first.
Am I missing anything as this doesn’t work? It does send the notification when I manually run it, but it doesn’t automatically send notifications when the temperature changes.
Down below is my current code. However, there are still two issues with this:
Sometime, I get a temperature of 0, how can I ignore this when evaluating the expression?
“23:07 Temp 1: 21.0, Temp 2: 0.0”
“23:09 Temp 1: 0.0, Temp 2: 21.5”
Also, sometime, changes are occurring in rapid succession
“21:56 Temp 1: 21.5, Temp 2: 21.6”
“21:58 Temp 1: 21.6, Temp 2: 21.5” => so basically here I would like either to ignore any state changes for 5-10 min or increase the difference that would trigger the value of the template, i.e. make it 0.5 degrees (I’ve already rounded the values)
“22:03 Temp 1: 21.7, Temp 2: 21.6” => this one is odd as temp 1 is still higher than temp 2 which triggered 5 mins earlier, why did it fire a notification?
At some point the temperature outside can/will be below 1 degree.
I think it’s better if the default is “N/A” or something that can be differentiated from a possible true value.
Agreed, and I have that problem with my exterior temperature sensors where I take the lower of the two values to eliminate sun loading, but they show 0°C when offline, not unknown or unavailable.
Thinking about it, my solution will continually send notifications. Here’s another go, ignoring the time condition in favour of the 0.5 difference in the template, and filtering out 0 values: