# Use String variable in if statement to change Item state

**URL:** <https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638>\
**Category:** Scripts & Rules\
**Tags:** variables\
**Created:** [April 30, 2021, 11:22am UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638 "2021-04-30T11:22:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 11:22am UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/1 "2021-04-30T11:22:31Z")

</div>

- Platform information:
  - OS: Raspbian GNU/Linux 10 buster on Pi 3

- openHAB version: 2.5.12

Hey Guys! Im kind of a newbie in OpenHAB, but i was wondering if someone here could answer my “short” question.:

First off here is the code, which is written into my .rules File:

```auto
var Number DebugCounter
val String CurrentBulbVar = "NULL"

rule "Bulbs Control"
when
    Item BulbCounterID changed
then
    if (BulbCounterID.state == 1) { CurrentBulbVar = "Bedroom_Light.sendCommand" }
    else if (BulbCounterID.state == 2) { CurrentBulbVar = "LivingRoom_Light.sendCommand" }
    else if (BulbCounterID.state == 3) { CurrentBulbVar = "HueAmbianceLamp1Brightness.sendCommand" }
    else if (BulbCounterID.state == 4) { CurrentBulbVar = "HueAmbianceLamp2Brightness.sendCommand" }
    else if (BulbCounterID.state == 5) { CurrentBulbVar = "HueAmbianceLamp3Brightness.sendCommand" }

    logWarn("DebugCurrentBulb", "Current Bulb Var has changed to " + CurrentBulbVar)
end

rule "Control Bulbs"
when
    Item BulbConfirmerID changed
then
    if (BulbConfirmerID.state == ON) { CurrentBulbVar (ON) }
    else if (BulbConfirmerID.state == OFF) { CurrentBulbVar (OFF) }
end

```

here are the .items files:  
(Virtual Items)

```auto
Group BulbStatusGrp "BulbStatusGrp"

Number BulbCounterID "BulbCounter" (BulnStatusGrp)
Switch BulbConfirmerID "BulbConfirmer" (BulnStatusGrp)
Switch DummySwitchID "DummySwitch" (BulbStatusGrp)
Switch DummySwitch2ID "DummySwitch2" (BulbStatusGrp)

```

(Tradfri Hardware)

```auto
Group Home "Home" <house>

Group Bedroom "Schlafzimmer" <bedroom> (Home)
Group LivingRoom "Wohnzimmer" <sofa> (Home)

Switch Bedroom_Light "Licht" <light> (Bedroom, gLight) {channel="tradfri:0220:gw4491603032fb:65537:brightness"}
Dimmer Bedroom_Light_Brightness "Schlafzimmer Helligkeit" <light> (Bedroom, gLight) {channel="tradfri:0220:gw4491603032fb:65537:brightness"}
Dimmer Bedroom_Light_ColorTemp "Schlafzimmer Farb Temp" <sun> (Bedroom, gLight) {channel="tradfri:0220:gw4491603032fb:65537:color_temperature"}
Number Bedroom_RemoteCtrl_BatteryStatus "Batterie Status" <battery> (Bedroom) {channel="tradfri:0830:gw4491603032fb:65539:battery_level"}
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
Switch LivingRoom_Light "Licht" <light> (LivingRoom, gLight) {channel="tradfri:0220:gw4491603032fb:65541:brightness"}
Dimmer LivingRoom_Light_Brightness "Wohnzimmer Helligkeit" <light> (LivingRoom, gLight) {channel="tradfri:0220:gw4491603032fb:65541:brightness"}
Dimmer LivingRoom_Light_ColorTemp "Wohnzimmer Farb Temp" <sun> (LivingRoom, gLight) {channel="tradfri:0220:gw4491603032fb:65541:color_temperature"}

/*Dummy Switch for rules*/
Switch AlleLichter "Alle Lichter"

```

So what Im trying to achieve is the switching between 5 different bulbs, therefore I have made the Virtual Items, “BulbCounterID” for example is used to define which Bulb should be controlled, at the moment I have set up the Switches in Basic UI:

 ![BasicUI](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/9/7/978c420e48b3a1c600e3f095b383367f71c1d483.png)  
I can switch between the Bulbs with the two arrows (up and down). You can ignore the “Dim Up” and “Dim Down” switches for now. My Problem is, that I would like to enable the currently selected Bulb with the “Bulb Confirmer” Switch. More precise: I want to know why the command:

```auto
if (BulbConfirmerID.state == ON) { CurrentBulbVar (ON) }

```

doesn’t work. Everything works if I just put the Item ID and .sendCommand instead of “CurrentBulbVar”, but if I do that I lose the ability to switch between the bulbs and control them, does anyone have an Idea?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 30, 2021, 12:12pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/2 "2021-04-30T12:12:52Z")

