[SOLVED] Using A Variable To Obtain An Item State

Yes this is possible, please take a look at this very educative Design pattern of @rlkoshak:

Based on this, I use something like this myself:

val name = triggeringItem.name.split("_").get(0)
val setpoint = AllThermostatSetpoints.members.findFirst[ t | t.name == name + "_SetpointHeat" ] as NumberItem
val temp = AllThermostats.members.findFirst[ t | t.name == name + "_CurrentTemperature" ] as NumberItem
val mode = AllThermostatModes.members.findFirst[ t | t.name == name + "_ThermostatMode"] as NumberItem

So for example I have a rule which is triggered when Member of AllThermostatSetpoints changed or Member of AllThermostats changed, and all items in these groups have a name like Thermostat1_SetpointHeat or Thermostat_CurrentTemperature, respectively.
So the first line gets the base name, so “Thermostat1” in this example. Then setpoint finds the item Thermostat1_SetpointHeat, so that holds that item, and so on.
Subsequently you can use setpoint.state to get the value of the setpoint item, mode.sendCommand to set the value of the Thermostat1_ThermostatMode item, and so on.

Essential in this approach is that all _SetpointHeat items are in the same AllThermostatSetpoints group, and so on.

EDIT: @vzorglub beat me to it, with another nice approach, which might also be more efficient in this case.

1 Like