25.11.2015, 22:01
Hello
I managed to integrate zwave, rf devices, .... by using domoticz as gateway, for now I only use it for switching zwave and rf devices and it works pretty well
domoticz you find here (I installed it on a synology nas):
https://www.domoticz.com/
below my user library
here a trigger to switch a device based a knx event
finally my deamon script to ensure that the knx status is correctly set even if I use domoticz or my zwave remote
I am sure code could be cleaner but I am still learning lua
Enjoy
Next step let a mobotix cam trigger the opening/closing of doors, but manage the opening hours per user through logic machine
I managed to integrate zwave, rf devices, .... by using domoticz as gateway, for now I only use it for switching zwave and rf devices and it works pretty well
domoticz you find here (I installed it on a synology nas):
https://www.domoticz.com/
below my user library
Code:
DOMOTICZ_IP = '192.168.0.12'
DOMOTICZ_PORT = '8084'
DOMOTICZ_ID_BASEMENT_COMPRESSOR = '23'
DOMOTICZ_ID_LIVINGROOM_READING_LIGHT = '215'
DOMOTICZ_ID_LIVINGROOM_VENTILATOR = '102'
KNX_GROUP_LIVINGROOM_COMPRESSOR_ON_OFF = '8/0/0'
KNX_GROUP_LIVINGROOM_COMPRESSOR_ON_OFF_STATUS = '8/0/1'
KNX_GROUP_LIVINGROOM_READING_LIGHT_ON_OFF = '8/1/0'
KNX_GROUP_LIVINGROOM_READING_LIGHT_ON_OFF_STATUS = '8/1/1'
KNX_GROUP_LIVINGROOM_VENTILATOR_ON_OFF = '8/1/2'
KNX_GROUP_LIVINGROOM_VENTILATOR_ON_OFF_STATUS = '8/1/3'
-- write value to knx only if value changed
function writeKNX(group, value, default, typ)
local key,time
-- time = os.microtime()
-- log('writeKNX(group, value, typ)',group,value,typ)
key = 'KNX_GRP_' .. string.gsub(group, "/", "_")
-- log('writeKNX(group, value, typ)',key)
if ((storage.get(key, default)) ~= value) then
-- log('now writing writeKNX(group, value, typ)',key, group, value)
storage.set(key, value)
grp.write(group, value, typ)
end
end
-- toggle on / off
function domoticzSwitch(host, port, domoticzId, value)
local time
-- time = os.microtime()
-- log('domoticzSwitch(host, port, domoticzId, value)',host, port, domoticzId, value)
if (value == true) then
domoticzCmd(host, port, domoticzId, 'switchlight', 'On')
else
domoticzCmd(host, port, domoticzId, 'switchlight', 'Off')
end
-- time = os.microtime() - time
-- log('domoticzSwitch(host, port)',host, port, domoticzId, value, time)
end
-- get On Off Status
function domoticzGetSwitchStatus(host, port, domoticzId)
local time, status
-- time = os.microtime()
-- log('domoticzGetSwitchStatus(host, port, domoticzId)',host, port, domoticzId)
status = domoticzStatus(host,port,domoticzId)
result = (status.result[1].Status == "On")
-- time = os.microtime() - time
-- log('domoticzGetSwitchStatus(host, port, domoticzId)',host, port, domoticzId, result, time)
return result
end
-- send command to domoticz
function domoticzCmd(host, port, domoticzId, domoticzCmd, cmdParam)
local http, request,r, c, h
-- time = os.microtime()
-- log('domoticzCmd', host, port, domoticzId, domoticzCmd, cmdParam)
-- load the http module
local http = require("socket.http")
request = "http://" .. host .. ":" .. port .. "/json.htm?type=command¶m=" .. domoticzCmd .. "&idx=" .. domoticzId .. "&switchcmd=" .. cmdParam
-- log('domoticzCmd', host, port, domoticzId, domoticzCmd, cmdParam, request)
-- connect to server
r, c, h = http.request (request)
-- time = os.microtime() - time
-- log('domoticzCmd', host, port, domoticzId, domoticzCmd, cmdParam, r, c, h, time)
end
-- retrieve a status from domoticz
function domoticzStatus(host, port, domoticzId)
local http, request,r, c, h
-- time = os.microtime()
-- log('domoticzStatus', host, port, domoticzId)
-- build the http request
local http = require("socket.http")
request = "http://" .. host .. ":" .. port .. "/json.htm?type=devices&rid=" .. domoticzId--
-- log('domoticzStatus', host, port, domoticzId, request)
-- execute the http request
r, c, h = http.request (request)
-- time = os.microtime() - time
-- log('domoticzStatus', host, port, domoticzId, r, c, h, time)
-- decode the json object
local json = require("json")
local statusobject = json.pdecode(r)
return statusobject
end
here a trigger to switch a device based a knx event
Code:
SCRIPT = 'user.library'
lib = require(SCRIPT)
if not lib then alert('Event ' .. event .. ' not handled: script ' .. SCRIPT .. ' missing') os.sleep(60) return end
-- object mapped to this event must have its data type set
value = event.getvalue()
-- toggle on/off
domoticzSwitch(DOMOTICZ_IP, DOMOTICZ_PORT, DOMOTICZ_ID_LIVINGROOM_VENTILATOR, value)
-- only write on changed data
writeKNX(KNX_GROUP_LIVINGROOM_VENTILATOR_ON_OFF_STATUS, value, false, dt.bool)
finally my deamon script to ensure that the knx status is correctly set even if I use domoticz or my zwave remote
Code:
SCRIPT = 'user.library'
lib = require(SCRIPT)
if not lib then alert('Event ' .. event .. ' not handled: script ' .. SCRIPT .. ' missing') os.sleep(60) return end
-----------------------------------
-- determine the domticz status
-----------------------------------
-- basement
bStatus = domoticzGetSwitchStatus(DOMOTICZ_IP, DOMOTICZ_PORT, DOMOTICZ_ID_BASEMENT_COMPRESSOR)
writeKNX(KNX_GROUP_LIVINGROOM_COMPRESSOR_ON_OFF, bStatus, false, dt.bool)
-- livingroom
-- reading light
bStatus = domoticzGetSwitchStatus(DOMOTICZ_IP, DOMOTICZ_PORT, DOMOTICZ_ID_LIVINGROOM_READING_LIGHT)
-- only write on changed data
--writeKNX(KNX_GROUP_LIVINGROOM_READING_LIGHT_ON_OFF, bStatus, false, dt.bool)
-- ventilator
bStatus = domoticzGetSwitchStatus(DOMOTICZ_IP, DOMOTICZ_PORT, DOMOTICZ_ID_LIVINGROOM_VENTILATOR)
-- only write on changed data
--writeKNX(KNX_GROUP_LIVINGROOM_VENTILATOR_ON_OFF, value, false, dt.bool)
I am sure code could be cleaner but I am still learning lua
Enjoy
Next step let a mobotix cam trigger the opening/closing of doors, but manage the opening hours per user through logic machine