Save sensor data for longterm outside of HA database

Hi, I’m usually storing data only for 24h, which works fine. I’ve a sensor “power consumed” that updates every 60 sec.

What I’d like to do is to store the power consumed (one sample every 24h) for 365 days. Ideally in an external file.

Is there anything that I can reuse or is it time for a custom component?
Thanks JKW

1 Like

What about a simple csv file?

Interesting!
Halfway there. is there a way to show all those values in a graph later on?
Jkw

https://community.home-assistant.io/t/daily-energy-monitoring/143436

What do you do with the saved csv file?
Can you re-use it in HA ?
As @JKW pointed out, it would be nice to re-use this data back in HA ie. in case you have deleted you database or for restore purposes?

To be honest: I’d like to see those data in a graph like this


Jkw

Hey guys, a little feedback from my side: I didn’t really find an existing approach so I’ve done some tinkering myself. Maybe you can comment on those ideas:

  1. Storing Data
    As of now I’m using the approach that @VDRainer has posted. that works super easy
  2. Retrieving Data
    I’ve create a very simple custom_component that can read an external file and store the content as attribute of a sensor. So that part is taken care of as well, although it is still a bit hacky.
  3. Displaying Data
    Thats where I’m currently working on. I’ve grabbed the source of the minigraph card and work on some modifications. The idea is to replace the history data with the data from the custom_component attribute. This isn’t working yet but I guess I’ll get that working with a little tweaking.

Is there anything that I can do smarter? Less hacky? Cleaner?
Thanks, JKW

Hello once more, just a little bit of documentation in case someone will ever find this:

  1. Storing data:
    as mentioned, simply by using builtin capabilities of HA
    ESP8266_mqtt_pwm_pir_temp/longtermenergy.yaml at 77fd1561303d457ce53c5348414f938ce85107f8 · KoljaWindeler/ESP8266_mqtt_pwm_pir_temp · GitHub line 1-16

  2. Retrieving Data:
    with the help of a custom component. again
    ESP8266_mqtt_pwm_pir_temp/longtermenergy.yaml at 77fd1561303d457ce53c5348414f938ce85107f8 · KoljaWindeler/ESP8266_mqtt_pwm_pir_temp · GitHub line 18-21 and the custom component is here
    ha_config/custom_components/load_external at main · KoljaWindeler/ha_config · GitHub this will give you a sensor with a nonsense state (I think the states reflects the line count of the file) and an attribute “entities” that stores all lines of the file

  3. Displaying
    a modified version of the mini-graph-card Release v0.10.0 · kalkih/mini-graph-card · GitHub stored here: GitHub - KoljaWindeler/long_term_card
    I’ve removed quite a bit of code and added some new options e.g.:

      - type: custom:mini-graph-card
        name: 'Energy Month'
        display_mode: 'diff'
        formater: 'yyyy-mm-dd'
        unit: 'kWh'
        data_delimiter: ','
        entities:
         - sensor.long_term_power

This will generate a nice graph styled exactly as the rest of the UI.
So thats it … I’ll do some polishing but it works pretty much as expected.
JKW

2 Likes

Nice, I’ll test next week.

Maybe rename the mini-graph-card to long-term-card to avoid conflicts when both cards are used or request a PR to the original dev to have your changes included.

Changing the name and the readme etc is something I’ll do next … including the changes in the existing respository is very unlikely due to the amount of the changes … its basically 50% different :slight_smile:


Just to show a screenshot … the top shows the actual sensor and the two graphs below are reading from the file … EM total month is just showing the data, Energy Monath / Day uses “diff” as display mode, so showing the difference between two days (so the amount that we’ve used within a day)

JKW

Hi Kolja, the link to the custom component is not working anymore. Could you please post it again?Thanks a lot

Hi, link updated above
Kolja

Edit: not sure if this is needed as HA implemented long-term data as well … but my graphs also worked fine for the last year
image

Wow so fast! Thank you very much!

Hi, I did a step forward :slight_smile:
I see now in the sensor attributes all entries of the csv file as follow:

How can I get those data plotted into a graph where x-axis is the timestamp and the y-axis the value? Thanks again for the support

Add the long term card from the link above and configure e.g. like this Screenshot_20211223-174055
Jkw

Sorry, how to install the card?

I copied the long-term-card-bundle.js file into the local folder (www) and then I added it to the resources, but it doesn’t work, cannot recognize the card

that’s exactly how you do it … hmmm restart HA ?