Last few weeks I spent some time to read the values of my Plugwise stretch and smile. With use of some examples in the forum I made my own scripts. Because I have two Solar panel arrays it was difficult to get the total produced kWh value. For other users who like to read their Plugwise equipment here is the code:
Please read the comments in the original code from ThaBoo (thanks for that !!!) to make the necessary translation files. I splitted them in a "consumed" and "produced" file. Be aware that the translate.sensorsP file ONLY contains the MAC adresses of your stealth modules. Otherwise the values of the other sensors are overwritten.
http://www.domoticz.com/forum/viewtopic ... ort#p34352
Please note: I am not an expert in writing scripts, this means that I am sure the code can be optimized and it will costs you more CPU cycles than necessary
The smile script:
Code: Select all
#!/bin/bash
#Parameters section
#Folders and files
scriptfolder=/home/pi/domoticz/scripts/
#The tempFile should preferably be on a ramdisk to avoid SD card wear
tempFile=/var/tmp/ram0/tempPlugwiseRead
#Devices
#Plugwise Smile Parameters
smileIp=<ip>
smilePassword=<password>
#Domoticz Parameters
domoticzIp=192.168.2.40
domoticzPort=8080
#domoticzUser=[username for domoticz]
#domoticzPassword=[password for domoticz]
gasMeterID=42
electricityConsumptionMeterID=40
electricityProductionMeterID=41
#end of parameters section, no further customization should be needed from here
# Get the P1 Smartmeter readings from the Smile unit
IFS=$'\r\n' result=(`curl -s --user smile:$smilePassword $smileIp"/core/modules" | \
xmlstarlet sel -I \
-t -m "/modules/module" \
-v "services/electricity_point_meter/measurement[@directionality='produced']" -n \
-v "services/electricity_point_meter/measurement[@directionality='consumed']" -n \
-v "services/electricity_cumulative_meter/measurement[@directionality='produced']" -n \
-v "services/electricity_cumulative_meter/measurement[@directionality='consumed']" -n \
-v "services/gas_interval_meter/measurement[@directionality='consumed']" -n \
-v "services/gas_cumulative_meter/measurement[@directionality='consumed']" -n \
| sed '/^$/d' `)
# uncomment for debug
#echo "result 0 =" "${result[0]}" "Electricity_point_meter produced"
#echo "result 1 =" "${result[1]}" "Electricity_point_meter consumed"
#echo "result 2 =" "${result[2]}" "Electricity_cumulative_meter produced nl_offpeak"
#echo "result 3 =" "${result[3]}" "Electricity_cumulative_meter produced nl_peak"
#echo "result 4 =" "${result[4]}" "Electricity_cumulative_meter consumed nl_offpeak"
#echo "result 5 =" "${result[5]}" "Electricity_cumulative_meter consumed nl_peak"
#echo "result 6 =" "${result[6]}" "Gas_interval_meter"
#echo "result 7 =" "${result[7]}" "Gas_cumulative_meter"
resultTprod=$(echo ${result[2]} + ${result[3]} | bc)
resultTcons=$(echo ${result[4]} + ${result[5]} | bc)
# uncomment for debug
#echo "resultTprod =" "${resultTprod}" "Total produced nl_offpeak + nl_peak"
#echo "resultTcons =" "${resultTcons}" "Total consumed nl_offpeak + nl_peak"
# Store the values over the json interface in Domoticz
# For the gas meter multiply readings by 1000 since domoticz can only store integers and since bash can't handle Real numbers bc needs to be called
curl -s $domoticzIp":"$domoticzPort"/json.htm?type=command¶m=udevice&idx="$gasMeterID"&nvalue=0&svalue="`echo ${result[7]}" * 1000 " | bc -l`";"`echo ${result[7]}" * 1000 " | bc -l`
# Store the values for the electric Khw meter and real time power meter for consumption
curl -s $domoticzIp":"$domoticzPort"/json.htm?type=command¶m=udevice&idx="$electricityConsumptionMeterID"&nvalue=0&svalue="${result[1]}";"${resultTcons}
# Store the values for the electric Khw meter and real time power meter for redelivery (teruglevering)
curl -s $domoticzIp":"$domoticzPort"/json.htm?type=command¶m=udevice&idx="$electricityProductionMeterID"&nvalue=0&svalue="${result[0]}";"${resultTprod}
Code: Select all
#!/bin/bash
#Parameters section
#Folders and files
scriptfolder=/home/pi/domoticz/scripts/
#The tempFile should preferably be on a ramdisk to avoid SD card wear
tempFile=/var/tmp/ram0/tempPlugwiseRead
#Plugwise Stretch parameters
stretchIP=<ip>
stretchPassword=<password>
#Domoticz Parameters
domoticzIp=192.168.2.40
domoticzPort=8080
#domoticzUser=[username for domoticz]
#domoticzPassword=[password for domoticz]
#end of parameters section, no further customization should be needed from here
# Read sensor data "consumed" from the plugwise zigbee network trough the stretch API.
curl -s --user stretch:$stretchPassword $stretchIP"/core/modules" | \
xmlstarlet sel -I \
-t -m "/modules/module" \
-v "services/electricity_point_meter/measurement[@directionality='consumed']" -o ";" \
-v "services/electricity_interval_meter/measurement[@directionality='consumed']" -o " " \
-v "protocols/network_router/mac_address" \
-v "protocols/network_coordinator/mac_address" -n \
| sed 's/\;\ //g' | sed 's/\ \ /\ /g' | sed 's/^ *//g' | sed 's/\;\;0\ //g' | sed '/^$/d' | awk ' { t = $1; $1 = $2; $2 = t; print; } ' > $tempFile.sensorsC
# now do some substitution for the "consumed" data to match the plugwise network with the domoticz device ID's.
# the translate.sensors file contains the MAC addresses of the sensors and corresponding domoticz ID's.
# the result is a url for the domoticz json API that is stored in the result variable
resultC=`awk '
FNR==NR{a[$1]=$2;next}
$1 in a{print "/json.htm?type=command¶m=udevice&idx=" a[$1] "&nvalue=0&svalue=" $2 $3 }
' $scriptfolder"/translate.sensorsC" $tempFile.sensorsC `
# Storing values, the URL is sent to the domoticz API
#for debugging
#for i in $resultC
#do
# echo $resultC[i]
#done
for i in $resultC
do
echo `curl -s $domoticzIp:$domoticzPort$i`
done
Code: Select all
#!/bin/bash
#Parameters section
#Folders and files
scriptfolder=/home/pi/domoticz/scripts/
#The tempFile should preferably be on a ramdisk to avoid SD card wear
tempFile=/var/tmp/ram0/tempPlugwiseRead
#Plugwise Stretch parameters
stretchIP=<ip>
stretchPassword=<password>
#Domoticz Parameters
domoticzIp=192.168.2.40
domoticzPort=8080
#domoticzUser=[username for domoticz]
#domoticzPassword=[password for domoticz]
#Devices
MACPanelenGarage=000D6F000xxxxxxx
MACPanelenDak=000D6F000xxxxxxx
PanelenGarageDakID=64
#end of parameters section, no further customization should be needed from here
# Read sensor data "produced" from the plugwise zigbee network trough the stretch API.
curl -s --user stretch:$stretchPassword $stretchIP"/core/modules" | \
xmlstarlet sel -I \
-t -m "/modules/module" \
-v "services/electricity_point_meter/measurement[@directionality='produced']" -o ";" \
-v "services/electricity_interval_meter/measurement[@directionality='produced']" -o " " \
-v "protocols/network_router/mac_address" \
-v "protocols/network_coordinator/mac_address" -n \
| sed 's/\;\ //g' | sed 's/\ \ /\ /g' | sed 's/^ *//g' | sed 's/\;\;0\ //g' | sed '/^$/d' | awk ' { t = $1; $1 = $2; $2 = t; print; } ' > $tempFile.sensorsP
# now do some substitution for the "produced" data to match the plugwise network with the domoticz device ID's.
# the translate.sensors file contains the MAC addresses of the sensors and corresponding domoticz ID's.
# the result is a url for the domoticz json API that is stored in the result variable
resultP=`awk '
FNR==NR{a[$1]=$2;next}
$1 in a{print "/json.htm?type=command¶m=udevice&idx=" a[$1] "&nvalue=0&svalue=" $2 $3 }
' $scriptfolder"/translate.sensorsP" $tempFile.sensorsP `
#for debugging
#for i in $resultP
#do
# echo $resultP[i]
#done
# Storing values, the URL is sent to the domoticz API
for i in $resultP
do
echo `curl -s $domoticzIp:$domoticzPort$i`
done
# finally add the values of the two sensors reading the "produced" values (I use two sensors to get my Solarpanel production) and display total value in a dummy sensor
filename="$tempFile.sensorsP"
while read -r line
do
# echo "MACadres read from file -:$line"
if [[ $line = *$MACPanelenGarage* ]]
then
# echo "found MACPanelenGarage in line"
# remove spaces
bothvalues=${line##* }
#split the values separated bij a ';'
ARRbothvalues1=( $( echo "$bothvalues" | sed -e 's/;/\n/g' ) )
# echo "${ARRbothvalues1[@]}"
# echo "${ARRbothvalues1[0]}"
# echo "${ARRbothvalues1[1]}"
fi
if [[ $line = *$MACPanelenDak* ]]
then
# echo "found MACPanelenDak in line"
# remove spaces
bothvalues=${line##* }
#split the values separated bij a ';'
ARRbothvalues2=( $( echo "$bothvalues" | sed -e 's/;/\n/g' ) )
# echo "${ARRbothvalues2[@]}"
# echo "${ARRbothvalues2[0]}"
# echo "${ARRbothvalues2[1]}"
fi
done < "$filename"
# add the corresponding values and store them, convert the "reals" with BC
result1=$(echo ${ARRbothvalues1[0]} + ${ARRbothvalues2[0]} | bc)
result2=$(echo ${ARRbothvalues1[1]} + ${ARRbothvalues2[1]} | bc)
# finaly update the dummy sensor by sending the URL to the domoticz API
curl -s $domoticzIp":"$domoticzPort"/json.htm?type=command¶m=udevice&idx="$PanelenGarageDakID"&nvalue=0&svalue="${result1}";"${result2}
#Now read the plugwise circle switch status trough the stretch API, write the status to the switches listed in the switches file
curl -s --user stretch:$stretchPassword $stretchIP"/core/modules" | \
xmlstarlet sel -I \
-t -m "/modules/module" \
-v "concat(protocols/network_coordinator/mac_address,services/relay)" \
-v "concat(protocols/network_router/mac_address,services/relay)" \
| sed 's/\t//g' | sed 's/\n//g' | tr '\n' ' ' | sed 's/\ 0/\n0/g' | sed 's/on/1/g' | sed 's/off/0/g' > $tempFile.switches
# again, do matching, now against the translate.switches file
result=`awk '
FNR==NR{a[$1]=$2;next}
$1 in a{print "/json.htm?type=command¶m=udevice&idx=" a[$1] "&nvalue=" $2 }
' $scriptfolder"/translate.switches" $tempFile.switches `
# Storing values
for i in $result
do
echo `curl -s $domoticzIp:$domoticzPort$i`
done
If somebody has improvements, let me know...........................