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)
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.
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…
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):