Input_number - moving average calculation

I am using my home assistant to manage my crypto mine. Part of this is needing the system to monitor the MH generation rate for my individual rigs. If and when a rig has a GPU which goes offline, I want home assistant to switch the rig off, wait a bit and then restart it. I have build custom wifi switches which are able to do this and I have managed to read the MH rate coming out of the mine. So I have the whole system working.

The change I want to make is, I currently manually set a threshold for each rig. So if the MH drops below this threshold a sequence of events take place. I want to change this to become an automatically calculated number based on a moving average of the mh generated by the rig. So if a rig is working and generating 100MH by example, I want the moving average to log it over a period of lets say 10 periods. I have a timer setup to trigger an automation every 2 minutes. I have also setup input_number variables. So now what I want to do is use the automation to do the following:

Step 1 - Take the current stored input_number MH and multiply it by 10.
Step 2 - Add that input_number to the current sensor MH.
Step 3 - Divide the calculated input_number by 11.

Simplified: AverageMH = ((AverageMH * 10) + SensorMH) / 11

This will then give me a moving average over the last 11 2 minute periods. Exactly what I want.

My problem is, I do not know how or where to do this calculation. Do I fit it into the automation somewhere? Do I make the automation trigger a script which does this calculation?

Any advice on how to do what I am looking for or perhaps alternate thinking to achieve the same goal would be appreciated.

Have you looked at statistics

I modified one of the examples. It will look at the last 11 over the last 30 minutes but will only keep 11 values to calculate he mean. So it will in effect be a moving average.

  - platform: statistics
    name: "Bathroom humidity change "
    entity_id: sensor.bathroom_humidity
    unique_id: "Bathroom Humidity Change"
    max_age:
      minutes: 30
    sampling_size: 11
    precision: 1

Hi, I want to do something that I am not sure will work with the configuration you showed. I have a pitot tube with a differential pressure sensor connected to ESPhome from which I can calculate wind speed in a wind tunnel. To estimate the wind speed, I have to get a moving mean of 3 seconds. My sensor is taking a measurement 100 times per second. In 3 seconds, I will have 300 samples, and I need to get the mean from points 1-300. When the samples go over 300, then I need to move the mean estimation one by one. If I now have 301 samples, I need to get the mean from data points 2-301, then 3-302, then 4-303 and so forth. Would this configuration work:

  - platform: statistics
    name: "3s Wind Speed "
    entity_id: sensor.differential_pressure
    unique_id: "Wind Speed - Pitot Tube"
    max_age:
      seconds: 3
    sampling_size: 300
    precision: 1

You will only ever have 300 measurements. It will discard everything every 3 seconds.

The suggested platform: statistics does not really provide a rolling/moving average.
BTW, it was missing a line state_characteristic: average_step (or a similar option).

I tried it out and got pretty disappointed: it restarts calculating the average after every sampling_size values. In this moment, the value is Unknown, and the average build up step by step until it is reset again. This is not a moving average!

A pity that Home Assistant does not provide a primitive for this.
Here is my workaround, which implements the idea mentioned by @DWaterford.
It (ab-)uses an input_number as a global variable that is remembered between runs of the automation that re-calculates the rolling average every second.

input_number:
  power_balance_avg:
    name: Power Balance Rolling Average
    initial: 0
    min: -999999
    max: +999999
    mode: box

# update input_number.power_balance_avg with sensor.power_balance to form rolling average over last 3 seconds
automation:
  - id: power_balance_rolling_avg
    initial_state: true
    trigger:
      - platform: time_pattern
	seconds: "/1"  # every second
    action:
      - variables:
          samples: "{{ 3 }}"
          prev   : "{{ states('input_number.power_balance_avg') }}"
          curr   : "{{ states('sensor.power_balance')| float(0) }}"
      - service: input_number.set_value
        target:
          entity_id: input_number.power_balance_avg
        data:
          value: "{{ ((prev * (samples-1) + curr) / samples)| round(0) }}"

The rolling average value is available to other automations and sensors via states('input_number.power_balance_avg').

BTW, shame that Home Assistant still does not support of global variables as first-class entities - see also the feature request of August 2020: