Switch Rule causes continuous ON/OFF loop

I have a group switch to turn on and off 4 tv’s and also have individual switches for each. when I press the group button all 4 switches go into a continuous loop turning each switch on and off. This also happens to each switch individually when pressed.

Rules:
// Turn TV1 ON/OFF 
rule "Power ON LGvTV1 via WOL"
when 
    Item PBATV1Power received command 
then
    if ( receivedCommand == ON ) 
        sendCommand( PBATV1_WOL, ON ) 
        logInfo("LG.rules", "TV1 Power ON")
    
end

rule "Power OFF LGvTV1 "
when 
    Item PBATV1Power received command 
then
    if ( receivedCommand == OFF ) 
        sendCommand( PBATV1Power, OFF )
        logInfo("LG.rules", "TV1 Power Off")
    
end
**** I tried this alternate rule and it does the same thing
// Turn TV2 On/Off rules
rule "Power on LGvTV2 via WOL"
when 
    Item PBATV2Power received command 
then
    if (receivedCommand == ON) 
        sendCommand( PBATV2_WOL, ON ) 
    else 
        sendCommand( PBATV2Power, OFF )
        logInfo("LG.rules", "TV2 Power Off")
    
end

ITEMS:
Group PBAtv  "PBA-TVS"  <lgtv> 

Group:Switch:OR(ON, OFF)  TVs      "All TVs [(%d)]"

Switch   PBATV1Power   "TV1 Power"  (PBAtv, TVs)  {channel="lgwebos:WebOSTV:93d6e248-596d-40b2-bf27-2f93e0c70c1b:power"}
Switch   PBATV2Power   "TV2 Power"  (PBAtv, TVs)  {channel="lgwebos:WebOSTV:c9bd12f1-9313-4903-b117-bb58460250cc:power"}
Switch   PBATV3Power   "TV3 Power"  (PBAtv, TVs)  {channel="lgwebos:WebOSTV:3670e8fa-33e6-45cf-b1de-857ec1d3f7bf:power"}
Switch   PBATV4Power   "TV4 Power"  (PBAtv, TVs)  {channel="lgwebos:WebOSTV:6947719a-7e95-4e3d-b463-a9758d69780d:power"}

Switch   PBATV1_WOL         { wol="192.168.25.255#A8:23:FE:9D:21:46" }   
Switch   PBATV2_WOL         { wol="192.168.25.255#A8:23:FE:CC:11:0C" } 
Switch   PBATV3_WOL         { wol="192.168.25.255#A8:23:FE:7D:98:DC" } 
Switch   PBATV4_WOL         { wol="192.168.25.255#A8:23:FE:C8:85:2E" } 

SITEMAP:
	Frame label="LG_TV" {
        Switch item=PBAtv icon="tv" mappings=[OFF="Off", ON="On"] 
		Text label="LG TVs" icon="tv" {
           // Group item=TVs label="ALL TVs" 
        
            Frame label="TV-1" {
                Switch item=PBATV1Power 
                Switch item=TV1Input label="INPUT" mappings=[0="PC", 1="DVD", 2="HDMI-1",3="HDMI-2",4="HDMI-3"]
                }
            Frame label="TV-2" {
                Switch item=PBATV2Power 
                Switch item=TV2Input label="INPUT" mappings=[0="PC", 1="DVD", 2="HDMI-1",3="HDMI-2",4="HDMI-3"]
                }
            Frame label="TV-3" {
                Switch item=PBATV3Power 
                Switch item=TV3Input label="INPUT" mappings=[0="PC", 1="DVD", 2="HDMI-1",3="HDMI-2",4="HDMI-3"]
                }
            Frame label="TV-4" {
                Switch item=PBATV4Power 
                Switch item=TV4Input label="INPUT" mappings=[0="PC", 1="DVD", 2="HDMI-1",3="HDMI-2",4="HDMI-3"]
                }
        }
	}

EVENTS.LOG
* this is what happens when a single tv switch is pressed:
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.600 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.616 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF
2019-10-02 15:28:29.631 [ome.event.ItemCommandEvent] - Item 'PBATV1Power' received command OFF

OPENHAB.LOG

2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power ON
2019-10-02 15:29:12.017 [INFO ] [ipse.smarthome.model.script.LG.rules] - TV1 Power Off

You have two rules that are triggered both on:
Item PBATV1Power received command
So each time that item received a command both are triggered. In the second rule you send a command to that item, so all starts over again.

Could you recommend how to code it. If you could show a nubie :thinking: .

I tried this and it did the same:

// Turn TV2 On/Off rules
rule "Power on LGvTV2 via WOL"
when 
    Item PBATV2Power received command 
then
    if (receivedCommand == ON) 
        sendCommand( PBATV2_WOL, ON ) 
    else 
        sendCommand( PBATV2Power, OFF )
        logInfo("LG.rules", "TV2 Power Off")
    
end

In all three examples, you have a Rule that gets triggered when PBATV2Power receives command OFF. Then you immediately sendCommand(OFF) to that same Item, which retriggers the Rule and on and on.

So what are you trying to accomplish? Why the two OFF commands to the same Item? We can’t tell you how do do it when we don’t understand what it is you are trying to accomplish. The Rule makes no sense as written.

1 Like

As what @rlkoshak said, it’s not clear what you want.
But based on your rule, i would suggest this:

 rule "Power ON LGvTV1 via WOL" 
when 
  Item PBATV1Power received command ON 
then
  PABTV1_WOL.sendCommand(ON)
  logInfo("LG.rules", "TV1 Power ON") 
end 

rule "Power OFF LGvTV1 "
when 
  Item PBATV1Power received command OFF
then 
  PBATV1WOL.sendCommand(OFF ) 
  logInfo("LG.rules", "TV1 Power Off")
end

This way, the rules won’t fire each other. I noticed now that in the first rule you sendCommand to PABTV1_WOL and the second rule you sendCommand to PABTV1POWER. The latler will create the loop.

Wake On Lan to Power Off the TV will not work :wink:

That makes no sense… there is your loop … sendCommand( PBATV1Power, OFF ) triggers the rule in loop

Try it like this … :slight_smile:

rule "Power ON / OFF LGvTV1"
when 
  Item PBATV1_SWITCH received command 
then
  if (receivedCommand == ON) {
    sendCommand( PBATV1_WOL, ON ) 
    logInfo("LG.rules", "TV1 Power ON")
  } else if (receivedCommand == OFF) {
    sendCommand( PBATV1Power, OFF )
    logInfo("LG.rules", "TV1 Power Off")
  }
end

Switch   PBATV1_SWITCH "Power Switch for TV PBATV1"
Switch   PBATV1Power   "TV1 Power"   {channel="lgwebos:WebOSTV:93d6e248-596d-40b2-bf27-2f93e0c70c1b:power"}
Switch   PBATV1_WOL    "TV1 WOL"     { wol="192.168.25.255#A8:23:FE:9D:21:46" }   

Made the changes and that stopped the looping. The TV’s turn on with the WOL command but they don’t turn OFF, and every time I do a turn On it generates a new UID and puts it in the INBOX. is that the reason it doesn’t respond to the power off command? How can I stop i from creating a new UID every time it wakes up the TVs.