Get Item Value by variable name

Hy to everyone!

how can i get the Value of an Ithem which name is stored in a Variable?

var number_of_channels = 1
var channel_1 = "Wohnzimmer_Licht"			//Item of HUE White
rule "scene_save"
when
    Item scene_id received command
then
    sendCommand('scene_'+{{scene_id.state}}, number_of_channels + ";" + channel_1 + ":" + channel_1.state)
end

What do i have to write instead of “channel_1.state” to get the value of the Item “Wohnzimmer_Licht” by using the variable channel_1?

Thanks a lot!

I think you can store an Item object in a variable.

var channel_1 = Wohnzimmer_Licht as GenericItem

I’m not sure how it would behave if it’s defined outside of the rule though, but I believe it should work.

thanks for your answer. I will try it tonight.

You cannot get the state of an Item if all you have is its name. What you can do is pull the Item out of a Group by its name. See Design Pattern: Working with Groups in Rules.

I don’t think that storing Items in variables always works the way we expect. The problem is the Item becomes stale and may not reflect the changing state of the Item while the Rule isn’t running.

What exactly are you trying to accomplish with these variables? I’m all but certain there is a better approach.

Thanks for your answer!

When i press a button on Hubpanel, a numeric Value will be written to scene_id. This should trigger my rule.

When the rule is running, i’d like to genarate a string which contains all the informations of all channels.
th string should look like this:

3;Wohnzimmer_Licht:50;channel_2:30;channel_3:127,55,100

If scene_id = 1 the string should be saved in scene_1, scene_id = 2 --> scene_2, …

With the rule in this Post i’d like to split the string and give the values back to the Items.

this would be my way to save and restore scenes.

You are describing how you would like to go about this. You are not describing why you are approaching it this way.

I see this all the time. For example, to make up a dumb simili. Someone asks how to attach sandpaper to the inside of a bowl and cover the bowl and what is the best way to spin a bowl. And they get all sorts of answers their questions.

But if they had just asked up front “what’s the best way to peel potatoes?” they would have received a lot more relevant and useful help (i.e. use a potatoe peeler).

There are lots of ways to create scenes and the rules that go with them. And your approach is looking a whole lot like the bowl with sandpaper contraption.

Why do you need to use a numeric value from HABPanel?

Why do you need to generate a String like this? Can’t you just do what you need to in the rule that triggers in the first place?

Don’t tell me how you want to implement this; tell me what you want to happen.

1 Like

@rlkoshak i think what he wants is to configure all devices and then save their states. I think this is a easy way to configure scenes without the need of programming.

@code723 as @rlkoshak says put all your items in a group. Then use the group to save the states of the items.

@rlkoshak what do you think about this?

Group:Switch GTest
Switch Test_1 (GTest) 
Switch Test_2 (GTest) 
Switch Test_3 (GTest) 

Number TestScenes "Scene Number is [%s]"
Number TestScenes_restore "Scene Number is [%s]"
import java.util.Map
import java.util.HashMap

var Map<Number,Map<GenericItem,String>> itemStore = new HashMap()

rule "Save Group"
when
    Item TestScenes changed 
then
    var stateNow = GTest.members.storeStates
    itemStore.put( triggeringItem.state as Number , stateNow)
    logInfo("Test  previousStates",  stateNow.toString)
end

rule "Restore Group"
when
    Item TestScenes2 changed 
then
    var map = itemStore.get(triggeringItem.state as Number)
    restoreStates(map)
    logInfo("Test  previousStates",  map.toString)
end

Edit: thanks @rlkoshak it got less code with that.
Previously the workaround was needed as the saved map when restored the states with forEach would contain the actuall states not the old ones.

1 Like

If you’re familiar with JSON, that’s how I would do it. It’s similar to the solution posted above but since you’re using HABPanel already, I would let the client handle the parsing part. So basically, you construct your JSON string and save that string into an item, then parse that string back to an object in HABPanel.

That makes some sense. There is actually a pair of actions that does this for you:

var states = storeStates(myGroupOfItems)
//or
var states = storeStates(MyItem1, MyItems2, ...)

storeStates will return a Map<Item, State> similar to what your code does. To restore the states use:

restoreStates(states)

According to the docs, if the stored state can be interpreted as a command then sendCommand is used. Otherwise, an update is used. I’m not sure how it makes that interpretation so your second rule might still be required.

See. I found a potato peeler for you. :wink:

Documented here: https://docs.openhab.org/addons/actions.html#event-bus-actions

My english is not that good, so it’s hard for me to express myself correctly.

your potato peeler looks good :wink:

thank you!

Hy!

It has been a bit stressful lately, so I was only able to test your (@rlkoshak) suggestions today.

import java.util.Map
import java.util.HashMap

var stateNow = null

rule "Save Group"
when
    Item scene_id changed 
then
    stateNow = storeStates(gSzene)
end

rule "Restore Group"
when
    Item scene_selector changed 
then
    if(stateNew != null) restoreStates(stateNow)
end

this works great and I can save and recall a scene.

As far as I know, only items can be stored (persistence). So I tried the following

import java.util.Map
import java.util.HashMap

var stateNow = null
var stateNew = null

rule "Save Group"
when
    Item scene_id changed 
then
    stateNow = storeStates(gSzene)
    scene1.sendCommand(stateNow.toString)
end

rule "Restore Group"
when
    Item scene_selector changed 
then
    stateNew = scene1
    if(stateNew != null) restoreStates(stateNew)
end

scene1 is a String Item

Save Group works but in Restore Group I have a problem with

stateNew = scene1

I’ve already tried a few things, but I can not do it with this item.

Does anyone have a hint for me how I can solve the problem?

Thank You!

At the top, edit you var declarations:

var String stateNow = ""
var String stateNew = ""

rule "Restore Group"
when
    Item scene_selector changed 
then
    stateNew = scene1.state.toString
    if(stateNew != "") restoreStates(stateNew)
end

1 Like