I have a lightning detector in my backyard. If the 10 or more lightning strikes occur in the last hour, I want to be notified and continue to be notified every time there are 10 more lightning strikes until the hourly strike count drops back below 10.
Currently I have a Utility Meter helper that counts the lightning strikes in the last hour and an automation that triggers when that count exceeds 10 and that works great. The issue now is that I can’t figure out how to have it continue to notify every time the delta increases by 10 or more until it drops back below 10.
Here’s my automation:
alias: Weather - Lightning Alert (Local)
description: ""
trigger:
- platform: numeric_state
entity_id:
- sensor.local_lightning
above: "10"
condition: []
action:
- service: notify.mobile_app_mc
data:
message: Lightning Detected (Local) is above 10 in the last hour
title: Lightning Detected (Local)
data:
priority: high
ttl: 0
channel: lightning
enabled: false
- service: script.turn_on
metadata: {}
data: {}
target:
entity_id: script.lightning_detected_confirm
mode: single
The script is actually a blueprint for sending templated notifications with actionable notification buttons that allow me to mute the automation for an hour.
Thanks for the tip on using a the statistics platform. I knew there was a way to do it, but gave up and decided that resetting every hour was good enough.
As for the notification for the deltas, after the lightning strike counter goes above 10, I don’t want to be notified for every strike after 10…so I figured getting a notification for every additional 10 strikes would be sufficient. My rationale is that the other night we had a storm. I got the initial notification that there had been 11 strikes in the last hour. Then didn’t get another alert for an hour. I was watching outside and there was a ton of strikes that I could see, so I opened Home Assistant and the counter was at 90.
So if the storm is really bad and let’s say there are 40 strikes in an hour…right now I would only know about the first 11. But I would like to get another alert when it passes 20, 30, 40 so I know the storm is still raging, but I don’t need to know about every strike. If I had that the other night, I would have had over 80 alerts. Eventually I want to add a condition to only count strike within a certain distance. My Lightning detector gives me both the count and the distance…one step at a time.
I’m trying to learn templates and admittedly I’m not a coder, but I got an error when I tried to use the value_template: “{{ states(‘sensor.lightning_strikes_last_hour’)|int(1) % 10 = 0 }}”
The error is: Message malformed: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘=’) for dictionary value @ data[‘condition’][1][‘value_template’]
I found that if I remove " = 0" from the end, the error goes away, but I don’t understand the syntax to know what that really does, unless you meant for that to be a boolean compare. In that case, I think you meant for it to be either “== 0” which results in either a true (when the number is above 10) or false (when it is below 10). Did I get that correct?
A storm rolled through last night, and it worked exactly how I wanted with one think I overlooked. As the statistical entity ages out lightning strikes, I get alerted again when the strike count goes down. Not a big deal, but just something I didn’t anticipate.
Basically, the automation fires at 10, 20, 30, etc and then fires again at 30, 20, 10, and 0. I can easily fix not having it fire at 0. Its not a big deal, but I don’t really care about lighting strike counts as they are aging out.
I am going to try adding a “Trend” helper and see if I can add that to the conditions of the automation. I’ve never used one before, so that will be something new to learn. My thinking is that it would only fire if the trend is increasing, not decreasing. I’ll let you know how it goes.