SensorEntity: Insert historical statistics data but also keep sensor up-to-date

Coming from this post here Import old energy readings for use in energy dashboard - #49 by Redwid I started to enhance an integration for smart meters, via a web API: WienerNetzeSmartmeter/sensor.py at c995aa2fb69fc7efdcc331171b96f31be9e9fe57 · reox/WienerNetzeSmartmeter · GitHub
Basically, it collects the hourly consumption data from the past days (that API inserts data with 24h+ delay), then I insert them via async_import_statistics.

So far, it works very nicely but there is this one problem, that I can not set the current state of the sensor itself, otherwise the statistics data gets totally messed up.
If I set the state on the SensorEntity itself, I get negative consumption values or other weird statistics.
What I understood is, that you should use a dummy sensor entity on which the historical statistics data is inserted - what is what I do.
However, it would be nice to have the total consumption as a state on the sensor itself as well. From my naive view point, I guess that I need to prevent the statistics module to run when I set the state on the sensor, such that the only statistics data is the one I insert. Or is this in general not possible to maintain a valid sensor value and insert historical statistics data?

1 Like