Tmbao / amberelectric-usages

MIT License
5 stars 1 forks source link

Save usage data as sensor attribute data instead #1

Open fhopley opened 9 months ago

fhopley commented 9 months ago

Hi @Tmbao, thanks for making this available. My use case is very close, but not the same as yours - I have sensors for power and energy flow which are available in real time and populate my energy dashboard, but I would still like to get the Amber usage information into my home assistant instance for additional analysis (as it is what is actually being billed).

I've tried to access the usage information directly from the API, but unfortunately the structure doesn't work well with the restful sensor because the json is a list (of dictionaries) rather than a dictionary itself.

Is there an easy way to save the usage information from the API as attributes of a sensor, rather than insert them into the home assistant statistics (either directly, or by simplifying your custom_component)? Attributes something like the structure pasted at the end of this post would be useful as it can be graphed directly with apex charts-card and is also easily manipulated in templates or python scripts.

Thanks, Fred

Usage:
  - type: Usage
    duration: 30
    date: "2023-12-08"
    endTime: "2023-12-07T14:30:00Z"
    quality: billable
    kwh: 0.191
    nemTime: "2023-12-08T00:30:00+10:00"
    perKwh: 23.55448
    channelType: general
    channelIdentifier: E1
    cost: 4.4989
    renewables: 6.199000000000001
    spotPerKwh: 7.85292
    startTime: "2023-12-07T14:00:01Z"
    spikeStatus: none
  - type: Usage
    duration: 30
    date: "2023-12-08"
    endTime: "2023-12-07T15:00:00Z"
    quality: billable
    kwh: 0.209
    nemTime: "2023-12-08T01:00:00+10:00"
    perKwh: 23.34667
    channelType: general
    channelIdentifier: E1
    cost: 4.8795
    renewables: 6.45
    spotPerKwh: 7.65591
    startTime: "2023-12-07T14:30:01Z"
    spikeStatus: none
  - type: Usage
    duration: 30
    date: 2023-12-08
    endTime: "2023-12-07T15:30:00Z"
    …
Tmbao commented 9 months ago

Hi Fred, I think the challenge of sensors is that it's supposed to record real-time data rather than historical data, so it would be tricky to insert historical data to the sensors. Amber usage is published daily and that's also way longer than the usual refresh period of sensors. Probably a workaround is to just have a dummy sensor that refresh every few minutes but does effectively nothing and is only for carrying rolling historical usage attributes 🤔

fhopley commented 9 months ago

Hi @tmbao thanks for the response and thoughts. I solved my problem by taking the API data in via a bit of a convoluted path, I'm sure if I knew more it could be done far more elegantly. The data eventually ends up being stored in a sensor's attributes, which has a size limit. I mostly want it just for yesterday's data for graphing purposes and the data is small enough to fit.

What I probably need to do, if I want to make use of a larger set of historical usage info, is learn how to store (and retrieve) data in a structure better suited for this task (maybe a small local database, a local file, a google sheet etc).