Tasmota is an opensource Sonoff compatible firmware which supercharge the the Sonoff Smart Switches with tons of functionality and options as compared to the stock firmware. Sonoff Pow R2 is a featured smart switch with power monitoring capabilities and combination of Sonoff Pow R2, Tasmota and Home Assistant / Hassio takes it to next level of true automation. In this post we’ll discuss a common issue i.e. Preserve Energy Data on Sonoff Pow R2 using Tasmota.
Preserve Energy Data on Sonoff Pow R2 using Tasmota:
In developing countries where load-shedding is a common practice, Sonoff Pow R2 with Tasmota experiences some sort of imperfectness i.e. incorrect energy consumption data. The reason is, to avoid quick ware of Flash memory, every bit of energy consumption is not preserved on Flash memory inside Sonoff Pow R2 as Tasmota stores the data periodically e.g. every mid night. If a Sonoff Pow R2 installed with Tasmota experiences power loss, it restores the last saved values of energy consumption on boot and report a totally incorrect values to the dashboard / user interface. How can we make this up? It is the question asked too frequently with no practical resolution of the issue. Here is a workaround to overcome this problem with a minute earning curve.
In this solution we’ll use Node Red, a visual wiring tool and Tasmota builtin Rules to fix the problem. The solution comprises of two tasks.
- Storing the energy values on a global variable in Node Red and Feed the Node Red stored energy values to Sonoff Pow R2 right after it connects to the server.
- Reset the energy values inside Sonoff Pow R2 right after boot using Tasmota Rules.
Before we start, a quick question usually comes in mind, We have a lot of Sonoff Pow R2 installed in our home, should we repeat these steps for each Sonoff Pow R2? The answer is a combination of NO & YES!. We have to work out only one flow with Node Red and it will work with all of the Sonoff Pow R2. As far as our YES is concerned, we have to add a Rule to each Sonoff Pow R2!! Just one time set up!!.
1) Storing the energy values with Node Red:
A Node Red Flow is composed of MQTT IN & OUT, Switch, JSON Parsing and Function nodes along with optional debug nodes. MQTT message is received on # topic which means it will get each and every payload published at any topic over MQTT. Payload is tested for JSON data type parsed into JSON objects. Real processing is done inside the Function node where energy values are saved, compared and fed back to different Sonoff Pow R2 using MQTT OUT node.
var topic = msg.topic; split = topic.split('/'); topic = split[1]; var payload = msg.payload; var database; var energy; if(payload.ENERGY === undefined) { return; } else { var ENERGY = payload.ENERGY; database = context.global.energy; if(database === undefined) { database = {}; update(); return; } else { energy = database[topic]; if(energy === undefined) { update(); return; } else { if((ENERGY.Total < energy.Total) || (ENERGY.Yesterday < energy.Yesterday) || (ENERGY.Today < energy.Today)) { msg.topic = "".concat("cmnd/",topic, "/EnergyReset1"); msg.payload = energy.Today*1000; node.send(msg); msg.topic = "".concat("cmnd/",topic, "/EnergyReset2"); msg.payload = energy.Yesterday*1000; node.send(msg); msg.topic = "".concat("cmnd/",topic, "/EnergyReset3"); msg.payload = energy.Total*1000; } else { update(); return; } } } } function update() { database[topic] = {"Total":ENERGY.Total, "Yesterday":ENERGY.Yesterday, "Today":ENERGY.Today}; context.global.energy = database; } return msg;
In Node Red, energy values are compared with previously stores values and if found comparatively less then stored values are published back to the Sonoff Pow R2 using node.send(msg) command.
2) Reset the energy values using Tasmota Rules:
Tasmota support Rules are very handy to implement automation at unit level without having to add dedicated code or using external solutions. Tasmota periodically stores energy data inside Flash memory of Sonoff Pow R2 and these values are restored at each reboot. Which means if latest values are not yet stored and Sonoff Pow faced a power loss then energy data values will not be the latest. In order to retrieve the last published values from server, We need to reset the energy values at every boot.
- Navigate to the Tasmota web interface of Sonoff Pow R2 and add the following rules in Console followed by Enter.
Rule1 on System#Boot do Backlog EnergyReset1 0; EnergyReset2 0; EnergyReset3 0 endon
- Do not forget to activate the rule by rule1 1 command.
- Also change the tele period by TelePeriod 10 command to set it to the minimum i.e. 10 seconds.
you could achieve the same thing with a simple Tasmota rule only, without the help of Node Red:
rule1 ON tele-ENERGY#Today DO backlog Var1 %value%; mult1 1000; Mem1 %Var1% ENDON ON Power1#Boot DO energyreset1 %Mem1% ENDON
rule1 1
MEM is a persistent variable which means it will be store somewhere in flash / EEPROM. Bloating the Flash / EEPROM every few seconds will end up the Flash / EEPROM so fast. It might not be a good solution in my personal opinion.
Regards,
Waqas Ahmed
This flow is incorrect (I get:Error: Input not a JSON Array)
I cant get it working. Can you share flow of this? I tried to do this alone, but from after function there is no output in debug node.