Can a spike during restart of a Derivative sensor be avoided?

Hello,

I have a template sensor from which I derive derivative sensor.

- platform: template
  sensors:
    zaehler_strom:
      friendly_name: "Stromzähler"
      unit_of_measurement: "kWh"
      value_template: "{{ (28803.0 + (states('sensor.roh_strom')|float - 28164453.0) /    1000.0  )          |    round(3) }}"

- platform: derivative
  source: sensor.zaehler_strom
  name: "Stromverbrauch"
  unit_time: "h"
  unit_prefix: "m"
  unit: "W"
  round: 0

During each restart there is a small timeframe when the template sensor gives unreasonable states bc it is not initialized. After initialization, it has a valid range. This initialization creates a huge “change” of the template sensor, thus giving a big spike in the derivative sensor which annoys me.

Can this be avoided somehow?

Regards
Ralf

Place a filter sensor between the two. https://www.home-assistant.io/integrations/filter/

@nickrout, this was helpful. Thank you.

- platform: template
  sensors:
    zaehler_strom:
      friendly_name: "Stromzähler"
      unit_of_measurement: "kWh"
      value_template: "{{ (28803.0 + (states('sensor.roh_strom')|float - 28164453.0) /    1000.0  )          |    round(3) }}"

- platform: filter
  name: "Zaehler Strom Filter"
  entity_id: sensor.zaehler_strom
  filters:
    - filter: outlier
      window_size: 4
      radius: 50.0

- platform: derivative
  source: sensor.zaehler_strom_filter
  name: "Stromverbrauch"
  unit_time: "h"
  unit_prefix: "m"
  unit: "W"
  round: 0

2 Likes