Scripts reading your Plugwise stretch and smile

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
westd001
Posts: 22
Joined: Friday 28 August 2015 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Scripts reading your Plugwise stretch and smile

Post by westd001 »

Hi all,

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 :P :P

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&param=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&param=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&param=udevice&idx="$electricityProductionMeterID"&nvalue=0&svalue="${result[0]}";"${resultTprod}
The stretch script wich read all the "consumed" values:

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&param=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
The stretch script wich read all the "produced" values and the status of my circles:

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&param=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&param=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&param=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
Its working fine for a few days now.
If somebody has improvements, let me know...........................
BBI_home
Posts: 25
Joined: Monday 08 December 2014 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by BBI_home »

Hi,

I'm implementing a Plugwise Smile and until know everything including scripts etc. are running fine.
To complete this I have to automate this with cron job and are struggling to setup a RAM disk on my Raspberry.

To avoid mistakes I hope that somebody is so kind to give some support and give me the correct steps and commands to do so.
djgodlike
Posts: 32
Joined: Wednesday 27 January 2016 16:37
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by djgodlike »

BBI_home wrote:Hi,

I'm implementing a Plugwise Smile and until know everything including scripts etc. are running fine.
To complete this I have to automate this with cron job and are struggling to setup a RAM disk on my Raspberry.

To avoid mistakes I hope that somebody is so kind to give some support and give me the correct steps and commands to do so.
Setting up a RAM drive on Raspberry Pi
http://www.domoticz.com/wiki/Setting_up ... spberry_Pi



PS can you help me to get the Smile script running? :) I placed the script (with my settings) in the scripts folder and made a crontab rule in /etc/crontab.

Code: Select all

*/5  *  * * *   root    /home/pi/domoticz/scripts/plugwise_read > /dev/null
But nothing happens...

When i try the following my domoticz crashes (and i have to remove the db file):
root@raspberrypi:~# /home/pi/domoticz/scripts/plugwise_read > /dev/null
/home/pi/domoticz/scripts/plugwise_read: line 50: bc: command not found
/home/pi/domoticz/scripts/plugwise_read: line 51: bc: command not found
/home/pi/domoticz/scripts/plugwise_read: line 60: bc: command not found
/home/pi/domoticz/scripts/plugwise_read: line 60: bc: command not found

Sorry im kinda new.

UPDATE:
I managed to get the script working on my system..

I had to install "bc" to begin with and tried out some code from various people i think...
Now working fine! Only thing i'm not getting done is the crontab..
Maybe it will work after some time, but not directly when edited.

UPDATE:
I got the cronjob working aswell! the code needed "bash" :shock:

Code: Select all

*/2 * * * * bash /home/pi/domoticz/scripts/plugwise_read > /dev/null
Aeon Labs Gen5 USB
Greenwave Powernode 6 NS310
2x Greenwave Powernode 1 NS310
Philips Hue
Logitech media Server
Plugwise Smile P1
PatrickDomoticz
Posts: 1
Joined: Tuesday 11 April 2017 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by PatrickDomoticz »

Hi Westd001,

I've almost got your script working. This is the output I'm receiving:

result 0 = 0.008 Electricity_point_meter produced Don't know what this is
result 1 = 2606.268 Electricity_point_meter consumed This is my Gas reading
result 2 = 0.000 Electricity_cumulative_meter produced nl_offpeak
result 3 = 191.000 Electricity_cumulative_meter produced nl_peak Dont now what this is, could be current consumption? Not sure...
result 4 = 0.000 Electricity_cumulative_meter consumed nl_offpeak
result 5 = 0.000 Electricity_cumulative_meter consumed nl_peak
result 6 = 3535694.000 Gas_interval_meter This is my Low Energy Charge (daltarief)
result 7 = 2238161.000 Gas_cumulative_meter This is my High Energy Charge (piektarief)

Is there anything I'm doing wrong?

Here is a dump of my XML file:

Code: Select all

