Make Sonoff POW R2 redundant to power loss

369 views
Skip to first unread message

Marc R

unread,
Jul 4, 2021, 3:14:37 AM7/4/21
to TasmotaUsers
Hello all,

First time newbie here, please accept my ignorance.

I have numerous POW R2's and the biggest weakness is that they do not keep the values for accumulated kWh during a power loss, which renders them almost useless for a power monitoring scenario, especially in areas with frequent power outages.

To overcome this, I would like to create a rule which stores today's accumulated kWh into a Mem1 variable. If there is a power loss and, as a result, a reboot, the Mem1 variable gets restored into today's accumulated power consumption, before MQTT connection is restored: 

Rule1
   ON tele-ENERGY#Today DO Mem1 %value% ENDON 
   ON Power#Boot DO energyreset1 %Mem1% ENDON


The rule almost works, but when I store Tele-ENERGY#Today into Mem1, it's stored as kWh, but when I load Mem1 back into Tele-ENERGY#Today with the command energyreset1, it's restored in Wh only, resulting in an error by the factor 1000. It seems I cannot use expressions with a pre-compiled binary. Does any of you have an easy fix?

Any help is appreciated.
M

Philip Knowles

unread,
Jul 4, 2021, 4:19:55 AM7/4/21
to Marc R, TasmotaUsers

There are several problems with this – not the least that constantly storing the value in mem will quickly wear the flash memory out.

Tasmota stores the values in Wh (but displays in kWh).

Due to the wearing out of flash memory, If you have ‘numerous’ devices I think your best bet would be to persist the results in something like openHAB or a MySQL database and restoring from there.

 

Phil

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/6efac179-d5bd-42dc-8287-80860082b74en%40googlegroups.com.

 

Marc R

unread,
Jul 4, 2021, 4:28:31 AM7/4/21
to TasmotaUsers
Thanks for your reply. I could change the teleperiod easily once every 10, 30 or 60mins or so, it's not so crucial how often the value gets stored as long as it can persist a power loss. So are you saying there is no way to restore the value in kWh? Cheers!

Philip Knowles

unread,
Jul 4, 2021, 6:26:27 AM7/4/21
to TasmotaUsers, Marc R
Actually, thinking about this there may be a better way. I think (but need to check) that the devices only lose that day's data but it still retains the overall consumption. You could then just store the total at the end of each day.
The restore (and calculation) would still need to be done remotely but you could just send a the EnergyReset value via MQTT.
It's a while since I fiddled with one but I seem to remember that you need to be careful with EnergyReset as putting a value in one can affect the others - putting a value in Day register adds that value to the Total.
However, you can use mult to do a simple multiplication
On tele-energy#today do backlog var1 %value%; mult1 1000 endon
That would solve the restore value issue but not the mem wear out or the Total increasing when it shouldn't.
If you really want to do it in the device (and the device does persist the Total on powercut) I would store the Total in mem1 once a day.
Then on reboot
Var1= Total (Total at power fail)
Sub1 mem1 (Total stored at midnight so var1 now day consumption)
Energyreset3 mem1 (reset the Total to midnight's)
Mult1 1000 (multiply var1 to get Wh)
Energyreset1 var1
Can't guarantee it works
Phil K



From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of Marc R <ma...@rogivue.com>
Sent: Sunday, July 4, 2021 9:28:31 AM
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: Make Sonoff POW R2 redundant to power loss
 

sfromis

unread,
Jul 4, 2021, 8:44:02 AM7/4/21
to TasmotaUsers
What I'm doing with energy monitoring is to manage the data backend (based on the MQTT messages), meaning that in case of power loss, I only lose up to TelePeriood data. I'd certainly not incur significant flash wear to the detriment of device lifetime by trying to keep energy usage updated on the device.

Even without a binary including support for expressions, the Scale command can be quite useful for doing multiply/divide.

LBuch

unread,
Jul 4, 2021, 12:20:00 PM7/4/21
to TasmotaUsers
Re wearing out the flash.  I thought only the Mem variables were written to the flash and not the Var variables.  Was I mistaken??

Philip Knowles

unread,
Jul 4, 2021, 12:22:03 PM7/4/21
to LBuch, TasmotaUsers

The original post was saving consumption to flash on every teleperiod

 

Sent from Mail for Windows 10

 

From: LBuch
Sent: 04 July 2021 17:20
To: TasmotaUsers
Subject: Re: Make Sonoff POW R2 redundant to power loss

 

Re wearing out the flash.  I thought only the Mem variables were written to the flash and not the Var variables.  Was I mistaken??

--

You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

Marc R

unread,
Jul 4, 2021, 11:50:08 PM7/4/21
to TasmotaUsers
Hi Phil, thanks for your reply. The Mult<x> command was exactly what I was looking for! Appreciated.

My rule looks now as follows and it seems to do the job. 

Rule1
     ON tele-ENERGY#Today DO backlog Var1 %value%; mult1 1000; Mem1 %Var1% ENDON


     ON Power#Boot DO energyreset1 %Mem1% ENDON

Will see how quickly it will wear out the flash, but a teleperiod of 10mins means about 50K writes a year and a good quality flash should be able to handle 1M write/read cycles, so that's 20 years. Anyhoo, I'm willing to take the chances thanks to Sonoffs being inexpensive.

As I'm only using the TODAY value for my energy monitoring, I'm currently not concerned about the TOTAL value, but I guess it could be handled somehow by subtracting Mem1 from TOTAL at Reboot.

Thanks again, M

Reply all
Reply to author
Forward
0 new messages