Aggro600
(Nils K.)
November 11, 2020, 11:19pm
1
Hello,
my rule should change the light color with the color i stored in an item.
Is it possible to do this with a group.
Without Group (it works):
if (WZ_Haengelampe_Color.state.toString != WZ_Haengelampe_Color_Szene_Speicher.state.toString)
{
if(Motion_WZ_Status.state != 11)
{
Motion_WZ_Status.sendCommand("10")
}
WZ_Haengelampe_Color.sendCommand(WZ_Haengelampe_Color_Szene_Speicher.state.toString)
}
if (WZ_Spotlight_Color.state.toString != WZ_Spotlight_Color_Szene_Speicher.state.toString)
{
if(Motion_WZ_Status.state != 11)
{
Motion_WZ_Status.sendCommand("10")
}
WZ_Spotlight_Color.sendCommand(WZ_Spotlight_Color_Szene_Speicher.state.toString)
}
with group (doesn’t work):
gLampen_WZ_Motion_Einschalten.members.forEach[item |
var xxx = item.name
if (item.state.toString != xxx+"_Szene_Speicher".state.toString)
{
if(Motion_WZ_Status.state != 11)
{
Motion_WZ_Status.sendCommand("10")
}
item.sendCommand(xxx+"_Szene_Speicher".state.toString)
}
]
rossko57
(Rossko57)
November 11, 2020, 11:28pm
2
Aggro600:
xxx+"_Szene_Speicher"
That’s just a string. You can’t get the .state
of string.
For a couple years now, I’ve been wanting/looking for a way to construct an item from its name as a string. There are other ways to do this, and the most popular requires the item to be in a group, which can have it’s challenges. I’ll keep this short and SWEET… I finally figured it out, and it is extremely simple!
For OH 2.x, you’ll need this import…
import org.eclipse.smarthome.model.script.ScriptServiceUtil
For OH 3.x, this import instead…
import org.openhab.core.model.script.ScriptService…
The correct way to do it, is to use a second group for the scene store, too. like this:
gLampen_WZ_Motion_Einschalten.members.forEach[item |
val Speicher = gLampen_Szene_Speicher.members.filter[i| i.name.contains(item.name)].head
if(item.state.toString != Speicher.state.toString) {
if(Motion_WZ_Status.state != 11) {
Motion_WZ_Status.sendCommand("10")
}
item.sendCommand(Speicher.state.toString)
}
]
1 Like
Aggro600
(Nils K.)
November 12, 2020, 10:36pm
4
It works. Thank you so much.
Aggro600
(Nils K.)
November 12, 2020, 10:53pm
5
gLampen_Szene_Speicher.members.forEach[item |
val Speicher = gLampen_WZ_Motion_Einschalten.members.filter[i| i.name.contains(item.name)].head
item.sendCommand(Speicher.state.toString)
]
if I try it this way, to save the lamps color, it doesn’t work. What do i have to change?
Well, I used a shortcut here The name of the Item that holds the value contains the whole name of the item which shall be set.
this is a oneway function for sure…
But as you know the last part of the name is “_Szene_Speicher” and there is no other Item which is identical to the first part of the name, you can use the reverse function:
val Speicher = gLampen_WZ_Motion_Einschalten.members.filter[i| item.name.contains(i.name)].head
1 Like