"global" Variable in rule file set to an item

Tags: #<Tag:0x00007f7460f9db58>

I’m changing my heating rules to a, be simpler, and b, use a time picker. In changing them over I want them both to be running, but only one will use the active switch, the other will use a fake switch.

In a single rule I’m setting:

var heaterItem = zwave_switch_central_heating

or

var heaterItem = pretend_central_heating

In my new rule, I’m running a single rule in a single file, this is fine.

But my old rules I have about 4 rules, and have to set heaterItem each time.

Is there a way at the top of the rule file that I can set heater item equal either item once?
I tired putting the assignment all by itself, but it complained it was set before assignment.

Even a single function that I can re-use at the top of each rule? As long as it goes to one place, when I can make the necessary change once.

@rlkoshak is our combining guru.

The big problem you will have is that the Item Object will get stale. From one run of a Rule to the next zwave_switch_central_heating == zwave_switch_central_heating will be false. This is why you must use the Item name in a HashMap as the key instead of the Item itself.

So I recommend using

var heaterItem = "zwave_switch_central_heating"

and then use one of the Associated Item techniques to gain access to the Item by name in your Rules. If all you do is send commands to this Item all you really need to do is switch to use the sendCommand Action instead of the Item method.

Why do you need to hard-code a variable to contain an item? All items are always available globally. There might be cases where there’s a benefit to doing so, e.g. if you need to decide which item to use later in the rule, but it might also just be complicating things. Can you post the rest of the rule?

Because there are 4 rules in that rules file operating on a switch. I’m implementing a new set of rules and so want to send the old code to a fake switch, rather than remove the rule file.

By having it set in one place it’s easier to swap