Detecting large fluctuations/oscillations in inputs

I've got a device which, due to a firmware bug, sometimes does this:

As you can probably imagine from the fact that the values are in watts, this not not healthy for said device, and I'd like to get an alert when it happens. I've figured out a way of doing it but I thought I'd ask for feedback on whether it's the way to do it. The idea was to use a Threshold helper to check when it goes outside bounds (upper = 5000, lower = 4000), and then connect it to a Statistics helper with max_age = 60 minutes and trigger an automation when count > 3 or 4, something like:

binary_sensor:
  - platform: threshold
    name: "Grid Power Excursion"
    unique_id: uniqueid__grid_power_excursion
    entity_id: sensor.grid_active_power
    lower: 4000
    upper: 5000

sensor:
  - platform: statistics
    name: "Grid Power Excursion Count"
    unique_id: uniqueid__grid_power_excursion_count
    entity_id: binary_sensor.grid_power_excursion
    state_characteristic: count_on
    max_age:
      hours: 1

automation notify_grid_power_excursion:
  - id: uniqueid__notify_grid_power_excursion
    alias: Notify of excessive grid power excursions
    triggers:
      - trigger: numeric_state
        entity_id:
          - sensor.grid_power_excursion_count
        above: 3
    actions:
      - action: notify.notify
        data:
          title: Grid Power Excursion
          message: Grid power use is oscillating +5kW / - 4kW

Is that a sensible way to do it, or have I missed an integration/helper/utility that'd do it better?

An update on this after a few days testing, the above doesn't actually work because it triggers continuously when the power usage is outside the given range, not just once when it crosses the threshold. Unfortunately there doesn't seem to be a sensor type that deals with this, requiring an automation for each range-exceeded event that increments a counter, which can then be examined by a statistics sensor:

counter:
  grid_power_excursion_counter:
    id: uniqueid__grid_power_excursion_counter
    name: "Grid Power Excursion Counter"
    initial: 0
    step: 1

automation check_grid_power_excursion_above:
  - id: uniqueid__check_grid_power_excursion_above
    alias: "Check for grid power excursion above maximum"
    triggers:
      - trigger: numeric_state
        entity_id: sensor.grid_active_power
        above: 5000
        for: "00:01:00"
    actions:
      - action: counter.increment
        target:
          entity_id: counter.grid_power_excursion_counter

automation check_grid_power_excursion_below:
  - id: uniqueid__check_grid_power_excursion_below
    alias: "Check for grid power excursion below minimum"
    triggers:
      - trigger: numeric_state
        entity_id: sensor.grid_active_power
        below: -4000
        for: "00:01:00"
    actions:
      - action: counter.increment
        target:
          entity_id: counter.grid_power_excursion_counter

sensor:
  - platform: statistics
    name: "Grid Power Excursion Count"
    unique_id: uniqueid__grid_power_excursion_count
    entity_id: counter.grid_power_excursion_counter
    state_characteristic: count
    max_age:
      hours: 1

So this increments the counter each time the power threshold is crossed, and the statistics sensor records how many of these threshold-exceeded counts have occurred in the last hour. A final automation (not shown) then acts on the events-per-hour going over a certain level.

This works, but if there's a less Rube-Goldberg way to go about it I'd be interested in hearing about it.