Event-based program to control Modbus Air-Conditioner from KNX
Example: Event-based program to control Modbus Air-Conditioner from KNX
Task
When KNX group address receives 1, write TRUE in according Modbus register, if 0 – then FALSE.
Event-based program
- Assume we have already added object 1/1/1 (1bit)
- In Scripting ->Event-based add a script assigned with group address 1/1/1
- Add the following script in Scripting Editor for this program
- require('luamodbus')
- mb = luamodbus.rtu()
-
- mb:open('/dev/ttyS2', 9600, 'E', 8, 1, 'H')
- mb:connect()
-
- mb:setslave(1)
-
- -- compare either 1/1/1 received 1 or 0
- if event.datahex == '01' then
- -- write in specific Modbus register TRUE if 1/1/1 is 1
- mb:writeregisters(0x9C40, 0x11)
- else
- -- write in specific Modbus register FALSE if 1/1/1 is 0
- mb:writeregisters(0x9C40, 0)
- end
Encoded A/C values
Some Air Conditioners are keeping enocded values in registers, you need to encode them first from HEX to use in the further scripts.
For example, value = 0x0cba after executing the below commands will give 24.2
- hex = lmcore.inttohex(value, 2)
- temp = knxdatatype.decode(hex, dt.float16)
How to encode KNX temperature into 16 Hexadecimal data and write to Modbus registers:
- function encodetemp(value)
- local hex = knxdatatype.encode(value, dt.float16).datahex
- return tonumber(hex, 16)
- end
-
- value = 20.8
- mb:writeregisters(30001, encodetemp(value))