</div>

> [@Kami](#):
>
> `CurrentBulbVar (ON)`

This isn’t DSL syntax. What is CurrentBulbVar, it’s a string type variable right? It’s just a string, you cannot execute it.

What’s your objective here, to access an Item when you have its name in a string variable?

> [@\[Rules DSL\] Get item from string name!](https://community.openhab.org/t/rules-dsl-get-item-from-string-name/48279):
>
> 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…

There might be shortcuts to what you are about, by carefulchoice of Item names

> [@Design Pattern: Associated Items](https://community.openhab.org/t/design-pattern-associated-items/15790):
>
> Edit: Updates for OH 4 Please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for how to read and use DPs. Problem Statement Often one will have a number of separate Items which are all related to each other in some way. For example, one might have a Contact on a door and a DateTime to represent when the last time the door was opened. This is easy enough to keep track of if there are only one or two such Items, but if one has a lot of similar Items or is using a lot of gener…

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 12:47pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/3 "2021-04-30T12:47:09Z")

</div>

Yeah I would like to just put the ID of the current Item into the string variable, which I planned to insert into the command, if the BulbCounterID Item would’ve been 1 for example the string would be “Bedroom\_Light.sendCommand”. I thought I could just put the variable inside my if statement and instead of the Variable name it would execute the command like this:

```auto
Bedroom_Light.sendCommand (ON)

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 30, 2021, 1:53pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/4 "2021-04-30T13:53:55Z")

</div>

Okay, from the first link you should be able to get hold of an Item by its string name, then do what you like with it.

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 2:44pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/5 "2021-04-30T14:44:13Z")

</div>

okay Thanks very much,I’ll try it!

---

<div class="post-metadata">

**Author:** ![JustinG](https://community.openhab.org/user_avatar/community.openhab.org/justing/32/55948_2.png) [@JustinG](https://community.openhab.org/u/JustinG)\
**Post date:** [April 30, 2021, 2:52pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/6 "2021-04-30T14:52:15Z")

</div>

This might most easily be accomplished with a switch statement. A switch statement will let you choose between multiple different values of an expression (in this case your `BulbCounterID.state`). This should let you reduce this down to one simple rule. You don’t even need the `CurrentBulbVar` global or the `Bulbs Control` rule. Something like this:

```auto
rule "Control Bulbs"
when
    Item BulbConfirmerID changed
then
    switch BulbCounterID.state
        {
        case 1: { Bedroom_Light.sendCommand(BulbConfirmerID.state) }
        case 2: { LivingRoom_Light.sendCommand(BulbConfirmerID.state) }
        etc...
        }
end

```

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 4:01pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/7 "2021-04-30T16:01:54Z")

</div>

Hey! Thanks for the Idea!  
Unfortunately that doesn’t work, I’ve tried with your code (except for the “etc…” obviuosly), that didn’t work either ☹

```auto
rule "Control Bulbs"
when
    Item BulbConfirmerID changed or Item BulbCounterID changed
then
    switch BulbCounterID.state
        {
        case 1: { Bedroom_Light.sendCommand(BulbConfirmerID.state) }
        case 2: { LivingRoom_Light.sendCommand(BulbConfirmerID.state) }
        case 3: { HueAmbianceLamp1Brightness.sendCommand(BulbConfirmerID.state) }
        case 4: { HueAmbianceLamp2Brightness.sendCommand(BulbConfirmerID.state) }
        case 5: { HueAmbianceLamp3Brightness.sendCommand(BulbConfirmerID.state) }
        }
end

```

If I take a look at the openhab log I get the following error when I try to turn on the “BulbConfirmerID” Switch:

```auto

05:48:23.728 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Control Bulbs': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

```

May I have to import something?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 30, 2021, 4:19pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/8 "2021-04-30T16:19:33Z")

</div>

You cannot always **command** Items with **state** objects.  
You can get over that by commanding with string, and let the framework parse it into whatever type it needs.

```auto
val x = someItem.state.toString

other item.sendCommand(x)

```

---

<div class="post-metadata">

**Author:** ![JustinG](https://community.openhab.org/user_avatar/community.openhab.org/justing/32/55948_2.png) [@JustinG](https://community.openhab.org/u/JustinG)\
**Post date:** [April 30, 2021, 4:20pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/9 "2021-04-30T16:20:49Z")

</div>

Sorry, typing quickly this morning. I forgot to stringify the item states. The error tells you that the sendCommand function expects a val of string type and item.state gives you a state type. Add `.toString()` to the end of each `BulbConfirmerID.state` → `BulbConfirmerID.state.toString()`.

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 4:44pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/10 "2021-04-30T16:44:32Z")

</div>

Unfortunately

> [@rossko57](#):
>
> ```auto
> val x = someItem.state.toString
> 
> other item.sendCommand(x)
> 
> ```

And

> [@JustinG](#):
>
> `BulbConfirmerID.state.toString()`.

Don’t work, there are no error messages anymore, but the Bulbs still don’t receive a command. If you are wondering if the Bulbs are even online and controlable: yes they are, I can switch them on and off in PaperUI and BasicUI.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 30, 2021, 4:53pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/11 "2021-04-30T16:53:23Z")

</div>

Show us the full rule now please. Don’t to-string your switch-case.

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 5:01pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/12 "2021-04-30T17:01:40Z")

</div>

Here you go:

```auto
var Number DebugCounter
var Number Helligkeit1
var Number Helligkeit2
var Number Helligkeit3
var Number Helligkeit4
var Number Helligkeit5
//var String CurrentBulbVar = "NULL"

rule "DebugLog"
when
    Item BulbCounterID changed
then
    DebugCounter = BulbCounterID.state.toString
    logWarn("DebugCounter", "BulbCounter changed to " + DebugCounter)
end

/*----------Dim Bulbs----------*/
/* You can ignore this Part
rule "DimBulbsUp"
when
    Item DummySwitchID changed to ON or Item DimmschalterDimmerSwitch changed to 2001.0
then
    Bedroom_Light_Brightness.sendCommand(5)
    Helligkeit1 = Bedroom_Light_Brightness.state as Number
    while (Helligkeit1 < 100 && DummySwitchID.state == ON) {
    Helligkeit1 = Helligkeit1 + 5
    Bedroom_Light_Brightness.sendCommand(Helligkeit1)
    Thread::sleep(700)
    if (Helligkeit1 < 5) { DummySwitchID.sendCommand(OFF) }
    }

end

rule "DimBulbsDown"
when
    Item DummySwitch2ID changed to ON
then
    Helligkeit1 = Bedroom_Light_Brightness.state as Number
    while (Helligkeit1 > 5 && DummySwitch2ID.state == ON) {
    Helligkeit1 = Helligkeit1 - 5
    Bedroom_Light_Brightness.sendCommand(Helligkeit1)
    Thread::sleep(700)
    }
end
/*----------Dim Bulbs----------*/

/*TEST AREA*/

rule "Event Trigger Debug"
when
   Channel "hue:0820:2ceacbcb:dimmer_switch_event" triggered
then
    logWarn("DimmerTest", "Dimmer switch event was triggered ", dimmer_switch_event.state.toString)
end

rule "Control Bulbs"
when
    Item BulbConfirmerID changed or Item BulbCounterID changed
then
    switch BulbCounterID.state.toString()
        {
        case 1: { Bedroom_Light.sendCommand(BulbConfirmerID.state.toString()) }
        case 2: { LivingRoom_Light.sendCommand(BulbConfirmerID.state.toString()) }
        case 3: { HueAmbianceLamp1Brightness.sendCommand(BulbConfirmerID.state.toString()) }
        case 4: { HueAmbianceLamp2Brightness.sendCommand(BulbConfirmerID.state.toString()) }
        case 5: { HueAmbianceLamp3Brightness.sendCommand(BulbConfirmerID.state.toString()) }
        }
end

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 30, 2021, 5:03pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/13 "2021-04-30T17:03:56Z")

</div>

Because for some mysterious reason you chose to change the switch statement which was working to a string as well, you must now change your case statements to strings as well or they won’t match.

---

<div class="post-metadata">

**Author:** ![Kami](https://community.openhab.org/user_avatar/community.openhab.org/kami/32/77173_2.png) [@Kami](https://community.openhab.org/u/Kami)\
**Post date:** [April 30, 2021, 5:11pm UTC](https://community.openhab.org/t/use-string-variable-in-if-statement-to-change-item-state/121638/14 "2021-04-30T17:11:37Z")

</div>

Okay Thank you very much, it’s working now after I removed “.ToString()” from:

> [@Kami](#):
>
> `switch BulbCounterID.state.toString()`

I’ve tried this before, but it didn’t work, I don’t know what I’ve done, but it wasn’t right 😅 😂

Thank you for the help @rossko57 @JustinG, I really appreciate it \<3
