barisdemirdelen / homeassistant-greenchoice

This is a Home Assistant custom component that connects to the Greenchoice API
MIT License
24 stars 6 forks source link

Dates are off #8

Open HomeAssistantPim opened 6 months ago

HomeAssistantPim commented 6 months ago

As Greenchoice publishes the readings one day behind When new data is available from the API, the datetime from Greenchoice should be used and stored, not now().

dedmen commented 5 months ago

This seems to actually be somewhat complicated.. https://community.home-assistant.io/t/allow-sensors-to-report-delayed-data/416733/2 https://community.home-assistant.io/t/energy-counter-delayed-data/332108/14 https://community.home-assistant.io/t/batch-receive-time-series-data/171915 https://community.home-assistant.io/t/insert-state-at-specific-timestamp/329790

One post says that setting the last_updated property would be able to solve it. But https://developers.home-assistant.io/docs/core/entity/sensor/ That does not exist as a property of SensorEntity, so does not seem like that is possible to do just inside the python script.

Maybe this could do it https://community.home-assistant.io/t/import-old-energy-readings-for-use-in-energy-dashboard/341406/9

There is also an example on how to do this with a energy supply https://github.com/janmolemans/huawei_fusionsolar/blob/ea2b58ee8a537b02ab1a367107f77c5960ac9f7a/sensor.py#L101

Seems relatively simple, but I don't know python well enough to implement that.

HomeAssistantPim commented 5 months ago

@dedmen thnx for diving into this and sharing the information. It doesn't look promising (even looks like a serious limitation in the HA architecture). Allthough I'm still a newbie with HA, it's architecture and with Python (yet), I am an IT Professional so let's see if I can help. Please allow me some time to setup a dev environment and try some things.

dedmen commented 5 months ago

I actually got it implemented in C# today.

Sending manual state_update events with last_updated property does work and correctly lists states in history.

Problem is the last state value isn't being set by it. So next day, it's hard to retrieve the last meter state of the previous day.

And the hourly API that I use doesn't have full meter readings, but restarts from zero every day. And total_increasing state class doesn't work when sending manual state_changed events. So to keep proper state, I need to add them to the previous days maximum.

And reading the full meter readings isn't helping because they are rounded to integer, but my daily usage is sometimes less than 1.

Could always store the last maximum in a separate file. Will probably try that tomorrow.

When I got it working sufficiently I'll publish the C# code for it.

dedmen commented 5 months ago

Works great grafik

Its a bit of a mess, but good enough for my needs https://github.com/dedmen/HomeAssistantTools/blob/main/apps/HassModel/HelloWorld/GreenchoiceMeterReader.cs

I could even use it to fill in the last 30 days of historic data.

One weird thing is that there is no lowConsumption data in the hourly meter readings. Even though the overall daily meter readings do include it.

Edit: Actually while this works seemingly perfectly for the history view and statistics. Inside the Energy dashboard, it says there is no data available. Seems like it cannot handle this method of injecting data, and thats a quite important feature :(

But maybe it just doesn't work with past data, gotta let it run for a few days and see what happens.

dedmen commented 5 months ago

Okey a couple days later. Data collection works mostly (I have some trouble with my scheduling). The History view works fine as shown above. But the "Energy" system from home assistant doesn't work at all. I suspect that system needs the SetState API, and doesn't work with state_update events. SetState API doesn't support timestamps in the past. So we're back on missing functionality in HA core. And seeing the above linked community forum threads, while the interest for it exists, we'll probably not see a implementation for it.

https://community.home-assistant.io/t/batch-receive-time-series-data/171915/12

firefox_2024-03-19_16-27-51 firefox_2024-03-19_16-28-09

HomeAssistantPim commented 5 months ago

Hi Dedmen, I didn't even get to setup a dev environment, swampt with other tasks and work :(. Thanx for the updates though. I suspect that the energy dashboards use statistical data rather than sensor data. I read something about that in the links you shared. That information also implied that you would have to rebuild these statistics and this is were I suspect the rabbithole starts because as I've read, building the statistics depends on the type of data store used to store the sensor data (and statistics). I.e. this would require different scripts for different databases (SQL for MariaDB, InfluxQL/Flux for InfluxDB). Allthough it can be done and might not even be that hard at first, I would not recommend going down this path because the solution could easily break upon update or people expecting your integration to work for their particular setup which might be very different from the mainstream HA setups. Many thnx though.