# Array Loop question

**URL:** <https://community.openhab.org/t/array-loop-question/48047>\
**Category:** Beginners\
**Tags:** strings, array, loop\
**Created:** [July 19, 2018, 6:01am UTC](https://community.openhab.org/t/array-loop-question/48047 "2018-07-19T06:01:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 19, 2018, 6:01am UTC](https://community.openhab.org/t/array-loop-question/48047/1 "2018-07-19T06:01:15Z")

</div>

I read the address of my phone from openstreetmap using the geolocation.  
the result is a json string and contains strings and numbers like: Road, housenumber, suburb, town, city, state, postcode, …).

Sometimes the json response doe not contain one or the other and I get the entire json string back (not null as expected).

Anyway, what I do is i check for this issue like this:

```
    if(result.contains("town")) {
		town = transform("JSONPATH", "$.address.town", result)
	}
	else {
		town = "-"
	}

```

Now I would like to use a loop to concat these string items if they are NOT “-” like:  
road, house\_number, suburb, town, state.

I am sure there is a way to do it without checking each single string for beeing “-”, because I need to size down my number of code lines. 🙂

(with the same approach I could create a list of open contacts on my windows or active devices in my home (coffee machine, lights, …).

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 19, 2018, 6:32am UTC](https://community.openhab.org/t/array-loop-question/48047/2 "2018-07-19T06:32:11Z")

</div>

You could do that at the same time you check your json:

```php
    var String fullAddress = ""
    if(result.contains("town")) {
        town = transform("JSONPATH", "$.address.town", result)
        fullAddress = fullAddress + ", " + town
    } else {
        town = "-"
    }

```

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 19, 2018, 2:35pm UTC](https://community.openhab.org/t/array-loop-question/48047/3 "2018-07-19T14:35:09Z")

</div>

Good idea.  
However - if possible - I would like to handle the information in the full address depending if it’s in my home town (just street and suburb) instead of the full address including state and country.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 19, 2018, 3:19pm UTC](https://community.openhab.org/t/array-loop-question/48047/4 "2018-07-19T15:19:17Z")

</div>

Again, in the same rule:

```php
    var String fullAddress = ""
    if(result.contains("state")) {
        state = transform("JSONPATH", "$.address.state", result)
        if (state != "mystate") {
            fullAddress = fullAddress + ", " + town
        }
    } else {
        state = "-"
    }

```

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 19, 2018, 4:02pm UTC](https://community.openhab.org/t/array-loop-question/48047/5 "2018-07-19T16:02:16Z")

</div>

You’re fast.  
Thanks, I will try that.  
I hoped there is a more generic approach  
to cover the other use cases mentioned.

Anyway, this will help to get my code more streamlined.  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 19, 2018, 4:11pm UTC](https://community.openhab.org/t/array-loop-question/48047/6 "2018-07-19T16:11:58Z")

</div>

> [@NCO](#):
>
> to cover the other use cases mentioned.

In this case is probably the easiest way to do it. What are your other cases. There are as many solutions to as many problems and the best way to solve your other use cases may be very different.

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 20, 2018, 1:32pm UTC](https://community.openhab.org/t/array-loop-question/48047/7 "2018-07-20T13:32:52Z")

</div>

Thanks, I really appreciate your help.  
Will post the other case soon 🙂

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 23, 2018, 6:18am UTC](https://community.openhab.org/t/array-loop-question/48047/8 "2018-07-23T06:18:24Z")

</div>

This is one of the other cases.  
I assume there is a smarter / shorter way to do this!!?

```
rule "Open Windows / Door Check"
when
Item G_Melder changed
// or Time cron "24 0/7 * * * ?"
then
	if(G_Melder.state == ON) {
		var String open_Contacts_tmp = ""
		if(Abus_ENG_Sw.state == ON) {
			if(open_Contacts_tmp == "") {
				open_Contacts_tmp = "Eingang "
			}
			else {
				open_Contacts_tmp = open_Contacts_tmp + "/ Eingang "
			}
			logInfo("abus.rules", "open_Contacts_tmp set to: " + open_Contacts_tmp)	
		}
		if(Abus_BLK_Sw.state == ON) {
			if(open_Contacts_tmp == "") {
				open_Contacts_tmp = "Balkon "
			}
			else {
				open_Contacts_tmp = open_Contacts_tmp + "/ Balkon "
			}
			logInfo("abus.rules", "open_Contacts_tmp set to: " + open_Contacts_tmp)	
		}
		if(Abus_OBL_Sw.state == ON) {
			if(open_Contacts_tmp == "") {
				open_Contacts_tmp = "Oberlicht "
			}
			else {
				open_Contacts_tmp = open_Contacts_tmp + "/ Oberlicht "
			}
			logInfo("abus.rules", "open_Contacts_tmp set to: " + open_Contacts_tmp)	
		}
		if(Z_Bath_W_Sw.state == ON) {
			if(open_Contacts_tmp == "") {
				open_Contacts_tmp = "Badfenster "
			}
			else {
				open_Contacts_tmp = open_Contacts_tmp + "/ Badfenster "
			}
			logInfo("abus.rules", "open_Contacts_tmp set to: " + open_Contacts_tmp)	
		}
		open_Contacts.postUpdate(open_Contacts_tmp)
		logInfo("abus.rules", "open_Contacts updated to " + open_Contacts.state)
	}
	else {
		if(G_Melder.state == OFF) {
			open_Contacts.postUpdate("Keine")
			logInfo("abus.rules", "open_Contacts updated: " + open_Contacts.state)
		}
	}
end

```

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [July 23, 2018, 7:38am UTC](https://community.openhab.org/t/array-loop-question/48047/9 "2018-07-23T07:38:39Z")

</div>

If the items are in a group named gContacts, then you can do this…

```
val String open_Contacts_tmp = gContacts.members.filter [i | i.state == ON].map[label].reduce[s, label | s + ", " + label]

```

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 23, 2018, 8:30am UTC](https://community.openhab.org/t/array-loop-question/48047/10 "2018-07-23T08:30:21Z")

</div>

> [@5iver](#):
>
> val String open\_Contacts\_tmp = gContacts.members.filter [i | i.state == ON].map[label].reduce[s, label | s + ", " + label]

that would be awsome to replace everything with this approach.  
I will try is asap

Thanks a lot!!

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 23, 2018, 10:06am UTC](https://community.openhab.org/t/array-loop-question/48047/11 "2018-07-23T10:06:52Z")

</div>

thank you so much.  
It works like a charm - for another use case as well (active devices).  
For both rules, I was able to reduce the code from approx. 70 lines to 10 🙂

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 24, 2018, 5:56pm UTC](https://community.openhab.org/t/array-loop-question/48047/12 "2018-07-24T17:56:04Z")

</div>

I love it!  
Thanks again.  
I replaced 5 rules around 70-90 lines to 10 each.  
You are my hero.  
Hope to find some more for this approach 🙂

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 24, 2018, 8:32pm UTC](https://community.openhab.org/t/array-loop-question/48047/13 "2018-07-24T20:32:51Z")

</div>

Are your rules parsing times getting any better? I know that is what prompted this lines of code reduction path you are heading down in the first place.

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 25, 2018, 6:35am UTC](https://community.openhab.org/t/array-loop-question/48047/14 "2018-07-25T06:35:44Z")

</div>

I did not check it in detail, but it should.  
I have eliminated most of my sleeps (as you recommend in your code design suggestions (avoid long running rules).

That really opened my eyes.

However, I striggle with some minor issues (these were the reason to use sleep in the first place).

One is this:  
I have some rules which are triggered by xx received command.  
within this rule I check for triggered by ON or OFF.  
This was not working consistently, so I used sleep to wait for the command to settle (??).  
Anyway, this worked in all cases with a sleep of 2 seconds.

Now I have started to split these rules mentioned above into “received command ON” and received command OFF (without sleep of course).  
This seems to work much better.

Again, instead of expand my code with new stuff, I started to streamline it after I found your design strategy hints.  
So thanks a lot for that!

There is still a way to go though. 🙂

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [July 25, 2018, 6:48am UTC](https://community.openhab.org/t/array-loop-question/48047/15 "2018-07-25T06:48:27Z")

</div>

> [@NCO](#):
>
> I have some rules which are triggered by xx received command.  
> within this rule I check for triggered by ON or OFF.  
> This was not working consistently, so I used sleep to wait for the command to settle (??).  
> Anyway, this worked in all cases with a sleep of 2 seconds.

When using the ‘received command’ trigger, the item’s state has not yet been changed. To get around this, there is an [implicit variable](https://www.openhab.org/docs/configuration/rules-dsl.html#implicit-variables-inside-the-execution-block) that you can use to get the command that was received for the item… receivedCommand. You can use this instead of the item’s state, as this is what the item’s state will be changing to. No sleep required!

```
rule "Test"
when
    Item Test_Switch_1 received command
then
    if (receivedCommand == ON) {
        //do stuff
    }
    else if (receivedCommand == OFF) {
        //do other stuff
    }
end

```

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 25, 2018, 7:01am UTC](https://community.openhab.org/t/array-loop-question/48047/16 "2018-07-25T07:01:53Z")

</div>

Hey, that’s great.  
I did not even know “receivedCommand == ON” exists.  
(I guess I should read some more documentation in my spare time).

Thanks for the hint.

I guess there is no disadvantage if I use separate rules for received command ON / OFF in the meantime?

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [July 25, 2018, 7:28am UTC](https://community.openhab.org/t/array-loop-question/48047/17 "2018-07-25T07:28:39Z")

</div>

No. It is limited to the scope of the rule instance, just like any other local variable. Meaning you can use it (and the other implicit variables) in multiple rules without issue.

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 30, 2018, 5:25am UTC](https://community.openhab.org/t/array-loop-question/48047/18 "2018-07-30T05:25:08Z")

</div>

Hi,

I tried to use this in multiple cases to get rid of the issue with the delay between command and the final change of the item.

However, I use this and the command is received in events.log, but the rule is not triggered.

```
rule "Abus_Guardmode command exec"
when
	Item Abus_Guardmode received command
then
	if(receivedCommand == OFF) {
		executeCommandLine("curl http://xxx/setparam.cgi?event_i0_enable=0", 3000)
		logInfo("abus.rules: ", "Abus_Guardmode received command OFF")
	}
	else if(receivedCommand == ON) {
		executeCommandLine("curl http://xxx/setparam.cgi?event_i0_enable=1", 3000)
		logInfo("abus.rules: ", "Abus_Guardmode received command ON")
	}
end

```

Any idea?

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 30, 2018, 7:32am UTC](https://community.openhab.org/t/array-loop-question/48047/19 "2018-07-30T07:32:20Z")

</div>

Anything in the logs?

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [July 30, 2018, 12:32pm UTC](https://community.openhab.org/t/array-loop-question/48047/20 "2018-07-30T12:32:22Z")

</div>

The openHAB.log does not show anything - (no debug mode yet).

But actually deleting cache and temp file obviously helped.

So, it seems that the change was too big - changing lots of rules from receivedCommand ON / OFF to the setup with if(receivedCommand == ON).

Thanks for your quick response.  
I will keep a close eye on it.
