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?
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.
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.
@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.
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.
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.
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?