Hello, I also use a couple of DECT 200 and wanted status and consumption as well as total energy. I initially used sensors to display but now headed over to multiline text. You need the following (I put all in my domoticz/scripts folder) and make sure all references to files are in the absolute path:
1. Script to get connected DECT200 written to a txt file; at the end, you find a link to python script to then write all into JSON format; my filename was avm.sh (make sure you have it executable chmod +x avm.sh):
Code: Select all
#!/bin/bash
#################
# Konfiguration #
fbox="192.168.178.1"
USER="putyourusernamehere"
PASSWD="putyourpasswordhere"
#################
rm /home/domoticz/scripts/DECT200.txt
if [ "$fbox" = "" ]; then
echo Bitte Fritzbox Adresse im Script eintragen
exit 1
fi
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
NC='\033[0m'
CURL=$(which curl)
MD5SUM=$(which md5sum)
ICONV=$(which iconv)
AWK=$(which awk)
# Die SmartHome-Seite der Fritzbox
CURLCMD="$CURL -s $fbox/webservices/homeautoswitch.lua"
# Funktion zum Ermitteln der SID / Anmeldung bei der Fritzbox
get_sid() {
challenge=$(curl -s http://$fbox/login_sid.lua | grep -o "<Challenge>[a-z0-9]\{8\}" | cut -d'>' -f 2)
hash=$(echo -n "$challenge-$PASSWD" |sed -e 's,.,&\n,g' | tr '\n' '\0' | md5sum | grep -o "[0-9a-z]\{32\}")
SID=$(curl -s "http://$fbox/login_sid.lua" -d "response=$challenge-$hash" -d 'username='${USER} \
| grep -o "<SID>[a-z0-9]\{16\}" | cut -d'>' -f 2)
#echo SID: $SID
}
# Funktion zum Ermitteln von Werten
get_value() {
RESULT=""
if [ "$2" = "" ]; then
RESULT=$($CURLCMD"?sid=$SID&switchcmd=$1")
else
RESULT=$($CURLCMD"?sid=$SID&ain=$2&switchcmd=$1")
fi
}
get_sid # SID holen
if [ "$SID" = "0000000000000000" ]; then
echo -e "${RED}Anmeldung fehlgeschlagen ${NC}"
exit 1
fi
if [ "$SID" = "" ]; then
echo -e "${RED}Anmeldung fehlgeschlagen ${NC}"
exit 1
fi
#echo -e "${GREEN}Anmeldung erfolgreich ${NC}"
#echo
# Liste der Schalter ermitteln
get_value getswitchlist
COUNTER=0
IFS=', ' read -r -a array <<< "$RESULT"
# Werte der Schalter holen
for AIN in "${array[@]}"
do
let COUNTER=COUNTER+1
#echo -e "${YELLOW}Actor #$COUNTER ${NC}"
switchpresent=0
get_value getswitchname $AIN
#echo -e "${CYAN}Name: $RESULT ${NC}"
get_value getswitchpresent $AIN
#echo AIN: $AIN
DConnected=$RESULT
#echo Connected: $DConnected
if [ "$DConnected" = "1" ]; then
get_value getswitchname $AIN
DName=$RESULT
get_value getswitchstate $AIN
DState=$RESULT
get_value getswitchpower $AIN
switchpower=`awk "BEGIN {printf \"%.2f\n\", $RESULT/1000}"` #Power in [W]
get_value getswitchenergy $AIN
DEnergy=$RESULT #Total energy in [Wh]
get_value gettemperature $AIN
DTemperature=`awk "BEGIN {printf \"%.1f\n\", $RESULT/10}"` #Switch temperature in [C]
echo "$AIN" "$DConnected" "$DState" "$DTemperature" "$switchpower" "$DEnergy" "$DName" >> /home/domoticz/scripts/DECT200.txt
fi
#if [ "$RESULT" = "1" ]; then
# bei aktiven Schaltern
# Parameter verarbeiten, etwa: 1 on
#if [ "$1" = "$COUNTER" ]; then
# if [ "$2" = "on" ]; then
# get_value setswitchon $AIN
# echo "set #$COUNTER on"
# fi
# if [ "$2" = "off" ]; then
# get_value setswitchoff $AIN
# echo "set #$COUNTER off"
# fi
# if [ "$2" = "toggle" ]; then
# get_value setswitchtoggle $AIN
# echo "set #$COUNTER toggle"
# fi
#fi
# # Alle Werte ermitteln/ausgeben (langsamer)
# get_value getswitchname $AIN
# DName=$RESULT
# get_value getswitchstate $AIN
# DState=$RESULT
# get_value getswitchpower $AIN
# switchpower=`awk "BEGIN {printf \"%.2f\n\", $RESULT/1000}"` #Power in [W]
# get_value getswitchenergy $AIN
# DEnergy=$RESULT #Total energy in [Wh]
# get_value gettemperature $AIN
# DTemperature=`awk "BEGIN {printf \"%.1f\n\", $RESULT/10}"` #Switch temperature in [C]
# echo "$DName" "$DState" "$AIN" "$switchpower" "$DEnergy" "$DTemperature" > fritz"$AIN".txt
#fi
done
python /home/domoticz/scripts/DECT200.py
exit 0
2. Script to write data into JSON format (my filename was DECT200.py):
Code: Select all
# Python program to convert text
# file to JSON
import json
# the file to be converted
filename = '/home/domoticz/scripts/DECT200.txt'
# resultant dictionary
dict1 = {}
# fields in the sample file
fields =['AIN', 'Connected', 'State', 'Temperature', 'Current Power', 'Total Energy', 'Switch Name']
with open(filename) as fh:
# count variable for employee id creation
l = 1
for line in fh:
# reading line by line from the text file
description = list( line.strip().split(None, 7))
# for output see below
print(description)
# for automatic creation of id for each employee
sno ='Switch'+str(l)
# loop variable
i = 0
# intermediate dictionary
dict2 = {}
while i<len(fields):
# creating dictionary for each employee
dict2[fields[i]]= description[i]
i = i + 1
# appending the record of each employee to
# the main dictionary
dict1[sno]= dict2
l = l + 1
# creating json file
out_file = open("/home/domoticz/scripts/DECT200.json", "w")
json.dump(dict1, out_file, indent = 5)
out_file.close()
3. Lua event in Domoticz; I kept a lot of other code since it required debugging on the way. I am not an expert and hope somebody of you can make this code much smarter. I used sensors which I now don't. I decided to stay with text driven based on the status. If switched on, I will show status, current power and total energy - if switched off, status and total energy.
Code: Select all
-- lua script
-- mokka plug
-- local idx_2_power = xx
-- local idx_2_energy = xx
local idx_2_state = 31
-- washining machines
-- local idx_6_power = xx
-- local idx_6_energy = xx
local idx_6_state = 34
-- devsocket
local idx_7_state = 35
os.execute('/home/domoticz/scripts/avm.sh')
commandArray = {}
json = (loadfile "/home/domoticz/scripts/lua/JSON.lua")() -- For Linux
-- API call
local config=assert(io.popen('curl "file:///home/domoticz/scripts/DECT200.json"'))
local Stringjson = config:read('*all')
config:close()
local jsonData = json:decode(Stringjson)
-- local name2 = jsonData.Switch2['Switch Name']
-- local AIN2 = jsonData.Switch2['AIN']
-- local connected2 = jsonData.Switch2['Connected']
local state2 = jsonData.Switch2['State']
local temperature2 = jsonData.Switch2['Temperature']
local power2 = jsonData.Switch2['Current Power']
local energy2 = jsonData.Switch2['Total Energy']
-- local name6 = jsonData.Switch6['Switch Name']
-- local AIN6 = jsonData.Switch6['AIN']
-- local connected6 = jsonData.Switch6['Connected']
local state6 = jsonData.Switch6['State']
local temperature6 = jsonData.Switch6['Temperature']
local power6 = jsonData.Switch6['Current Power']
local energy6 = jsonData.Switch6['Total Energy']
-- local name7 = jsonData.Switch7['Switch Name']
-- local AIN7 = jsonData.Switch7['AIN']
-- local connected7 = jsonData.Switch7['Connected']
local state7 = jsonData.Switch7['State']
local temperature7 = jsonData.Switch7['Temperature']
local power7 = jsonData.Switch7['Current Power']
local energy7 = jsonData.Switch7['Total Energy']
--print (state2)
local state2num = tonumber(state2)
local state6num = tonumber(state6)
local state7num = tonumber(state7)
if state2num == 0 then
state2txt = 'Switched off'..'\r\n'..'Total '..energy2 ..' Wh'
elseif state2num == 1 then
state2txt = 'Switched on'..'\r\n'..'Power '..power2 ..' W\r\n'..'Total '..energy2 ..' Wh'
end
if state6num == 0 then
state6txt = 'Switched off'..'\r\n'..'Total '..energy6 ..' Wh'
elseif state6num == 1 then
state6txt = 'Switched on'..'\r\n'..'Power '..power6 ..' W\r\n'..'Total '..energy6 ..' Wh'
end
if state7num == 0 then
state7txt = 'Switched off'..'\r\n'..'Total '..energy7 ..' Wh'
elseif state7num == 1 then
state7txt = 'Switched on'..'\r\n'..'Power '..power7 ..' W\r\n'..'Total '..energy7 ..' Wh'
end
--print (state2txt)
--print (Stringjson) -- debug json
--print ('Name '..Name) -- parsed json value
--print ('AIN '..AIN) -- parsed json value
--print ('Connected '..Connected) -- parsed json value
--print ('State '..State) -- parsed json value
--print ('Temperature '..Temperature..' C') -- parsed json value
--print ('Current Power '..CurrentPower..' W') -- parsed json value
--print ('Total Energy '..TotalEnergy..' Wh') -- parsed json value
--energy2kwh = math.ceil(energy2 / 1000)
--energy6kwh = math.ceil(energy6 / 1000)
--commandArray[#commandArray + 1] = {['UpdateDevice']= idx_2_power .. "|0|" .. power2 .. ";" .. energy2}
--commandArray[#commandArray + 1] = {['UpdateDevice']= idx_2_energy .. "|0|" .. energy2}
commandArray[#commandArray + 1] = {['UpdateDevice']= idx_2_state .. "|0|" .. state2txt}
--commandArray[#commandArray + 1] = {['UpdateDevice']= idx_6_power .. "|0|" .. power6 .. ";" .. energy6}
--commandArray[#commandArray + 1] = {['UpdateDevice']= idx_6_energy .. "|0|" .. energy6}
commandArray[#commandArray + 1] = {['UpdateDevice']= idx_6_state .. "|0|" .. state6txt}
commandArray[#commandArray + 1] = {['UpdateDevice']= idx_7_state .. "|0|" .. state7txt}
return commandArray