How to string array syntax?

Hi.
I need string array with 10 elments, and cycle to access each element.
I didnt found examples in wiki.

Thanx for help.

At least according to Designer’s syntax highlighting it doesn’t appear that openHAB’s rules support arrays so you will need to use a List.

val myList = newArrayList('One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten')

Looping will depend on what you want to do. You can do a forEach:

myList.forEach[ str |
    // do stuff
]

or an Iterator:

val iterator = myList.iterator
while(iterator.hasNext) {
    val str = i.next
    // do stuff
}

Needing to create an array like this and iterate over them is a rare need which is why you didn’t find an example in the wiki.

Rich

2 Likes

How to create fixed size array?
I use this code, but it ugly:

var OledLines = newArrayList()
(0..31).forEach[
    OledLines.add("")
]

 OledLines.set(20, "Test")

I use it to post messages to OLED display in particular line.

The Rule’s DSL doesn’t have good support for arrays. ArrayList isn’t really an array, its a collection and it is not fixed sized.

You can try to see if one the following with work:

var OledLines = newArrayList(32)

var List<String> OledLines = new ArrayList(32)

Note that you will have to import both java.util.List and java.util.ArrayList for that second example to work.

Another cheezy hack could be:

var OledLines = " , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,".split(',')

Hello @rlkoshak

I wrote simple script to switch off all lights in my home. But there is the problem with array, in this script are executed onlu first 10 elements from arrach. The rest of them, when I trrying to read the array some errors in the string are.

Could you help me in this case? When OH is planing to improve scripts for array working?

logInfo("22:00 - ALL LIGHTS IN HOME OFF AT 22:00", "");

val String vlSwitchSalon01     = "tasmota_34DAFA";  //01
val String vlSwitchSalon02     = "tasmota_1222E4";   //02
val String vlSwitchSalon03     = "tasmota_34D599";   //03
val String vlSwitchSalon04     = "tasmota_34DB77";   //04
val String vlSwitchKorytarz01  = "tasmota_344CE2";   //05
val String vlSwitchAsia01      = "tasmota_129640";   //06
val String vlSwitchKorytarz02  = "tasmota_0EF091";   //07
val String vlSwitchKorytarz03  = "tasmota_10FEC7";   //08
val String vlSwitchStrych01    = "tasmota_11152C";   //09
val String vlSwitchKuchnia01   = "tasmota_353116";   //10
val String vlSwitchPiec01      = "tasmota_1110BD";   //11
val String vlSwitchSypialnia01 = "tasmota_353164";   //12
  
val vlSwitches = newArrayList(vlSwitchSalon01, vlSwitchSalon02, vlSwitchSalon03, vlSwitchSalon04, vlSwitchKorytarz01, vlSwitchAsia01, vlSwitchKorytarz01, vlSwitchAsia01, vlSwitchKorytarz02, vlSwitchKorytarz03, vlSwitchStrych01, vlSwitchKuchnia01, vlSwitchPiec01, vlSwitchSypialnia01);

var viTemp = -1;
var vsTemp = "" as String;
var vsMQTTCmd = "" as String;
var viListCount = 12;
var mqttActions = getActions("mqtt","mqtt:broker:d2e8e6744e");
//for (viTemp=0;viTemp<4;viTemp++) {
while((viTemp=viTemp+1)<viListCount){
  vsTemp = vlSwitches.get(viTemp);
  logInfo("22:00 OFF for", "switch: {}",vsTemp);
  vsMQTTCmd = 'cmnd/'+vsTemp+'/POWER';
  mqttActions.publishMQTT(vsMQTTCmd, "OFF");
  vsMQTTCmd = 'cmnd/'+vsTemp+'/POWER1';
  mqttActions.publishMQTT(vsMQTTCmd, "OFF");
  vsMQTTCmd = "cmnd/"+vsTemp+"/POWER2";
  mqttActions.publishMQTT(vsMQTTCmd, "OFF");
 }

Maybe. Is there a reason these switches are not represented by separate Items? Then you’d just put them all into a Group and you can send one command to the Group Item and it will get forwarded to all the Group members.

This is a six-year-old thread. I haven’t seriously used Rules DSL in a long time.

I notice that you’ve not tried the forEach approach. That’d be the post appropriate for this case.

vlSwitches.forEach[ light | 
  logInfo("22:00 OFF for", "switch: {}", vsTemp) // not you do not use ; in Rules DSL except for return
  vsMQTTCmd = 'cmnd/'+vsTemp+'/POWER';
  mqttActions.publishMQTT(vsMQTTCmd, "OFF")
  mqttActions.publishMQTT(vsMQTTCmd+"1", "OFF");
  mqttActions.publishMQTT(vsMQTTCmd+"2", "OFF");    
]

Never, at least for Rules DSL. Rules DSL served it’s purpose but has since been surpassed in features and ease of use by pretty much any of the other rules options available in OH 3.0+. If you are not a great programmer Blockly is an excellent choice.

OK, thank you for your reply. So what is a alternative for RulesDSL? OH 4.x is supported javascript or something else? I don’t like “block programming” :frowning:

Officially supported add-ons include:

  • JS Scripting (required for Blockly)
  • jRuby
  • Jython
  • Nashorn JavaScript (legacy)
  • Groovy

There’s also JRule on the marketplace for Java based rules, HABApp for an external pure Python 3 rules environment, and some people use NodeRed for rules as well.

Yes, they are - each of gong (mostly 2-gongs per switch) are represented by separated item.

Very good idea to group them! Thank You! Very good idea!

:metal: :+1:

Thank you! Everything is clear! Maybe not everything becuse I’m begginer with OH, but spet by step more is clear :slight_smile: Thank you. Have a nice evening.

Then why don’t you use the items at all?

Hello Udo,

do you mean something like this?

SwitchSypialnia01_SwitchSypialnia01A.sendCommand(OFF)

By using array, my main idea was to use mqtt path as string defined once in array and re-use it (as Ctrl-C/V) in another scrips without need of write it again.

But is array mechanism is not perfect in OH now it seems that I have to use excatly sendCommand.

gAllLights.sendCommand(OFF)

Make all the switch items members of the gAllLights group.

rlkoshak proposed that but if I have a one switch1 as member related to room1, and the 2nd switch2 related to room2, and then if trying to group them to new group allSwitches then switch1 and switch2 disappper in the model from these room :frowning:

maybe I’m doing something wrong.

The Group you create to control the lights must not be in the semantic model. Make sure the Group has no tags.

A semantically tagged Item can only be a member of one semantically tagged Group.