Saving data

Can someone help me to understand how kwh data is collected and stored when not using EmomCMS. I have the Emon library installed on an Arduino. I have added code to generate and accumulate the watt/second data, more code converts that data to kwh data and displays it on a serial terminal. What I am confused about is how the kwh data is stored or processed further because at some point the uint32_t variable accumulating the data must oveflow. Does, say, the kwh data have to be sent to a database for storage at some specified period like every minute or every hour? I use MQTT to send data to Node-Red, Grafana ans InfluxDb under Home Assistant. So, if someone could outline the procedure to correctly store the kwh data I would appreciate the help. I know how to transmit data from the Arduino via MTT.

Welcome, Bob, to the OEM forum.

Essentially, nothing is stored in the Arduino (as per the emonTx libraries and sketches) unless you have added it.

The details: The most recent energy value is retained in dynamic memory and accumulated, the accumulated value is sent periodically somewhere else. In the original concept of the emonTx, this was to the emonCMS server, latterly the data can be sent to many places, including the databases you mention. And that’s all. There is no storage at source of historical values.

Unless you have incorporated or used the code contained in our EEPROM library (emonEEPromLib), the accumulated energy will be lost when the Arduino is powered down for whatever reason. If you are using emonEEPromLib, then a very recent (but not necessarily the most recent) energy has been stored in EEPROM and accumulation will continue with that when the sketch restarts.


is “somewhere else”. emonCMS incorporates, in one of the Input Processes, the facility to carry on from the last value in the event that the newly received value is zero or close to zero; the assumption being that the emonTx has reset and started counting from zero. Our standard for storing all power and energy data is once every 10 s, and in order to avoid empty (null) values in the database, the data is sent slightly faster at 9.95 s intervals (or so), at the expense of a value being overwritten occasionally when it arrived within a millisecond or two of the 10 s window opening - meaning the next value arrives just before the window closes and so overwrites it. This of course applies principally to the fixed interval database of emonCMS and any other that works in the same way. If the database stores the arrival time of the data, this trick isn’t necessary.

Robert, thank you for the prompt reply it has clarified what I was asking. Having read your reply I realise my question wasn’t as clear as it could have been. I am accumulating data on the Uno but not using EEPROM so I am not storing it, thus the data would be lost if the Uno was power cycled. I added code to compute the kwh and cost as a means to have some initail visual representation on the serial monitor.
The part about sending accumulated data every 10 seconds was the part I was looking for. I guess the coding around the database then gets usage data such as hourly kwh, daily kwh etc. I have never used emonCMS so I think that is an option I should try. I believe influx does store the time that data arrives so the ‘trick’ should not be necessary.
As an aside I built the ZMPT101B transformer circuit as per your suggested design elsewhere on the forum. I am really pleased with it and get practically text book secondary voltages. The only issue I had was the (my) calculated calibration value(213.25) resulted in an Arduino ADC value that was too high. Lowering the calibration value to 208.45 fixed that on a 240 Vac supply I get Arduino outputs in the range of 239 to 241. I used the default power factor correction of 1.7 as I assumed that would be close for the ZMPT transformer. Current CT is an Efergy device with the internal components removed and my calculated burden resistor fitted.