I’m trying to figure out how to create an automation that will generate a notification anytime the temperature has risen or fallen by so many degrees within the past one hour.
Obviously the value could be any type of numerical value and the amount of time could be variable depending on what Im wanting to track.
Does anyone have some suggestions as to how I could build this.
At minimum, i’m thinking I have to kick off an evaluation of these temperatures every x minutes in order to perform the evaluation or would it make sense to perform the evaluation continuously on every change of temperature event? IE, every time the temperature arrives from my temp sensor over mqtt or every time the wunderground api returns a new temperature value?
Look at the statistics sensor
Set it up for a 60 min age, then create an automation that triggers on temperature change and set a condition that check whether the abs(temp_value - min_value) >= X OR track whether the abs(temp_value - max_value) >= X, X being your X degrees from your post title…
Thanks very much for this. I have created the statistics sensor and am now seeing all of the statistical calculations I hoped to see. I think some items have even been added to the component and may already perform the change over time calculation i’m looking for. I setup the sensor for a sampling_size of 120 (because I publish my temperatures every 30 seconds) and I set the max age to 60 minutes. Despite the fact that the graph shows more than 1 hour, i think may be a bug. Notice how there is now a ‘Change’ attribute. As far as I can tell, the absolute value of that is what I want to trigger a notification. IE, ABS(change) >10.
I’m struggling understanding how to build that into the automation as a condition. I’m probably just getting the syntax wrong. In the attached image, i’m triggering off of the change in state of the sensor.attic_stats_mean which does appear to be working. I just can’t figure out how to evaluate the ‘change’ attribute.
I had given you a formula as a reference. I personally don’t use the editor in the UI so not sure if you’ll be able to use it, you may need to manually edit the automation from the files in order to add a template condition.
The automation would look something like this:
- alias: Significant Temperature Change
trigger:
- platform: state
entity_id: sensor.attic_temperature
condition:
- condition: or
conditions:
- condition: template
value_template: '{{ abs((states.sensor.attic_temperature.state | float) - (states.sensor.attic_stats_mean.attributes.min_value | float) >= 10 }}'
- condition: template
value_template: '{{ abs((states.sensor.attic_temperature.state | float) - (states.sensor.attic_stats_mean.attributes.max_value | float) >= 10 }}'
action:
- service: notify.emailme
data_template:
message: 'There is a significant temperature change of 10+ degrees on {{states.sensor.attic_temperature.name}}'
Few notes
I don’t have any stats sensor setup so states.sensor.attic_stats_mean.attributes.max_value may not work. you may need to replace with states.sensor.attic_stats.attributes.max_value or states.sensor.attic_stats_max_value.state
your screenshot above shows you’ve select sensor.attic_stats_meanas your trigger and condition. I’ve selected the real actual value, if you prefer the mean value, note that you’ve set the sampling to 120 so it will only compare min and max to the mean value of the past hour, not the actual current value. Amend as you wish to meet your requirements.
you need to replace the service: notify.emailme with the actual notification platform that you have set up
Regarding the formatting of the attributes… I think i’m beginning to understand how to call those items. i’ll do some experimenting to get a better handle on it.
My screen shot was just an example of something I had tried. Agree with you that I could want the actual value.
However, did you notice the ‘change’ attribute? I assume that is actually the change across the sample size present. If so, wouldn’t that be easier since the sensor already did the calculation for me? Then I could just send a notification when the change value is greater than ABS of X.
Last, the reason I chose sensor.attic_stats_mean as the trigger is simply to fire the event to cause the condition to be evaluated as I didn’t know any other way. From what I read, by using that as a trigger without specifying a ‘from’ and ‘to’, it just triggers each time a new sensor value arrives.
Ok, you helped me a ton. I’ve finally gotten it working. I set it up under the ‘automation old:’ area of my configuration.yaml. One thing I did figure out is the absolute value comes at the end of the expression. Just a little trial and error to get that working. I’m still struggling just a bit wrapping my head around the template extensions and the Jinja syntax but I appreciate you helping me out. Thanks again.
- alias: Significant Temperature Change
trigger:
- platform: state
entity_id: sensor.attic_temperature
condition:
- condition: or
conditions:
- condition: template
value_template: '{{ ((states.sensor.attic_temperature.state | float) - (states.sensor.attic_stats_mean.attributes.min_value | float) | abs) >= (10) }}'
- condition: template
value_template: '{{ ((states.sensor.attic_temperature.state | float) - (states.sensor.attic_stats_mean.attributes.max_value | float) | abs) >= (10) }}'
action:
- service: notify.ios_iphone8pm
data_template:
message: 'There is a significant temperature change of 10+ degrees in the last hour on {{states.sensor.attic_temperature.name}}'