Ok, so you need at least the mqtt binding and a mqtt broker. given, the broker is already online, you need a mqtt broker thing in openHAB to connect to the broker (this thing is called a bridge. Please donāt create a system broker thing but only a broker thing).
Now, to represent your device, you need a generic mqtt thing, linked to the mqtt broker bridge.
In the generic mqtt thing, you need one channel, which is bidirectional. As you get a number, the channel is of type number.
stateTopic is (in your case) MX210/Output/GET/DO/STATE and commandTopic is MX210/Output/SET/DO/MASK.
Bridge mqtt:broker:myBroker [
host="192.168.178.99" // ip or fqdn of your mqtt broker
] {
Thing topic myDevice
Channels:
Type number : IO "In/Out" [
stateTopic="MX210/Output/GET/DO/STATE",
commandTopic="MX210/Output/SET/DO/MASK"
]
}
Now link the channel to a Number Item (letās call it DeviceIO)
As you have 24 individual channels, we need 24 additional Switch Items. And as there are 24 of them, letās create an additional Group Item. Letās call them Switch_1 to Switch_24 and gSwitches.
Number DeviceIO "In/Out" { channel="mqtt:topic:myBroker:myDevice:IO", autoupdate="false" }
Group gSwitches { autoupdate="false" }
Switch Switch_1 "Out 1 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_2 "Out 2 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_3 "Out 3 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_4 "Out 4 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_5 "Out 5 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_6 "Out 6 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_7 "Out 7 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_8 "Out 8 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_9 "Out 9 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_10 "Out 10 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_11 "Out 11 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_12 "Out 12 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_13 "Out 13 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_14 "Out 14 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_15 "Out 15 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_16 "Out 16 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_17 "Out 17 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_18 "Out 18 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_19 "Out 19 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_20 "Out 20 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_21 "Out 21 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_22 "Out 22 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_23 "Out 23 [%s]" (gSwitches) { autoupdate="false" }
Switch Switch_24 "Out 24 [%s]" (gSwitches) { autoupdate="false" }
Now to the rules:
import java.lang.Math
rule "Device in"
when
Item DeviceIO changed
then
var BigInteger register = (DeviceIO.state as DecimalType).toBigDecimal.toBigInteger
gSwitches.members.forEach[i |
val Integer nBit = Integer::parseInt(i.name.split("_").get(1)) - 1
i.postUpdate(if(register.testBit(nBit) == 1) ON else OFF)
]
end
rule "Device out"
when
Member of gSwitches received command
then
var BigInteger register = (DeviceIO.state as DecimalType).toBigDecimal.toBigInteger
val Integer nBit = Integer::parseInt(triggeringItem.name.split("_").get(1)) - 1
if(receivedCommand == ON)
register = register.setBit(nBit)
else
register = register.clearBit(nBit)
DeviceIO.sendCommand(register)
end
I did not test this code, so itās very likely to fail at first attempt, but Iām pretty sure you get the idea.
Every time, you switch one of the 24 switches, the second rule is triggered.
The rule gets the state of the device and creates a variable called register
. then it determines which switch was switched. this is the second part of the item name, split by ā_ā. We do need this value as an integer, so we need to parse the string to integer.
If the received command was ON, we set the bit, else we clear the bit.
As a last step, we send the whole number to the devices.
If the device state was changed (the change came from mqtt), all 24 switches are switched to their correct state (ON or OFF) through the first rule.