Lightning Notification with updates based on deltas

I’m looking for some brainstorming assistance.

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.

In case someone wants to see that script:

alias: Lightning Detected Confirm
use_blueprint:
  path: homeassistant/confirmable_notification.yaml
  input:
    notify_device: [redacted device id]
    title: Lightning Detected (Local)
    message: "{{ states('sensor.local_lightning') }} lightning strikes in the last hour."
    confirm_text: Mute Alarms
    confirm_action:
      - service: script.turn_on
        metadata: {}
        data:
          priority: high
          ttl: 0
          channel: lightning
        target:
          entity_id: script.mute_lightning_notifications
description: ""
alias: Weather - Lightning Alert (Local)
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.local_lightning
    not_to:
      - unknown
      - unavailable
condition:
  - condition: numeric_state
    entity_id: sensor.local_lightning
    above: 10

Unfortunately this will still have the issue that after the hourly reset to 0 it will not trigger again until >10

Instead of using the utility meter you could use the statistics integration that will give you a rolling hourly count.

sensor:
  - platform: statistics
    name: "Lightning strikes last hour"
    entity_id: sensor.raw_count_from_your_detector
    state_characteristic: change
    max_age:
     hours: 1

Don’t forget to change the entity_id in the automation.

EDIT: actually I’m not sure what you mean by this:

Do you want it to trigger on 10, 11, 12, 13… or 10, 20, 30…?

The sensor and automation above will do the first one, this will do the second (using the same stats sensor):

alias: Weather - Lightning Alert (Local)
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lightning_strikes_last_hour
    not_to:
      - unknown
      - unavailable
condition:
  - condition: numeric_state
    entity_id: sensor.lightning_strikes_last_hour
    above: 0
  - condition: template
    value_template: "{{ states('sensor.lightning_strikes_last_hour')|int(1) % 10 = 0 }}"

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.

Thanks for your help…

Hope that makes more sense.

You would only get an alert after the first 10 (then every 10). If you want to be alerted for all the strikes up to 10 change the automation to this:

alias: Weather - Lightning Alert (Local)
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lightning_strikes_last_hour
    not_to:
      - unknown
      - unavailable
condition:
  - condition: numeric_state
    entity_id: sensor.lightning_strikes_last_hour
    above: 0
  - condition: or
    conditions:
      - condition: numeric_state
         entity_id: sensor.lightning_strikes_last_hour
         below: 11
      - condition: template
        value_template: "{{ states('sensor.lightning_strikes_last_hour')|int(1) % 10 = 0 }}"

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?

Yes that was the mistake.

Awesome, Thanks for your help!!

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.

Thanks again for your help!!!