Sonoff POW - How to read Voltage, Current, Power,

I wanted to share my experience with Sonoff POW here.
I bought 3 Sonoff POW units a few months back and finally flashed them with Sonoff-Tasmota firmware (v5.9.1).
I followed the numerous examples of how to setup and run MQTT in OpenHAB.
I then set up the Sonoff device using the build in WEB page with the following configuration:
Topic: sonoff
Full Topic: sonoff_1/%prefix%/%topic%/
Telemetry period: 300

My goal was to get the voltage/current/power/energy information from the Sonoff POW device periodically updated (300 = 300s -> every 5 minutes) and also get an immediate update when the device is switched ON or OFF. Here is my set-up (please note the gSonoff group in the definition, I’ll get to that later):

items:

// The following definition does 3 things:
// 1. output ON/OFF command to Sonoff POW device to turn ON/OFF relay based on item switch state
// 2. output "Status8" command to Sonoff POW device to retrieve voltage/current/power/energy info
// 3. input status feedback from Sonoff POW device to update the relay state 
//    (this also updates the switch state in the case you press the button on the device itself)  
Switch Sonoff_1_Switch	(gHOLights,gSonoff)	{mqtt="	>[broker:sonoff_1/cmnd/sonoff/power:command:*:default],
													>[broker:sonoff_1/cmnd/sonoff/status:command:*:8],
													<[broker:sonoff_1/stat/sonoff/POWER:state:default]"}

// The following definitions retrieve the info from the Status8 command
// as well as from the periodic TELE feedback and stores the results in the corresponding items
// Voltage in V
Number Sonoff_1_V  { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Voltage)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Voltage)]" }
// Power in W
Number Sonoff_1_P  { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Power)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Power)]" }
// Current in A
Number Sonoff_1_I  { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Current)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Current)]" }
// Energy for today in Wh
Number Sonoff_1_E  { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Today)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Today)]" }
// Energy since last reset to 0 in Wh
Number Sonoff_1_Et { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Today)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Total)]" }

// The following definition sends a Status8 command to force sending of voltage/current/power/energy back
// this switch can be placed on the sitemap/HABpanel to force a manual update of the info
Switch Sonoff_1_Get_VPI		{mqtt=">[broker:sonoff_1/cmnd/sonoff/status:command:*:8]"}

The downside of sending Status8 command pretty much at the same time that the switch state change happens, is that the voltage/current/power info is not quite accurate, since the device did not have enough time for sampling. I therefore created a new groupl in the items file:

Group:Switch 	gSonoff	"Group Sonoff"				// All Sonoff devices

Using this group definition I created a rule to send a Status8 command a short time after a Sonoff device switched ON or OFF:

import java.util.concurrent.locks.ReentrantLock  // place this at the top of your rules file

var ReentrantLock lock = new ReentrantLock() // place this at the beginning portion of your rules file where vars are defined

// ********************************************************
// Sonoff POW data update upon state change
// ********************************************************
rule "Sonoff POW data update"
when
    Item gSonoff received update
then
    // using the lock code here will prevent that this rule runs several times
    // in the case where you have multiple Sonoff devices in the group and only one of them switches
	var gotLock = new Boolean(lock.tryLock())

	try{
	    if(gotLock){
        	Thread::sleep(2000)  // wait for 2s to let voltage/current/power stabilize
			if (Sonoff_1_Get_VPI.state!=ON)  // toggle the switch to initiate sending the Status8 command
        		Sonoff_1_Get_VPI.sendCommand(ON)
    		else
        		Sonoff_1_Get_VPI.sendCommand(OFF)
            // add more if ... else ... statements here for your other Sonoff devices
	    }
	}
	finally{
	    if(gotLock){
	        lock.unlock();
	    }
	}
end

This works pretty well. If you have multiple devices, just add more definitions:
Sonoff_1_Switch, Sonoff_2_Switch,…
Sonoff_1_V, Sonoff_2_V,…
Sonoff_1_Get_VPI, Sonoff_2_Get_VPI,…
… you get the idea.

Hope this helps anybody; with this additional info lots of more rules can build on them.

My next project will be to use the current/power info to control my entertainment system using ZMOTE.
A while back, I created ZMOTE control of my TV/Receiver/Cable system, but I do not have any feedback for power on/off status for the devices (When you use the remote control the human is the feedback device…). The ZMOTE RF commands only allows a toggle of the power.
Now I can combine the Sonoff current/power info and find out if I toggled a particular device ON or OFF :grin:

6 Likes

Hi,

I would like to send MQTT messages to my Pow as well, but unfortunately it does not recieve them when sent through openhab. I created a switch:

Switch Pow “Pow Test Switch MQTT” (gBM) {mqtt=" >[mysensors:sonoff_1/cmnd/sonoff/power:command::default],>[mysensors:sonoff_1/cmnd/sonoff/status:command::8],<[mysensors:sonoff_1/stat/sonoff/POWER:state:default]"}

I think there is sth. wrong with my MQTT command, but I don´t know what and where…

When I use the swith to send the command, my broker recieves this:

/openHAB/out/Pow/command

So the message gets transformed. How do I manage to get the message delivered to the Pow?

Hey guys, did you check those sources:

That´s where I started…but forgot to return…

Thanks for reminding, all my questions are answered now!!

hi thanks for your help here,
is it possible that the voltage, power… gets updated every 5min?
because right now it only shows the consumption at start.
maybe in the rule file?
thanks a lot
mike

Yes it is possible (I assume you implemented the items as I showed above).
First of all, you need to configure your Sonoff POW to send out a “TELE” MQTT messages at an interval.
You can do that by connecting to your Sonoff using a Browser. Type in the URL the following:

Sonoff_1.local

Please note to replace “Sonoff_1” with the device name that you assigned to your device (you can normally check that in your router setups, when you look for connected devices)
This is what you should see when you are connected to the device via browser:

Then click on “Configure Logging”
You should now see:

Here you can change the “Telemetry period” value. In my case it is the default: 300 = 300s (i.e. every 5 minutes)

Save the configuration and after several seconds, your device should then send out a MQTT message with all the power/voltage/current info every 5 minutes. (check with a MQTT reader, or whatever it is called, that you see those messages).

Ok, we are half way there…

Now you need to make sure that have the correct setup in your item definition for Sonoff_1_V, Sonoff_1_P, Sonoff_1_I,… (see definitions of those further up).

One thing that is important though: The JSON format has changed with later versions of the Tasmota software. The above definition applies to version 5.9.1 that I used when posting.
I recently updated to the newer 5.11.1 version and the JSON format changed as shown below:

v5.9.1

Number Sonoff_1_V { mqtt="	<[broker:sonoff_1/tele/sonoff/ENERGY:state:JSONPATH($.Voltage)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusPWR.Voltage)]" }

v5.11.1

Number Sonoff_1_V { mqtt="	<[broker:sonoff_1/tele/sonoff/SENSOR:state:JSONPATH($.ENERGY.Voltage)],
							<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusSNS.ENERGY.Voltage)]" }

As you can see:

/ENERGY:state:JSONPATH($.Voltage)
/STATUS8:state:JSONPATH($.StatusPWR.Voltage)

changed to

/SENSOR:state:JSONPATH($.ENERGY.Voltage)
/STATUS8:state:JSONPATH($.StatusSNS.ENERGY.Voltage)

This definition change applies to Voltage,Current,Power,Energy,…

Have fun…:wink:

2 Likes

BTW, one more thing. I recently updated the automatic Voltage,Current,Power update that executes upon Sonoff Switch state change of any of the connected Sonoff devices (thanks to rikoshak’s plentyfull examples). This will save a lot of code additions when you add new devices :grinning:

For this to work, make sure that all your switch items (e.g. Sonoff_1_Get_VPI, see above) in your item definition belong to the same group (in my case I created a new group “gSonoff_Get_VPI”) and the actual Sonoff switch items to another group (in my case Sonoff_1_Switch,Sonoff_2_Switch,… belong to group “gSonoff”).

The update for all “…_Get_VPI” items now looks like this:

Group:Switch	gSonoff_Get_VPI		"Group Sonoff get Voltage,Current"

Switch Sonoff_1_Get_VPI		(gSonoff_Get_VPI)
	{mqtt=">[broker:sonoff_1/cmnd/sonoff/status:command:*:8]"}

With this in place, the new rule definition looks like this:

// place the import statement at the top of your rules file
import java.util.concurrent.locks.ReentrantLock

// place this variable declaration also on top of the rules file
var ReentrantLock SonoffPowUpdatelock = new ReentrantLock()

// ********************************************************
// Sonoff POW data update upon state change
// ********************************************************
rule "Sonoff POW data update"
when
    Item gSonoff received update // run this when any Sonoff switch turns ON or OFF
then
	var gotLock = new Boolean(SonoffPowUpdatelock.tryLock())

    // please note, that with this lock, you might loose updates when a Sonoff gets switched while executing this rule
    // (with the sleep(s) it'll take a while to get out)
	try{
	    if(gotLock){
        	Thread::sleep(2000) // give Sonoff some time to sample all data after switching
				gSonoff_Get_VPI?.members.forEach[sw|
					sw.sendCommand(ON)
					Thread::sleep(50)
					sw.sendCommand(OFF)
					Thread::sleep(50)
				]
	    }
	}
	finally{
	    if(gotLock){
	        SonoffPowUpdatelock.unlock();
	    }
	}
end
1 Like

Hey,
thanks a lot for your help!!
I will try that tomorrow.
So i get all consumption right, i‘ve Read the log file out and changed the json format.
But my problem is that the energy total for example stays the same on habpanel and basicui. Only if I push the item switch Sonoff get vpi manually the status updates in habpanel.
Do you think I should write something in the rule file that the Sonoff get vpi switch gets triggered for example every 5 min.?
If yes how would be the code?
Again thanks man you really help me out here!

Greets
Mike

No need for a rule that triggers periodically.
The “TELE” message from the Sonoff will update your Openhab values automatically (assuming your JSON TELE definition is correct…)

