Creating a Data-Logger


I am using NodeRed in combination with HomeAssistant.
So far, everything is working as expected - but right now, I am struggling with creating some kind of datalogger like behave.

Home Assistant
In HomeAssistant, I have some "measurement" sensors.
These sensors are counting for example the energy usage over a specific period of time (daily, weekly, monthly and yearly).
Everytime, when such a period is over, the sensor will reset to zero - and start a fresh cycle.

So far - so good, this is what I do expect.

Now, I am searching for a way to save the last sensor value before a new cycle is starting - to get some kind of datalogger.

for example, these are two of these sensors:

solaredge_weekly_kwh_consumption 7.616
state_class: total_increasing
source: sensor.solaredge_consumption_energy_kwh
status: collecting
last_period: 2.947
meter_period: weekly
cron pattern: 0 0 * * 1
last_reset: 2022-08-21T22:00:00.038337+00:00
unit_of_measurement: kWh
device_class: energy
icon: mdi:counter
friendly_name: solaredge_weekly_kwh_consumption
solaredge_monthly_kwh_consumption 10.563
state_class: total_increasing
source: sensor.solaredge_consumption_energy_kwh
status: collecting
last_period: 0
meter_period: monthly
cron pattern: 0 0 1 * *
last_reset: 2022-08-21T11:22:27.310930+00:00
unit_of_measurement: kWh
device_class: energy
icon: mdi:counter
friendly_name: solaredge_monthly_kwh_consumption

Now, whenever the sensor "monthly_kwh_consumption" will reset to 0.00 - I would like to store its last value, together with the information of the month, example "August 2022 - VALUE"
either in a file, or in a Database table - or whatever else could be used...

With this information, I would like to create a dashboard in NodeRed, providing a statistics and history table with all these information.

Unfortunately, I don't know how I could acheive that the sensors value will be saved before it will be reseted.
And I don't know exactly, when HomeAssistant does reset the sensor (I only have the attributes & Information: "last reset" and "last_period" which does (as far as I understand) includes the sensors value from the last measurement period.

I assume you have an SQL database, but you might prefer a time series database such as Influxdb.

It makes sense to write every reading to the database as you receive it.
You can always retrieve the data grouped by month, but if you want to show readings in more detail, the data will be there.

However, if you really want the db to contain monthly values, still write each new meter reading but make the table's unique key include the last_reset date and use ON DUPLICATE KEY UPDATE ...

Something like this (untested):
INSERT INTO mytable(sensorname, lastresetdate, consumption) VALUES (:friendly_name, :last_reset, :solaredge_monthly_kwh_consumption) ON DUPLICATE KEY UPDATE consumption = :solaredge_monthly_kwh_consumption;

edit: fixed typos.

thanks :slight_smile:
Since HomeAssistant is already writing sensor values to its own Database, it is not required to save every reading / change.
I could also write the information into an influxdb and use graphana for creating a dashboard - but honestly speaking, that was too much for what I wanted to do :slight_smile:

Your second attempt with writing the information into the DB by using: "on duplicate key update" sounds reasonable - and pretty much what I want to acheive.
I will start with this and do some more tests.


Well I'm not familiar with Home Assistant or it's database, but can't you extract the monthly summary from that?

the sensors keep a history in their own Database - but this would depend on how long history will be kept in the database depending on the system, the amount of days until old data will be purged from the database might be different from system to system - but even with a larger history, this is not exactly what I want / and need.

The main purpose is to access this "custom history" with other tools - or create a dedicated Dashboard, which I can share with others - who should not have access to my whole HomeAssistant setup.

I know, this request is a bit exotic - since most users would not need such a setup...

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.