HA Bridge <-> Domoticz Bi-Directional Interface

Client tools or tools that can connect with Domoticz. Tools for Windows, iOS, Android, Linux etc.

Moderator: leecollings

Post Reply
Cornholio1234
Posts: 23
Joined: Thursday 03 March 2016 11:01
Target OS: Windows
Domoticz version: V3.4964
Location: Germany
Contact:

HA Bridge <-> Domoticz Bi-Directional Interface

Post by Cornholio1234 »

Just as a extentsion to my thread here:
http://domoticz.com/forum/viewtopic.php?f=21&t=11104

Just installing the HA-Bridge will allow you to trigger the lights using the Harmony. But switching the light in Domoticz will not give the Harmony (HABridge) new state information. Therefore I built my own "HABridge-Notifier" using LUA. Maybe it is useful for someone here:

LUA Code:

Code: Select all

t={}
t['C - Wall Plug Leselicht']={'Leselicht','211','1951577859'}
t['TV Switch']={'Fernseher','4','1200792111'}
t['Hue T1']={'Hue Table 1','37','1296398667'}
t['Hue T2']={'Hue Table 2','39','970283985'}
t['Hue S1']={'Hue Stehlampe','38','1551866107'}
t['Hue LivingColors 1']={'Hue Living Colors','40','957168100'}
t['Light Switch Office']={'Licht Buero','102','1272799582'}
t['Light Switch Living Room']={'Living Room Light','2','1820035260'}

for deviceName,deviceValue in pairs(devicechanged) do
    if(t[deviceName] ~= nil) then
        if(tostring(deviceValue)=='On') then
            s=[[ batch/turnON1.sh]]..' '..t[deviceName][3] 
        elseif(tostring(deviceValue)=='Off') then
            s=[[ batch/turnOFF2.sh]]..' '..t[deviceName][3]
        elseif(deviceValue ~= nil) then
            state='true'
            dimN=tonumber(deviceValue:sub(11,13))
            if(dimN~=nil) then 
               dim=tostring(math.ceil(dimN*2.55))
               s=[[ batch/turnON2.sh]]..' '..state..' '..dim..' '..t[deviceName][3]
            end
        end
        os.execute(s)
    else
    end
end
As you see I put all my Harmony enabled devices to a LUA table, holding the name, idx and HABridge ID ([1] and [2] are just for me and maybe future usage). If a device is triggered and found in the table I just call a little sh script (works nearly the same with a windows batch file).
Depending on the action (On/Off/Dimming) I will trigger the according script. My HABridge is running on the same pi, Port 8099. In Windows domoticz waited for the OS.execute to close and I used the start command to trigger a new process - I assumed the RPI will have the same behaviour so I used nohup to get away from the console and save some ms...:

Off:

Code: Select all

nohup curl -H "Content-Type: application/json" -X PUT -d "{ \"on\": false }" http://127.0.0.1:8099/api/user/lights/$1/bridgeupdatestate -o curl.log
On:

Code: Select all

nohup curl -H "Content-Type: application/json" -X PUT -d "{ \"on\": true }" http://127.0.0.1:8099/api/user/lights/$1/bridgeupdatestate -o curlO.log
Dim:

Code: Select all

nohup curl -H "Content-Type: application/json" -X PUT -d "{ \"on\": $1, \"bri\": $2 }" http://127.0.0.1:8099/api/user/lights/$3/bridgeupdatestate -o curlO2.log
Let me know if you've got Ideas for improvement.
Windows Server, Aeotec Z-Stick Gen5, V3.4964
m3mojo
Posts: 2
Joined: Thursday 22 December 2016 19:20
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: HA Bridge <-> Domoticz Bi-Directional Interface

Post by m3mojo »

Hi Cornholio1234,

first i would like to thank you for your code - it helped me a lot!

I changed it slightly to update all devices as soon as i press a button on my remote (nodon), because it talks directly to my lights and the server does not receive events from the lights but only once from the remote. Moreover i integrated the commands from your sh scripts directly into the lua script (please note the double backslash).

For me it works great and with a quick result on the logitech remote - about 5 sec!

Code: Select all

server_hab = '127.0.0.1:8099'
for deviceName,deviceValue in pairs(devicechanged) do
	if (deviceName=='Fernbedienung B2B4') then
		for HABdeviceName,HABdeviceValue in pairs(t) do
			s = ''
			if(tostring(deviceValue)=='On') then
				s='nohup curl -H "Content-Type: application/json" -X PUT -d "{ \\"on\\": true }" '.. server_hab ..'/api/user/lights/'.. HABdeviceValue[3] ..'/bridgeupdatestate -o curl.log'
			elseif(tostring(deviceValue)=='Off') then
				s='nohup curl -H "Content-Type: application/json" -X PUT -d "{ \\"on\\": false }" '.. server_hab ..'/api/user/lights/'.. HABdeviceValue[3] ..'/bridgeupdatestate -o curl.log'
			elseif(deviceValue ~= nil) then
				state='true'
				dimN=tonumber(deviceValue:sub(11,13))
				if(dimN~=nil) then
					dim=tostring(math.ceil(dimN*2.55))
					s='nohup curl -H "Content-Type: application/json" -X PUT -d "{ \\"on\\": '.. state ..', \\"bri\\": '.. dim ..' }" '.. server_hab ..'/api/user/lights/'.. HABdeviceValue[3] ..'/bridgeupdatestate -o curl.log'
				end
			end
			print (s)
			os.execute(s)
		end
	end
end
I wish you and everyone who reads this a Merry Christmas and a Happy New Year!

m3mojo
rwijbenga
Posts: 20
Joined: Sunday 25 September 2016 22:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: HA Bridge <-> Domoticz Bi-Directional Interface

Post by rwijbenga »

Thank you for this super script! :) :mrgreen:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest