I made a bash script, all by myself. It gets Volts, Amps, Watts & usage, cos-phi values and puts it in Domoticz, every minute.
It's made for a Smappee with one usage pickup coil, so no 3-phase, no solar.
It runs every minute, by crontab, on the Domoticz host. (Raspberry)
Code: Select all
# THIS single script takes Smappee data and puts it in Domoticz dummy sensors
#VERSION 1.1 (changed the AMPS sed line a bit, because sed was off by a backslash just before it, in the smappee output)
#VERSION 1.2 (changed the WH handling, because earlier, it didn't initialize properly, somehow. ...and some minor things...
# uses CURL, for talking http
# uses SED, for sifting out what I need from Smappee's output
# uses BC, for calculating cumulative usage
SMAP=`curl http://smappeeip/gateway/apipublic/reportInstantaneousValues`
ERR=`echo $SMAP|grep -c Couldn`
# 'Couldn' means that curl couldn't do what's needed, for example, when Smappee's network connection is temporarily out
# So if no error, we continue to try to get values from Smappee
# Else, we get the previous values stored in /tmp
if [ $ERR != 1 ]
then
ERR=`echo $SMAP|grep -c error`
# 'error' means that Smappee answered with an error, most likely, not logged in
if [ $ERR == 1 ]; then curl -H "Content-Type: application/json" -X POST -d "admin" http://smappeeip/gateway/apipublic/logon ; SMAP=`curl http://smappeeip/gateway/apipublic/reportInstantaneousValues`; fi
VOLTS=`echo $SMAP |sed -e 's|.*voltage=\(.*\)|\1|' -e 's|\(.\{1,5\}\).*|\1|'`
WATTS=`echo $SMAP |sed -e 's|.* activePower=\(.*\)|\1|' -e 's|\(.\{1,6\}\).*|\1|'`
AMPS=`echo $SMAP |sed -e 's|.*urrent=\(.*\)|\1|' -e 's|\(.\{1,4\}\).*|\1|'`
COSF=`echo $SMAP |sed -e 's|.* cosfi=\(.*\)|\1|' -e 's|\(.\{1,2\}\).*|\1|'`
else
VOLTS=`cat /tmp/volts`
WATTS=`cat /tmp/watts`
AMPS=`cat /tmp/amps`
COSF=`cat /tmp/cosf`
fi
# just in case there's still nothing there...
if [ 1$VOLTS == 1 ]; then let "VOLTS=230"; fi
if [ 1$WATTS == 1 ]; then let "WATTS=460"; fi
if [ 1$AMPS == 1 ]; then let "AMPS=2"; fi
if [ 1$COSF == 1 ]; then let "COSF=1"; fi
# Save this. If Smappee is not in mood at next run, we'll use these instead.
echo $VOLTS >/tmp/volts
echo $WATTS >/tmp/watts
echo $AMPS >/tmp/amps
echo $COSF >/tmp/cosf
#Put the voltage, amps and cos phi to Domoticz
curl "http://localhost:8080/json.htm?type=command¶m=udevice&idx=372&nvalue=0&svalue="$VOLTS
curl "http://localhost:8080/json.htm?type=command¶m=udevice&idx=376&nvalue=0&svalue="$AMPS
curl "http://localhost:8080/json.htm?type=command¶m=udevice&idx=377&nvalue=0&svalue="$COSF
# CUM is the cumulative watthour value
# Domoticz wants watthours, so that's why watthour
# To make sure we have a CUM value:
[ -f /tmp/wh ] && echo wh present || echo 0.0001 >/tmp/wh
CUM=`cat /tmp/wh`
# In case there's still no good CUM yet..
if [ 1$CUM == 1 ]; then let "CUM=1"; fi
# By resetting the WH cumulative like this, we might see strange jumps in the kWh usage graphs.
# But if WH fails (/tmp/wh empty), Domoticz also stops showing and registering WATTs and the log fails.
#
#Calc new WattHour value
WH=`echo $WATTS\*0.0166667+$CUM|bc`
echo $WH >/tmp/wh
# the 0.0166667 number is 1/60, meaning, we take 60 measurements per hour, adding to the cumulative.
# Now put the watts and the watthours to Domoticz
CURLURL="http://localhost:8080/json.htm?type=command¶m=udevice&idx=371&nvalue=0&svalue="$WATTS";"$WH""
curl $CURLURL
Everywhere where it says smappeeip, the ip-address of your smappee should be entered.
There's a line with "admin", which is the default password for the Smappee. Replace that with your own local Smappee password.
In the json curl statements, replace the IDX values for your own.
If your Domoticz requires authentication, you'll have to add that to the json curl statements.
My /tmp is mounted on RAM, so it won't wear out my SD card.
This script might contain lines that might hurt the eye of the experienced Bash script programmer. I am not an experienced Bash programmer. I'm just happy it runs without errors on my Raspberry.
Use it at your own risk, if you dare. Or use my ideas to improve on your own script.
Good suggestions are very welcome.
An issue with this...
Domoticz does not store the average of every five measurements per 5 minute period in it's log. It only takes the last of the five measurements and stores that in it's log. Suppose that my background usage is 300W and at 18:46 I turn on hi-power electrical equipment (oven, room heater) and I turn it all off at 18:49. At 19:00, Domoticz would have registered that in the Watt / Usage log? Yes and no. The Watt line will not show any peak. However, the usage bars will be registering it.
If I would average it out in my script, then I can send my averaged data to Domoticz once in five minutes. I can even take measurements every five minutes, instead of every minute. Then, for sure, Domoticz won't know about my short electricity usage binges. So, I've chosen to send to Domoticz every minute. That will make the meters on the utility tab more like 'real-time' than it would be with a five minute interval.
The only workaround would be to use different meters for logging and for displaying 'real-time' Watt values and feeding those with optimal feeds at optimal intervals.
EDIT:
The cos phi value should be divided by 100. I forgot that and I was in no mood to:
- add that division
- delete and recreate the custom dummy sensor for it, in order to delete the history, because that transition would look weird, in there.
- put the new idx value in the curl statement.
- losing my history.
I think I'll just put a % in the axis label, to make it mathematically correct.
EDIT:
- Corrected a mistake and put a version number in the code.
- There must be a fault in my error-checking code; the lines that test if a string is empty. ...testing...
EDIT:
- I think I fixed my error, so, new version of the script is pasted, above.