13.06.2016, 07:59
Hi,
This is an example about how LM can integrate in KNX a Mohlenhoff EZR zone heating/cooling system, via XML (parse/change).
Resident script (to regular read the data from EZR controller via XML):
Event script (writing the data to EZR controller when some data are changed in KNX):
This is an example about how LM can integrate in KNX a Mohlenhoff EZR zone heating/cooling system, via XML (parse/change).
Resident script (to regular read the data from EZR controller via XML):
Code:
grp.checkupdate = function(alias, newvalue, dpt, mindelta)
local curvalue, valdelta
-- get current object value
curvalue = grp.getvalue(alias)
-- value is numeric and delta is specified
if type(curvalue) == 'number' and type(mindelta) == 'number' then
newvalue = tonumber(newvalue) or 0
valdelta = math.abs(curvalue - newvalue)
-- delta is below minimum, do nothing
if valdelta < mindelta then
return
end
-- do not send same boolean values
elseif type(curvalue) == 'boolean' and curvalue == newvalue then
return
end
grp.update(alias, newvalue, dpt)
end
http = require('socket.http')
url = 'http://192.168.1.111/data/dynamic.xml'
function starttag(p, tag, attr)
local nr = tonumber(attr.nr)
table.insert(path, tag)
if nr then
nrtag = tag
table.insert(path, nr)
end
end
function endtag(p, tag)
table.remove(path)
if tag == nrtag then
nrtag = nil
table.remove(path)
end
end
function chardata(p, txt)
local ptr, len
txt = txt:trim()
if #txt == 0 then
return
end
ptr = result
len = #path
for i, tag in ipairs(path) do
if i == len then
ptr[ tag ] = txt
elseif not ptr[ tag ] then
ptr[ tag ] = {}
end
ptr = ptr[ tag ]
end
end
-- current xml tree path
path = {}
-- resulting table
result = {}
-- tag name that contains "nr" attribute
nrtag = nil
lxp = require('lxp')
parser = lxp.new({
StartElement = starttag,
EndElement = endtag,
CharacterData = chardata,
})
xml = http.request(url)
res = parser:parse(xml or '')
if res then
log(result.Devices.Device.HEATAREA[ 1 ].HEATAREA_MODE)
log(result.Devices.Device.HEATAREA[ 1 ].T_ACTUAL)
log(result.Devices.Device.HEATAREA[ 1 ].T_TARGET)
log(result.Devices.Device.HEATAREA[ 1 ].PROGRAM_WEEK)
log(result.Devices.Device.HEATAREA[ 1 ].PROGRAM_WEEKEND)
log(result.Devices.Device.HEATAREA[ 1 ].PARTY)
log(result.Devices.Device.HEATAREA[ 1 ].PARTY_REMAININGTIME)
log(result.Devices.Device.HEATAREA[ 1 ].ISLOCKED)
log(result.Devices.Device.IODEVICE[ 1 ].SIGNALSTRENGTH)
log(result.Devices.Device.IODEVICE[ 1 ].BATTERY)
end
grp.checkupdate('2/1/1', result.Devices.Device.HEATAREA[ 1 ].HEATAREA_MODE, dt.uint8, 1)
grp.checkupdate('2/1/2', result.Devices.Device.HEATAREA[ 1 ].T_ACTUAL, dt.float16, 0.1)
grp.checkupdate('2/1/3', result.Devices.Device.HEATAREA[ 1 ].T_TARGET, dt.float16, 0.1)
grp.checkupdate('2/1/7', result.Devices.Device.HEATAREA[ 1 ].PROGRAM_WEEK, dt.float16, 1)
grp.checkupdate('2/1/8', result.Devices.Device.HEATAREA[ 1 ].PROGRAM_WEEKEND, dt.float16, 1)
grp.checkupdate('2/1/9', result.Devices.Device.HEATAREA[ 1 ].PARTY, dt.float16, 1)
grp.checkupdate('2/1/10', result.Devices.Device.HEATAREA[ 1 ].PARTY_REMAININGTIME, dt.float16, 1)
grp.checkupdate('2/1/12', result.Devices.Device.HEATAREA[ 1 ].ISLOCKED, dt.float16, 1)
grp.checkupdate('2/1/13', result.Devices.Device.IODEVICE[ 1 ].SIGNALSTRENGTH, dt.uint8, 1)
grp.checkupdate('2/1/14', result.Devices.Device.IODEVICE[ 1 ].BATTERY, dt.uint8, 1)
Event script (writing the data to EZR controller when some data are changed in KNX):
Code:
http = require('socket.http')
setpoint = event.getvalue()
-- format to XY.Z format (1 decimal)
setpoint = string.format('%.1f', setpoint)
url = 'http://192.168.1.111/data/changes.xml'
xml = [[<?xml version="1.0" encoding="UTF-8"?>
<Devices>
<Device>
<ID>EZR01233F</ID>
<HEATAREA nr="1">
<T_TARGET>]] .. setpoint .. [[</T_TARGET>
</HEATAREA>
</Device>
</Devices>]]
socket.http.request(url, xml)