How to combine two items into one item?

Hello, All!
I am fairly new to openHAB and I am testing out some of the basic features.
I would like to know if there is a way to create an item which combines switcher and display device status (icon on or off).

openHAB version: 2.3.0
I used arduino and modbus rtu for remote control a lamp.
My configs:
/etc/openhab2/sitemaps/lamp.sitemap

sitemap default label="Lamp" {
    Frame label="Lamp remote control" {
        Text   item=LED
        Switch item=BTN
    }
}

/etc/openhab2/items/lamp.items

Contact LED "Lamp status"       {modbus="slave1:5"}
Switch  BTN "Button"   	<light> {modbus="slave2:5"}

/etc/openhab2/rules/lamp.rules

rule "Lamp swicher"
when
        Item LED changed
then
        if(LED.state==OPEN){BTN.label="On"}else{BTN.label="Off"}
end

The rule above change the item label by current lamp status.
The rule above work correctly if on sitemap present two items but need one item and show the current lamp status as an icon.
Thank you.

You don’t need a rule for that:

Contact LED "Lamp status"       {modbus="slave1:5"}
Switch  BTN "Button"   	<light> {modbus="slave2:5"}
sitemap default label="Lamp" {
    Frame label="Lamp remote control" {
        Switch item=BTN label="On" visibility=[LED==OPEN]
        Switch item=BTN label="Off" visibility=[LED!=OPEN]
    }
}

In fact, you don’t need two items either:

Switch  BTN "Light"   	<light> {modbus=">[slave2:5],<[slave1:5]", autoupdate=false""}

The virtual “binding” autoupdate="false" will cause openHAB not to update the state through a sendCommand(), so, the state is only set by received messages or postUpdate().

Now this sitemap should suffice:

sitemap default label="Lamp" {
    Frame label="Lamp remote control" {
        Switch item=BTN
    }
}
2 Likes

Thank you very much for your replying! Appreciate it.