Struggling to understand how to get average data from a sensor


I have a sensor

power-monitor-ct PZEM-004T V3 Power
257.00	state_class: measurement
unit_of_measurement: W
device_class: power
friendly_name: power-monitor-ct PZEM-004T V3 Power

i’m trying to understand how to get data for history out of the sensor into a template.

What i’m trying to write is something that can calculate what the estimated power usage will be today at a given hour, based on what the average value was for a previous day for the same hour…

I can’t seem to work out what the approach is to read a sensor | get average data for time x, period y

I’m not after graphs, just the real data in a template.

I hope i’m just missing something obvious.

Thank you

You’re going to need to access the database…

It might be possible with a sufficiently sophisticated SQL sensor.

But you may want to check out the Data Science portal.

Afraid I have long lost the config but I save to the influx db add on and did something similar using a query to create a rolling average sensor for a past time period. Just searched and there are some examples around.

Good luck!

The statistics integration would normally be used to create a sensor that reports an average of another sensor over a specified time period. However that integration doesn’t support specifying a start or end for that window, which is why you’re being told that you have to access the database.

However, there is a custom integration called ha_average which can do what you need so that you don’t have to mess with SQL code. There is also a thread on this forum created by the author if you need assistance with it.

Once you install the integration, here is an example config that should get what you need:

- platform: average
    name: 'My device average power over previous hour yesterday'
      hours: 1
    end: "{{ now() - timedelta(days=1) }}"
    unique_id: "1cf4fbfc-8e6c-4c70-a39e-a4e41074ceb1"
      - sensor.my_device_power

1 Like