Sync 2 Switch states

upfront apology: I’ve been doing a lot of openhab configs last few days (with frustration and yet successes) and I’m stuck on getting some states to sync without unwanted side-effects. I normally do a lot of reading first before posting and have done for this issue but I think I’m just overloaded with what I’ve read and I hope a direct plea for help will get me over this particular bump.

I have a Samsung TV and a Broadlink RM2 and use the following bindings:
Samsung binding for general Samsung TV stuff
Broadlink binding for sending IR code to TV
Network binding for online status

My .items

Switch LRTV_online "LR TV online" { channel="network:pingdevice:192_168_2_96:online"}
Switch LRTVpower "LR TV" { channel="samsungtv:tv:accd7816_e74e_4cd5_a147_f5173316f64b:power"}
Switch LRTVpowerRM2 "LR TV Power (RM2)"
String BroadlinkRM2 "BroadlinkRM2" { channel="broadlink:rm2:b4-43-0d-da-25-b2:command"}

My .sitemap

Text item=LRTVpower label="LR TV is [%s ]" icon=television  //Displays TV state.  ping could be used but Samsung binding is faster.
Switch item=LRTVpower  //Uses Samsung binding
Switch item=LRTVpowerRM2  //Virtual switch which uses rules to send IR power command via RM2

My .rules

rule "LRTV power"
when
	Item LRTVpowerRM2 changed
then
	if (LRTVpowerRM2.state == ON)
	{
		BroadlinkRM2.sendCommand("LRTVpower")
	}else if (LRTVpowerRM2.state == OFF)
	{
		BroadlinkRM2.sendCommand("LRTVpower")
	}
end

The Samsung binding (and related switch) can switch my TV off but not on, but knows TV state so is ON when TV is on and off when TV is off no matter how the TV was put into the state.
The LRTVpowerRM2 switch sends an IR code and can be used to switch the TV on or off.

What I’d like to have is the LRTVpowerRM2 switch change how it looks in the UI depending if the TV is on or off (and so depending either if LRTV_online is ON/OFF but preferably if LRTVpower is ON/OFF) because this way it is always ready to propose the right action even if the physical remote is used, or the Samsungbinding is used to switch the TV off.

I tried to create a rule for this ‘sync’ and tried several variations which didn’t work.
I currently have:

rule "Sync RM2 IR LRTV Power switch with TV state"
when
	Item LRTVpower received command
then
	LRTVpowerRM2.postUpdate(LRTVpower.state)
end

and although this does change the RM2 switch state it also sends the command so I get either a TV switch on (physical remote) and then a TV switch off follows (via RM2 switch automatically), or the TV is switched off (Samsung binding or physical remote) and then gets switched on (via the RM2 switch).

I had thought about using the item autoupdate=false parameter but not sure of the ramifications in this scenario and can’t check it now as kids and wife have the TV.

I did also find this rule/trigger table https://docs.openhab.org/configuration/rules-dsl.html#manipulating-item-states but at this stage my brain isn’t able to process.

I hope what I provide is clear enough and someone is able to offer some help. Thanks in advance.

power	Switch	TV power. Some of the Samsung TV models doesn't allow to set Power ON remotely.
rule "LRTV power"
when
	Item LRTVpowerRM2 changed
then
	if (LRTVpowerRM2.state != LRTVpower.state) {BroadlinkRM2.sendCommand("LRTVpower")}
end

Your rule `“LRTV power” doesn’t make sense because it ALWAYS send a command to the Broadlink.

So, use the samsung binding to turn OFF
and Broadlink to turn ON

rule "LRTV power"
when
    Item LRTVpowerRM2 changed
then
    if (LRTVpowerRM2.state == ON) {
        if (LRTVpower.state == OFF) {
            BroadlinkRM2.sendCommand("LRTVpower")
        }
    } else if (LRTVpowerRM2.state == OFF) {
        LRTVpower.sendCommand(OFF)
    }
end

@hr3 @vzorglub thanks for the replies and apologies for just dropping out but I needed to focus on something else. I’ll give your suggestions a look and try in the next day or so. Since I have a few devices that both are pingable (hence stateful) and RM2 controllable (with no state) your suggestions have an application across several items so I’m looking forward to getting it working. I’ll report back my findings. Thanks again.

a thought: another way to do this would be to have separate items for on and off and show them only depending on the device online state. Wouldn’t need a rule and can be handled by the sitemap visibility tag. any thoughts around the pros and cons of both approaches?

Hi there,
here is my setup and its working through interface and manually.

items:

Switch SonyTV_VirtualSwitch  "SonyTV_VirtualSwitch"  
String SonyTV_BroadlinkPower "TV Power"    { channel="broadlink:rm3:??-??-??-??-??-??:command" }
Switch SonytvPower           "SonytvPower" { channel="sony:simpleip:sonyTvSimpleip:power" }

rules:

rule "SonyTV Power part 1"
when
    Item SonyTV_VirtualSwitch changed
then
    if (SonyTV_VirtualSwitch.state == ON) {
        if (SonytvPower.state == OFF) {
            SonyTV_BroadlinkPower.sendCommand("SonyTvPowerON/OFF")
        }
    } else if (SonyTV_VirtualSwitch.state == OFF) {
        SonytvPower.sendCommand(OFF)
    }
end
rule "SonyTV Power part 2"
when
    Item SonytvPower changed
then
    if (SonytvPower.state == ON && SonyTV_VirtualSwitch.state == OFF) {
        SonyTV_VirtualSwitch.sendCommand(ON)
    } else if(SonytvPower.state == OFF && SonyTV_VirtualSwitch.state == ON){
        SonyTV_VirtualSwitch.sendCommand(OFF)
    }
end

sitemap:

	Switch item=SonyTV_VirtualSwitch
1 Like