Use variable for item

Tags: #<Tag:0x00007fe05834ad60>

(Kiss Tamás) #1

Dear All!

How can I define item (name) with variable? I would like to use variable instead of item, to simplify the reuse of my code in different “rules” files.
This doesnt work:
var ItemName = “hum11” (this doesn’t show any error in openhab log but doesnt work)
var ItemName = hum11 (This shows error: Cannot reference the field ‘hum11’ before it is defined)

How should i do this?

Thanks in advance!
Tamás Kiss

(HomeAutomation) #2

The syntax is the same as in java.

var is used for something that can be changed in rule
val is used for something which is only initialized and used not changed

If it is clear what it is:
var = value

var TextString = “Test of the String”
It is clear that this is a String because of “”""

If it is not clear
var = value

var Timer timer = null
var Number percent = 0

val autoModeItem = gMAPDB.members.findFirst[name.equals( + “_Number”)] as GenericItem

(Kiss Tamás) #3


So i have to use this syntax:
val autoModeItem = hum11 as GenericItem


(HomeAutomation) #4

a val is a not changeable variable. Normaly it is used to calculate soemthing and send this to the val.

Bacause my example is one of thsi calcualtion the syntax is like I post.

normally you can use var and val definition similar.

var TextString = “Test of the String”
val TextString = “Test of the String”

val GenericItem autoModeItem = hum11 // will be OK but makes no sence because autoModeItem should be calculated? Instead you can use hum11 which is already defined.

In you case:
var String ItemName = “hum11” // if you need a string
var Number ItemName = 10 // If you need a number

But look around here. There are a lot of code here you can learn about.

(Kiss Tamás) #5


I dont understand exactly…
I have already tried var ItemName = “hum11” .
But it didnt work for me…

Part of the code:

var ItemName = "hum11"
rule "Bathroom humidity"
        Item ItemName changed
        logInfo("BathRoomHumidity1", ItemName.state.toString())
       if (!sent && ItemName.state < Low) {
                //sendMail(";","Fürdőszoba páratartalom alacsony","fürdőszoba:" + ItemName.state)
                sendBroadcastNotification("Fürdőszoba páratartalom alacsony!:" + ItemName.state)
                pushover("Fürdőszoba páratartalom alacsony!:" + ItemName.state)
                sent = true
                alert= true
                waitTimer = createTimer(now.plusMinutes(30))[|
                        sent = false

(HomeAutomation) #6

This will not work. You have to use the item from item file. Because your item is not an item it is a string.

Maybe this can work:
val ItemName = YourItemFromItemfiile as GenericItem

But, from my point of view, you understand the logic of OH wrong.

Why do you use a var instead ot the real item?

(Rossko57) #7

Wrong approach, there is no way to substitute a name string for an OH Item in the rules language.

There is a way to select Items by name from a Group, which is one of the possible ways to re-use code

There are also lambda “functions” which allow passing of Items to reusable code

(Kiss Tamás) #8


Thank you! I see the problem and the solution. I will organize these items to group and i will try to put the code into one rule… i think i can go through the groups’ items one by one…

(Rich Koshak) #9

In my experience, this may not work because the Item Object’s link to the Item registry will get stale and will stop reflecting updates to the states.

@Tommas84, I agree with HomeAutomation. What it appears you are trying to do is not possible. Your goal should not be to “simplify the reuse of [your] code in different “rules” files.” Your goal should be to avoid repeating code anywhere.

There are lots of ways to do this (note these are intended to be use in combination):

(Kiss Tamás) #10

Hi All!

I see. Ok, i will try to go on this way.
Im very happy to see your helpful replies.

Best regards,