Refer to Items by value or variable in Rules? (Code portability.)

How do I refer to an Item by a var or val defition? Here’s how I would like it to work, but it’s not working at the moment:

sample.rules:

...imports omitted...

var  physical_relay1  =  "HardWire_20AMP_Relay"  // An actual Item name, as defined in .items file.
var  physical_relay2  =  "Safety_20AMP_Relay"  // An actual Item name, as defined in .items file.

// All the Item references below are to *variables* defined above, not "real" Items in .items file. 
// How do I get OH to interpret the variables as Items?

rule "Zone Heat Power - Status Indicator"
when
Item  physical_relay  changed  
then   

if ((physical_relay1.toString() + ".state") == OFF ) {  
Kitchen_Heat_Power_STATUS.postUpdate("OFF") }
} else if {
      ((physical_relay1.toString() + ".state") == OFF ) {
      physical_relay2.sendCommand(OFF)
       }  
end

I’m going to leave these here:

For this second link also look at ben_jones12’s post a little down.

The tl;dr of it is don’t do this. There are much much better ways to reuse the same code for different Items.

But because some people simply must suffer this sort of pain on their own…

I think you can just do:

val physical_relay1 = HardWire_20AMP_Relay
val physical_relay2 = Safety_20AMP_Relay

No quotes.

Furthermore, your rule is incorrect syntax.

    if(physical_relay1.state == OFF) {
        Kitchen_Heat_Power_STATUS.postUpdate(OFF)
    }
    else if(pyhsical_relay1.state == ON) {
        physical_relay2.sendCommand(OFF)
    }

NOTE: do you really mean to check if physical_relay1’s state is ON in the else if? If not then that clause will never be executed. If you really do mean OFF then:

    if(physical_relay1.state == OFF){
        Kitchen_Heat_Power_STATUS.postUpdate(OFF)
        physical_relay2.sendCommand(OFF)
    }

Thanks for your detailed reply and for outlining the “hack” solution even though it’s a hack. It works.

The code I pasted was just a hypothetical, not actually real-world, please ignore the ON/OFF consequences.

Looks like I need to invest some time learning how to program properly for using Groups and lambdas.

In the meantime tho, just to hack it together & make it work I will be using “val” definitions & copy/pasting .rules definitions for each zone.

There’s a learning curve for all this stuff and I can’t integrate everything, mentally, at once, but I do need it to work in the interrim until I can restructure it all properly.

Actually, it doesn’t work as hoped.

I wanted to just do a single val definition at the top of the .rules file:

val physical_relay1 = HardWire_20AMP_Relay
val physical_relay2 = Safety_20AMP_Relay

And then refer to “physical_relay1” etc. in any rule within that file. But for whatever reason the rules engine refuses to use the global val definitions. The val definitions above will work if declared within a single rule, but not when declared at the top of a .rules file.

I tried restarting OH completely (not just reloading Rules files) to ensure the val’s get initialized, still doesn’t work.

I currently face the same problem. When trying to initalize a new variable with the value of an Item called Temp_Value_Day, I get “The method or field Temp_Value_Day is undefined”.

Edit: Restarting the designer fixed this…

1 Like