Use an string item state as a name and state for another item

hi, can u plz help me guys with this… my items and rule:

My items:
Switch   Alarm_switch          "Alarm"
Contact  LivingRoom_Window     "Window [%s]"
String ALARM2_SENSOR           "Sensor"
String ALARM2_SENSOR_STATUS    "Sensor status"

my rules:
rule "ALARM sensors"
    Item Alarm_switch changed
	if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
	logInfo("TEST", ALARM2_SENSOR.state.toString)
    logInfo("TEST", ALARM2_SENSOR_STATUS.state.toString)
    sendCommand(LivingRoom_AirCon , ON)
  • ALARM2_SENSOR has a selection of [LivingRoom_Window=“Window sensor”]
  • ALARM2_SENSOR_STATUS has a selection of [OPEN=“Open”,CLOSED=“Closed”]

There is no error in the log but nothing is working… the loginfo returns below:

2018-03-26 23:25:45.518 [INFO ] [.eclipse.smarthome.model.script.TEST] - LivingRoom_Window
2018-03-26 23:25:45.519 [INFO ] [.eclipse.smarthome.model.script.TEST] - CLOSED

if i use:

if LivingRoom_Window == CLOSED

it works…so,what could be wrong with below:

if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)


Try this und post the log

   logInfo("TEST1", ALARM2_SENSOR.state.toString)
   logInfo("TEST2", ALARM2_SENSOR_STATUS.state.toString)
   if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
           sendCommand(LivingRoom_AirCon , ON)

You are basically comparing to strings:

if ("LivingRoom_Window" == "Closed") 

Which never evaluates to true.

hi, so whats will be the correct one?

2018-03-27 12:01:14.718 [INFO ] [eclipse.smarthome.model.script.TEST1] - LivingRoom_Window
2018-03-27 12:01:14.719 [INFO ] [eclipse.smarthome.model.script.TEST2] - CLOSED

You see, they are not equal

i did not understand ur point…

if i use:
if LivingRoom_Window == CLOSED, it works…the values retured in the log is the same…

if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)

ALARM2_SENSOR.state.toString is a string containing your item name. It will not work like that.
What is your purpose?
Why do you need to have an item containing the name of another item? It makes things really complicated.
What are you trying to achieve?
Can you explain your idea and maybe we can come up with a solution. It probably has been done before.

You need to get the item from your string value.

To achieve this, you need to put the sensor items in a group (gSensors) and filter the group by name. It’s close to the associated items pattern.

val mysensor = gSensors.members.filter[ i | == ALARM2_SENSOR.state.toString ].head
if (mysensor.state.toString == ALARM2_SENSOR_STATUS.state.toString)

But i second @vzorglub, this complicates everything. What are you really trying to achieve?

thanks for ur reply, what i want to achieve is that i want to write one rule to execute multiple tasks based on the items states from habpanel, and i dont want to keep repeating the rule for each item, im using variable item and state based on the habpanel selection… see below screenshot:




i appreciate if can help me to solve it using string state in if condition…

What you mean is

if (ALARM2_SENSOR.state == "LivingRoom_Window"

mmm… i got ur point, its comparing it as strings not as an item name and its state…

Hi Mo,
You need to put your items in groups and follow this design pattern
Forget about putting the item name in another item, you won’t get anywhere.

i tried yesterday below statement and it worked, u helped me yesterday with the same:

sendCommand(ALARM2_DEVICE.state.toString , ALARM2_COMMAND.state.toString)

im wondering why this one is not working:

if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)


sendCommand accepts 2 strings as arguments

so if condition doesn’t accept strings… so only way is Design Pattern

what abt:


That’s the same, It will not work!

Read the design pattern:

It is quite clever stuff
Don’t copy and paste the code.
Write it, yourself line by line, best way to learn
Try to understand the code, bit by bit, what it does and why

That’s how I started back in 80’s copying code from book
Making mistakes and learning that way

Maybe read a dummy guide of programming and understand the differences in types, variables, core functions and statements

Good luck!


thanks all of u…