Help me Binding Modbus 1.9.0 holding

I’ve never had to do this, so no example.

Change Modbus data type
modbus:serial.O1.valuetype=uint16
This is to read/write the register bits combined as a single number

I think you then need an Item -
Number MyRegisterImage “Whole register value” {modbus:“O1:0”}
This will poll the register containing the bits of interest, and when sent a command within Openhab, write to the register.

Now you can define your individual bits as switch Items, for writing -
Switch MyBit00 “First bit”
Switch MyBit01 “Second bit”
etc.
Note that these ‘bits’ are NOT directly linked to Modbus.

Now we need a rule, which will respond to changes in the switches and send a new value on Modbus -

rule “action switch change”
when
Item MyBit00 received command or
Item MyBit01 received command
then
var Number skeleton = 0
if (MyBit00.state == ON) {
skeleton = skeleton + 1 // set bit 0
}
if (MyBit01.state == ON) {
skeleton = skeleton + 2 // set bit 1
}
MyRegisterImage.sendCommand(skeleton) // send the built-up number
end

This is all untried and probably needs work.
It has the limitation that it doesn’t read the initial state from Modbus and copy into the Switches. I think it would be possible to do that with a rule that triggered on update (not command) of ‘MyRegisterImage’, and then analysed the value by bits, and then updated (not command) the Switches. I think by using update in this way, it would prevent the whole thing getting into an update-command-update loop?

Edit - this looks useful for reading the bits if needed

1 Like