SkipMorrow
(Skip Morrow)
January 26, 2019, 1:26pm
1
From the docs here:
We can set a state value like this:
val index = 5
sendCommand(“My_Lamp_” + index, ON)
But, how do I read the current value?
I’m thinking something like
val index = 5
logInfo (“test”, “MyLamp_” + index + " = " + getState(“MyLamp_” + index).toString)
vzorglub
(Vincent Regaud)
January 26, 2019, 1:33pm
2
When generating item names in rules you need a little workaround:
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…
//At the TOP of the rules file
import org.eclipse.smarthome.model.script.ScriptServiceUtil
val index = 5
val testItem = ScriptServiceUtil.getItemRegistry.getItem("Virtual_Switch_1")
logInfo (“test”, “MyLamp_” + index + " = " testItem.state.toString)
soesas
(Sascha_S)
January 26, 2019, 2:04pm
3
I think your question is covered in my solution Option 2
in below post:
So here are the two working Solutions:
Option 1: Using ScriptServiceUtil
import org.eclipse.smarthome.model.script.ScriptServiceUtil
when
Member of fg_gStatus_gSensor_gWindow changed from OPEN to CLOSED
then
val myItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name+"Timestamp")
//logInfo("Window", "Item " + myItem)
val duration = (now.millis - (myItem.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) / 1000
//logInfo("Window", triggeringItem.name+" was Open f…
so either filter groups, or use the ScriptServiceUtil
1 Like
SkipMorrow
(Skip Morrow)
January 27, 2019, 12:58pm
4
Solved.
I ended up using the ScriptService. Worked perfectly! Thanks
1 Like