<modules>
<module id="82dfa5a8efa34fd1a549573da66ced89">
<vendor_name/>
<vendor_model/>
<hardware_version/>
<firmware_version/>
<created_date>2016-04-07T14:19:18.815+02:00</created_date>
<modified_date>2017-04-11T17:22:25.625+02:00</modified_date>
<deleted_date/>
<services>
<gas_interval_meter id="25f7843fa189453599bea391593086e5">
<measurement log_date="2017-04-11T16:00:00+02:00" unit="m3" interval="PT1H" directionality="consumed">0.008</measurement>
</gas_interval_meter>
<gas_cumulative_meter id="ae7144c03bc541a5b18590fdd1bca04f">
<measurement log_date="2017-04-11T17:00:00+02:00" unit="m3" directionality="consumed">2606.276</measurement>
</gas_cumulative_meter>
</services>
<protocols>
<dsmrgas id="0c9d6849a4dd49f28afcf96a31c64fc4">
<serial>G0016561209786214</serial>
<dsmrmain id="4e16d30ff6db438db22e4cf7e1589401"/>
</dsmrgas>
</protocols>
</module>
<module id="fe57bc73f14b40e284874b2297c33dfe">
<vendor_name>Xemex</vendor_name>
<vendor_model>XMX5LGBBFFB231033563</vendor_model>
<hardware_version/>
<firmware_version/>
<created_date>2016-04-07T14:19:18.793+02:00</created_date>
<modified_date>2017-04-11T17:22:25.626+02:00</modified_date>
<deleted_date/>
<services>
<electricity_point_meter id="2163690fe02a4a77b5c4d1ef280c6237">
<measurement log_date="2017-04-11T17:22:09+02:00" unit="W" directionality="produced">0.000</measurement>
<measurement log_date="2017-04-11T17:22:09+02:00" unit="W" directionality="consumed">95.000</measurement>
</electricity_point_meter>
<electricity_interval_meter id="45e371c6e8f04c5eb97a3f8c79cc9724">
<measurement log_date="2017-04-11T17:00:00+02:00" unit="Wh" interval="PT300S" directionality="produced" tariff_indicator="nl_offpeak">0.000</measurement>
<measurement log_date="2017-04-11T17:00:00+02:00" unit="Wh" interval="PT300S" directionality="produced" tariff_indicator="nl_peak">0.000</measurement>
<measurement log_date="2017-04-11T17:00:00+02:00" unit="Wh" interval="PT300S" directionality="consumed" tariff_indicator="nl_offpeak">0.000</measurement>
<measurement log_date="2017-04-11T17:00:00+02:00" unit="Wh" interval="PT300S" directionality="consumed" tariff_indicator="nl_peak">41.000</measurement>
</electricity_interval_meter>
<electricity_cumulative_meter id="b97e854112494026add3fa2020af165b">
<measurement log_date="2017-04-11T17:20:00+02:00" unit="Wh" directionality="produced" tariff_indicator="nl_offpeak">0.000</measurement>
<measurement log_date="2017-04-11T17:20:00+02:00" unit="Wh" directionality="produced" tariff_indicator="nl_peak">0.000</measurement>
<measurement log_date="2017-04-11T17:20:00+02:00" unit="Wh" directionality="consumed" tariff_indicator="nl_offpeak">3535694.000</measurement>
<measurement log_date="2017-04-11T17:20:00+02:00" unit="Wh" directionality="consumed" tariff_indicator="nl_peak">2238260.000</measurement>
</electricity_cumulative_meter>
</services>
<protocols>
<dsmrmain id="4e16d30ff6db438db22e4cf7e1589401">
<serial>E0004001548735614</serial>
<dsmrmbuses>
<dsmrgas id="0c9d6849a4dd49f28afcf96a31c64fc4"/>
</dsmrmbuses>
</dsmrmain>
</protocols>
</module>
</modules>


westd001 wrote:Hi all,

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.

viewtopic.php?f=31&t=600&p=35700&hilit= ... 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 :P :P

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&param=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&param=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&param=udevice&idx="$electricityProductionMeterID"&nvalue=0&svalue="${result[0]}";"${resultTprod}
The stretch script wich read all the "consumed" values:

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&param=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
The stretch script wich read all the "produced" values and the status of my circles:

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&param=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&param=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&param=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
Its working fine for a few days now.
If somebody has improvements, let me know...........................
BBI_home
Posts: 25
Joined: Monday 08 December 2014 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by BBI_home »

I wonder if someone can help me with a small detail.

I've created a swap file:

df
tempPlugwiseRead 1024 0 1024 0% /var/tmp/ram0/tempPlugwiseRead

but by running the default Plugwise_prod script I get an error:

./plugwise_prod: line 37: /var/tmp/ram0/tempPlugwiseRead.sensors: Permission denied

should there be an extra swapfiel like : tempPlugwiseRead

tempPlugwiseRead.switches /var/tmp/ram0/tempPlugwiseRead tmpfs nodev,nosuid,size=1M 0 0

or di I have to use another setting in the script?
BlueSky
Posts: 3
Joined: Friday 22 December 2017 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by BlueSky »

I,m trying to get a Smile P1 up and running but all I'm getting is error messages. I copied the script into a file (Smile.sh) and made it executable. This is the error message:

pi@raspberrypi:~/domoticz/scripts $ ./Smile.sh
-:6.8: Opening and ending tag mismatch: hr line 5 and body
</body>
^
-:7.8: Opening and ending tag mismatch: body line 3 and html
</html>
^
-:8.1: Premature end of data in tag html line 1

^
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: syntax error
{
"status" : "OK",
"title" : "Update Device"
}
{
"status" : "OK",
"title" : "Update Device"
}
{
"status" : "OK",
"title" : "Update Device"
}

I can read the smile on a webpage so it is working.

Any ideas? Thanks in advance.
BlueSky
Posts: 3
Joined: Friday 22 December 2017 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by BlueSky »

I managed to get rid of the first 3 error messages. In line 29, IFS=$'\r\n' result=(`curl -s --user smile:$smilePassword $smileIp"/core/modules" | \ I replaced core to smartmeter which differs due to the firmware version. But I still get this 4 syntax error messages. Any clues?
BlueSky
Posts: 3
Joined: Friday 22 December 2017 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by BlueSky »

This script is for devices with firmware 2.0 and on. The XML output of the Smile P1 differs a lot between 1.2 en 2.0 firmware versions. That means this script will not work for firmware versions below 2.0 At this stage I am getting data from the Smile but it gives values on places where you won't suspect. Also, when you manually read the smile, the XML shows the gas value on the beginning of the file and when you download and read the XML again it is on the end of the file. Now the readings in the script follows an A to Z pattern so when the gas value is the first value, the scripts put it also in the first position. When the script reads the values again it is in the last position, so it switches back and forth.

I'm not a programmer hero so I struggled a lot and that's why I quit for now. Here's a link with more info: http://domoticx.com/plugwise-smile-p1-d ... t-scripts/
PietjeNL
Posts: 1
Joined: Saturday 17 March 2018 15:53
Target OS: Linux
Domoticz version:
Contact:

Re: Scripts reading your Plugwise stretch and smile

Post by PietjeNL »

For the Smile script to work i needed to install:
sudo apt-get install xmlstarlet
sudo apt-get install bc

And now it works like it should.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest