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