home-assistant-glow icon indicating copy to clipboard operation
home-assistant-glow copied to clipboard

Filter out spikes in the power consumption sensor

Open IliasEagle opened this issue 2 years ago • 0 comments

Is there a way to filter out the spikes created when power increases suddenly and that creates a huge spike as can be seen below. If someone has a suggestion can you please suggest the code.

image.

I have tried to create a copy of the pulse counter sensor and add a sliding moving average filter but it was not reporting any values (the sensor would show 0W but nothing would appear in the history).

- platform: pulse_meter
   name:  '${friendly_name} - Filtered Power Consumption'
   id: sensor_filtered_energy_pulse_meter
   unit_of_measurement: 'W'
   state_class: measurement
   device_class: power
   icon: mdi:filter
   accuracy_decimals: 0
   pin: ${pulse_pin}
   # internal_filter: 100ms
   filters:
     - sliding_window_moving_average:
         window_size: 15
         send_every: 15
     - lambda: return x * ((60.0 / ${pulse_rate}) * 1000.0);

I also tried using a template sensor but that was giving me compilation errors and frankly I am not sure if the id(sensor_energy_pulse_meter) is outputting the raw pulse counter value or the lambda value from the source sensor ( I assumed it was the latter).

 - platform: template
    name: '${friendly_name} - Filtered Power Consumption'
    icon: mdi:filter
    accuracy_decimals: 0
    unit_of_measurement: 'W'
    state_class: measurement
    device_class: power
    lambda: |-
      if (id(sensor_energy_pulse_meter)<= 12000.0)  
      {
        return id(sensor_energy_pulse_meter);
      }
      else
      {
        return 12000.0
      }

IliasEagle avatar Aug 01 '22 20:08 IliasEagle