"Visability" case

I am trying to make dynamic sitemap and have the following configuration:

.items file

String OPPO105      "OPPO 105D"   {channel="globalcache:gc100_12:000C1EC016A4:sl-m2#c1"} 
Number OPPOSwitchVisability

.rules file:

rule "OPPO Power Visibility"

when
	Item OPPO105 received command 
then
	var OPPOSwitchVisability = 1
	println("Point 0 reached, visability state is: " + OPPOSwitchVisability)
	println("Point 1 reached")
	if(receivedCommand == "OPPO_DICSCR_POWER_ON") {
		println("Point 2 reached")	
		OPPOSwitchVisability = 0
		println("Point 3 reached, visability state is: " + OPPOSwitchVisability)
	}
		println("Point 4 reached, visability state is: " + OPPOSwitchVisability)
end

.sitemap file:

sitemap globalcache label="My home automation" {
    Frame label="Overall turn off/on" {      
        Switch item=OPPO105 label="OPPO105 Power" mappings=[OPPO_DICSCR_POWER_ON = "ON"] visibility=[OPPOSwitchVisability == 1, OPPOSwitchVisability != "Uninitialized"]
        Switch item=OPPO105 label="OPPO105 Power" mappings=[OPPO_DICSCR_POWER_OFF = "OFF"] visibility=[OPPOSwitchVisability == 0]     
                }}

I have a single switch with “ON” label at the sitemap initiation.
When I push the switch button I expect to have “ON” button to be hidden and “OFF” button shown. But actually nothing happens. The rule works without errors. Below are the records from the Console as a result of println() command in .rules file:

openhab> Point 0 reached, visability state is: 1
Point 1 reached
Point 2 reached
Point 3 reached, visability state is: 0
Point 4 reached, visability state is: 0

I can not understand why it does not work since variable “OPPOSwitchVisability” get the value of “0”, that shall trigger “OFF” switch button to be shown and “ON” button to be hidden, but I do not see it.

Would much appreciate for your guidance and help how to fix this problem and comments where I made a mistake. Thank you in advance.

You have OPPOSwitchVisability declared as Item AND as a variable in your rule. The variable is known to rule only and is lost when the rule is finished, the sitemap however does know the Item, but that one doesn’t change.

I don’t understand what you are trying to do with the mapping, I would do it like this on the sitemap:

Switch item=TestSomething label="OPPO105 PowerON" visibility=[OPPOSwitchVisability == 1, OPPOSwitchVisability != "Uninitialized"]
    Switch item=TestSomething label="OPPO105 PowerOFF"  visibility=[OPPOSwitchVisability == 0]

I did test it with an SwitchItem TestSomething and this rule did work (I used LogInfo, that way the lines show in log:tail):

rule “OPPO Power Visibility”

when
Item TestSomething received update
then
logInfo(“Test”, “Testsomething = {}”,TestSomething)
OPPOSwitchVisability.postUpdate(1)
logInfo(“Test”, “OPPOSwitchVisability = {}”,OPPOSwitchVisability)
logInfo(“Test”, “Point 1 reached!”)
if(TestSomething.state.toString==“ON”) {
logInfo(“Test”, “Point 2 reached!”)
OPPOSwitchVisability.postUpdate(0)
logInfo(“Test”, “Point 3 reached OPPOSwitchVisability = {}”,OPPOSwitchVisability)
}
logInfo(“Test”, “Point 4 reached OPPOSwitchVisability = {}”,OPPOSwitchVisability)
end

1 Like

Thank you for your help, I reached the desirable result through process of trial and error.
To my mind, the key mistake in my code was that as you mentioned

What I wanted is:

  1. at the initiation I have button “ON” visible
  2. When I click the “ON” button it disappears and the button “OFF” become visible
  3. After I click “OFF” button it disappears and “ON” button become visible again
  4. on and on

Would much appreciate if you can give me a clue why the following code works for the case described above:

rule "OPPO Power Visibility"

when
	Item OPPO105 received command 

then
	if(receivedCommand == "OPPO_DICSCR_POWER_ON") {
		OPPOSwitchVisability.postUpdate(0)
		}
	else {
		if(receivedCommand == "OPPO_DICSCR_POWER_OFF"){
		OPPOSwitchVisability.postUpdate(1)
		}
		}
end	

and this code does not work:

rule "OPPO Power Visibility"

when
	Item OPPO105 received command 

then
	if(receivedCommand == "OPPO_DICSCR_POWER_ON") {
		OPPOSwitchVisability = 0
		}
	else {
		if(receivedCommand == "OPPO_DICSCR_POWER_OFF"){
		OPPOSwitchVisability = 1		
		}
		}
end	

postUpdate( ) works for item state and equality sign for variables?

That’s what I think (at least).