I just flashed sonoff POW R2 and in console it is showing proper energy data.
I am having trouble defining the item in my items file to get current and voltage. Have spent hours on forums but of no use.
Secondly what command can i give in it’s web server console to view energy data. Please help.
I am able to get it’s power state, RSSI value etc but not able to get Current and Voltage values. Maybe my item definition is wrong ?

Number sonoffPOW1_Voltage “Voltage [%d]” (gVoltage){ mqtt="<[mosquitto:tele/sonoffPOW1/STATUS8:state:JSONPATH($.StatusPWR.Voltage)]" }

or
Number sonoffPOW1_Voltage “Voltage [%d]” (gVoltage){ mqtt="<[mosquitto:tele/sonoffPOW1/SENSOR:state:JSONPATH($.Energy.Voltage)]" }

There are several things to check (to make sure everything matches):

  1. Check with a MQTT client (I use " MQTT.fx" on an iMac) that you can connect to your broker and are able to subscribe to your topic to see that the data is being published by the sonoff device.
    In my setup the broker name is openhab2 and the topic I subscribed to is sonoff_1/# (the /# allows me to see all the topics for this device).
    In my setup I see the following:

If this does not work, there are two things that might be wrong:
a.) You don’t have the right information for your broker setup (name, user, password,…) or your broker is not running/installed, so check your broker setup (see MQTT Binding (v1.11) Getting Started 101)
b.) Your sonoff MQTT configuration is wrong/missing. Check that by using a browser and log into you sonoff device. Mine is setup like this:

Note, that your “User” and “Password” entry need to match your definition of the broker (see a.) above)
Also write down the “Topic” and the “Full Topic” you entered in this setup, you’ll need that later for your Openhab mqtt.cfg file.

  1. You need to solve 1. above, otherwise do NOT proceed.
  2. Configure your mqtt.cfg file. Mine looks like this:
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
#broker.url=tcp://192.168.1.10:1883
broker.url=tcp://openhabianpi.local:1883
# broker.url=ssl://192.168.1.10:8883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#<broker>.clientId=<clientId>
broker.clientId=openhab

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
broker.user=openhabian

# Optional. Password to authenticate with the broker.
broker.pwd=*******

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
broker.qos=0

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
broker.retain=false

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
broker.async=true

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

Please note, that I blanked out my password (broker.pwd=*******), you’ll have to enter your real password instead of the *******
4. As you can see in the first screen shot, the sonoff publishes the voltage information to a topic named: “sonoff_1/tele/sonoff/SENSOR” and the topic actually looks like this:

{"Time":"2018-10-13T10:05:55","ENERGY":{"Total":38.104,"Yesterday":3.220,"Today":0.707,"Period":0,"Power":169,"Factor":1.00,"Voltage":118,"Current":1.431}}

To get the voltage out of this topic I have defined my item as follows:

Number Sonoff_1_V	"S1_V [%.0f V]"
	{ mqtt="	<[broker:sonoff_1/tele/sonoff/SENSOR:state:JSONPATH($.ENERGY.Voltage)],
				<[broker:sonoff_1/stat/sonoff/STATUS8:state:JSONPATH($.StatusSNS.ENERGY.Voltage)]" }

You will see several things here:

  • ignore the second line that contains “Status8”, I use another method in addition to tele to get data from the sonoff device.
  • “broker” matches the name I defined in the mqtt.cfg file
  • “sonoff_1” matches the first item in the “Full Topic” definition that I entered in the Sonoff setup (accessed through login into the sonoff device as admin, and of course saving it).
  • “tele” matches what the sonoff devices uses to publish its data (assuming you have “logging” in the sonoff device active with a reasonable " Telemetry period")
  • “sonoff” matches “Topic” entry in the sonoff device setup
  • “Sensor” matches what the sonoff device transmits
  • “$.ENERGY.VOLTAGE” matches what the sonoff device transmits.

Hope that helps…

1 Like

Hi guys,

so the end of the year is near and i was wondering how i could reset my POW Energy Year.
So it calculated the whole year and now at 01.01 i wanna start with a fresh calculation.

thanks alot

sdfd

Hi,
Would you be able to share udpate to your item file for the Sonoff POW that is mqtt 2.4 compliant?

Hi @epicurean,

I have not upgraded to mqtt 2.4 yet, I am still using the OpenHab 1.x binding for MQTT.

Hello Guys!
I am just tasting tasmota. I have several Pow R2 which I wish to use them as a power monitoring.
There are two Kwh data, today and yesterday. I am not interested in yesterday’s power consumption as I link my pow to node-red and all the data logged into a csv file. So what I try achieve, is there a way to change the yesterday data to weekly or monthly.
Node-red flow is available on request.
Thanks!

2 Likes

HI!

Could you send to me this FLOW?

thanks

1 Like

Hi guys.
Thanks for the solution, it works very well. I would like to add electricity consumption data and costs for this month, last month, this year and last year, someone could show me the way how I can do it.

Best regard emiks.

Pisti already mentioned it but no one answered