AVM DECT200 - switch / domiticz on raspberry

Moderator: leecollings

Post Reply
HarryHase
Posts: 25
Joined: Friday 28 July 2017 21:21
Target OS: Linux
Domoticz version: stable
Location: Germany&Netherlands
Contact:

AVM DECT200 - switch / domiticz on raspberry

Post by HarryHase »

ongoing thread, to be continued later. please help to make it better ...

I searched for an implementation for the AVM devices, but only found an old sample, which is not working with username and password.
I looked around in the internet and got an sample based on bash scripting from chip magazine. I adapoted it to a domoticz switch.

1) All we need a new folder in ....domoticz/scripts (like fritz)
2) the scripts from the attachment. All the files in the fritz directory and don't forget to make the scripts exutable (chmod +x *)
fritz.rar
dect200 bash scrpting
(4.43 KiB) Downloaded 255 times
3) in both scripts we set
- fritz!box address
- password
- username
4) In domitcz we generate a virtual switch
5) know we set on/of parameters
on: script:///home/pi/domoticz/scripts/fritz/smart_fritz_ain.sh on 087610330743
off: script:///home/pi/domoticz/scripts/fritz/smart_fritz_ain.sh off 087610330743
6) save & ready to go
---
If we start the script smart_fritz_terminal.sh in an terminal window we got all the Information that are avaible in the Fritzbox. This can be used to improve my quick&dirty implenmantation.
visioncvg
Posts: 4
Joined: Wednesday 21 November 2018 22:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: AVM DECT200 - switch / domiticz on raspberry

Post by visioncvg »

Switching the DECT 200 on or off with domoticz works fine.
Now I want to see the power consumption of the DECT200 in domoticz.
Anybody who can help me ?
GeyerA
Posts: 12
Joined: Friday 18 September 2020 11:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: AVM DECT200 - switch / domiticz on raspberry

Post by GeyerA »

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
Attachments
DECT200 Domoticz.png
DECT200 Domoticz.png (80.86 KiB) Viewed 894 times
visioncvg
Posts: 4
Joined: Wednesday 21 November 2018 22:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: AVM DECT200 - switch / domiticz on raspberry

Post by visioncvg »

Hi Geyer,

I have already a node red script working to see the power of a dect 200 in domoticz :)
But want to see the total energy as well.
Try to get your script working but i miss some stupid things.

Did this steps:
-1-
copy your first script in file with name avm.sh in folder domoticz/scripts/
edit this file and changed USER and PASSWD and save it
With putty I did chmod +x avm.sh

-2-
copy your second script in file with name DECT200.py in folder domoticz/scripts/

-3-
in Domoticz2021.1 I create al Lua All with your third script

In domoticz log file I see :
row 17: attempt to call a nil value
row 17 is:
json = (loadfile "/home/domoticz/scripts/lua/JSON.lua")() -- For Linux

I also create in domoticz 3 general devices with text.
But where do I put this 3 IDX numbers in wich script ?

Thanks so far
GeyerA
Posts: 12
Joined: Friday 18 September 2020 11:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: AVM DECT200 - switch / domiticz on raspberry

Post by GeyerA »

Hi, sorry, wasn't here for a while and have not received a notification on your question. Anyhow, the idx numbers are set in the lua script in Domoticz just at the beginning.

Code: Select all

-- devsocket
   local idx_7_state = 35
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests