Concatenate variables to get another variable-name (generic)

  • Platform information:

    • Hardware:RPi 3b
    • OS: Release = Raspbian GNU/Linux 9 (stretch)
    • Kernel = Linux 4.19.58-v7+
    • Platform = Raspberry Pi 3 Model B Rev 1.2
  • Java Runtime Environment:
    openjdk version “1.8.0_222”
    OpenJDK Runtime Environment (Zulu8.40.0.178-CA-linux_aarch32hf) (build 1.8.0_222-b178)
    OpenJDK Client VM (Zulu8.40.0.178-CA-linux_aarch32hf) (build 25.222-b178, mixed mode, Evaluation)

  • 2.5.0M2

I want to make a new variable out of two, to summarise in that variable within a rule.

So I have the following variables.

.rules (snippet)

rule "Test mit Variablen"
     Item Dummy4 changed to ON

    logInfo ("Test", "Starts" + " + " + triggeringItem)  
   var tmp_timestamp = new DateTime().toString("yyMMdd")
   //++++ Temperature Results for the days
   var Number sum_temp_day0 = 0
   var Number sum_temp_day1 = 0
   var Number sum_temp_day2 = 0
   var Number sum_temp_day3 = 0
   var Number sum_temp_day4 = 0
   var Number sum_temp_day5 = 0
   var Number avg_temp_day0 = 0
   var Number avg_temp_day1 = 0
   var Number avg_temp_day2 = 0
   var Number avg_temp_day3 = 0
   var Number avg_temp_day4 = 0
   var Number avg_temp_day5 = 0
   //+++++ for daily MIN/MAX Temperature-Values
   var Number min_temp_day0 = 999.9
   var Number min_temp_day1 = 999.9
   var Number min_temp_day2 = 999.9
   var Number min_temp_day3 = 999.9
   var Number min_temp_day4 = 999.9
   var Number min_temp_day5 = 999.9
   val suffix_day0 = "day0"
   val suffix_day1 = "day1"
   val suffix_day2 = "day2"
   val suffix_day3 = "day3"
   val suffix_day4 = "day4"
   val suffix_day5 = "day5"
   var suffixTemp_day "day"

// somewhere in the rule

        switch(tmp_timestamp) {
          case temp_day.get(0): {
                                 logInfo("owm-rules-test","Zeitstempel {} Count_Day0 {}  test {} ", tmp_timestamp, temp_day.get(vCounter_day) )  // test 15.8.19
                                 count_day0 = count_day0 + 1
                                 sum_temp_day0 = sum_temp_day0 + tempItemTemperature.getStateAs(DecimalType)
                                 //++++++++++++++++ Temperature Minimum of the day +++++++++++++++++++++++++++++++++++++++++++++++++
                                  var Number min = 0 
                                  min = min + tempItemTemperature.getStateAs(DecimalType)
                                  if (min_temp_day0 > min) {
                                   min_temp_day0 = min
                                  //+++++++++++++++ Temperature Maximum of the day +++++++++++++++++++++++++++++++++++++++++++++++++
                                  var Number max = 0
                                  max = max + tempItemTemperature.getStateAs(DecimalType)
                                  if (max_temp_day0 < max) {
                                   max_temp_day0 = max
                                  //+++++++++++++++ Rain Sum of the day +++++++++++++++++++++++++++++++++++++++++++++++++
                                  sum_rain_day0 = sum_rain_day0 + tempItemRainVolume.getStateAs(DecimalType)
                                   //+++++++++++++++ Snow Sum of the day +++++++++++++++++++++++++++++++++++++++++++++++++
                                  sum_snow_day0 = sum_snow_day0 + tempItemSnowVolume.getStateAs(DecimalType)
          case temp_day.get(1): {
                                 vCounter_day =1               
                                 logInfo("owm-rules-test","Zeitstempel {} Count_Day1 {}  test {} ", tmp_timestamp, temp_day.get(vCounter_day) )  // test 15.8.19
                                 count_day1 = count_day1 + 1
                                 sum_temp_day1 = sum_temp_day1 + tempItemTemperature.getStateAs(DecimalType)
                                 //++++++++++++++++ Temperature Minimum of the day +++++++++++++++++++++++++++++++++++++++++++++++++
                                  var Number min = 0 

At the moment this means in each “case” of the statemachine I summerize my values in fix-named variables like

count_day0 = count_day0 + 1
sum_temp_day0 = sum_temp_day0 + tempItemTemperature.getStateAs(DecimalType)

So my question is whether it’s possible to declare a temporary suffix in the “case” like this:

case temp_day.get(0): {
                       logInfo("owm-rules-test","Zeitstempel {} Count_Day0 {}  test {} ", tmp_timestamp, temp_day.get(vCounter_day) )  // test 15.8.19
                       suffixTemp_day = suffix_day0

and then make the calculation after switch/case in this way:

sum_temp_+suffixTemp_day = sum_temp_+suffixTemp_day + tempItemTemperature.getStateAs(DecimalType)

Hope my explanation is understandable. Maybe there’s someone to help me.
thx in advance.

Not really but I think you are looking for Design Pattern: How to Structure a Rule. If I understated what you are asking, the answer is yes.

1 Like

thx for reply. And for the hint.
I have a rule running, but it’s about 350 lines of code. So now I try to find another solution. But my knowledge about scripting and programming are very small. Even if I try to structure my rules, at the very end it’s mostly “spaghetti-code” :smirk:.

What I’m looking for is something like generic item but for variables:

var GenericItem tempItemTimestamp = ScriptServiceUtil.getItemRegistry.getItem("localHourlyForecast"+itemindex+"Timestamp") as  GenericItem

The above Codeline brings me the name of a real item “localHourlyForecast3Timestamp”, e.g.
Now I am wondering whether to do so with Variables. So maybe I’m on the wrong way.
So I will study the DP


There is not variable registry. But you can use a Map. There are tons of examples on the forum.


thx. I already tested with Map (and HashMap) and Arraylist, like below. And as you said there are tons of examples.

import org.eclipse.smarthome.model.script.ScriptServiceUtil
import java.util.Map
var Map<String, String> mDay = newHashMap
var logCount = 8
var Number logYes = 1  // it's no boolean - I know ;)

rule "Test mit Variablen"
     Item Dummy4 changed to ON

    logInfo ("Test", "Starts" + " + " + triggeringItem)  
   var vCounter_day = 0
   var vCount_temp_day = "day"+vCounter_day
    mDay.put("day0", new DateTime(localLastMeasurement.state.toString).toString("yyMMdd"))
    mDay.put("day1", new DateTime(localLastMeasurement.state.toString).plusDays(1).toString("yyMMdd"))
    mDay.put("day2", new DateTime(localLastMeasurement.state.toString).plusDays(2).toString("yyMMdd"))
    mDay.put("day3", new DateTime(localLastMeasurement.state.toString).plusDays(3).toString("yyMMdd"))
    mDay.put("day4", new DateTime(localLastMeasurement.state.toString).plusDays(4).toString("yyMMdd"))
    mDay.put("day5", new DateTime(localLastMeasurement.state.toString).plusDays(5).toString("yyMMdd"))
   var tmp_timestamp = new DateTime().toString("yyMMdd")
//---------------------------------------  Array List  --------------------------------------------------------------------------------------
   var day0 = new DateTime(localLastMeasurement.state.toString).toString("yyMMdd")
   var day1 = new DateTime(localLastMeasurement.state.toString).plusDays(1).toString("yyMMdd")
   var day2 = new DateTime(localLastMeasurement.state.toString).plusDays(2).toString("yyMMdd")
   var day3 = new DateTime(localLastMeasurement.state.toString).plusDays(3).toString("yyMMdd")
   var day4 = new DateTime(localLastMeasurement.state.toString).plusDays(4).toString("yyMMdd")
   var day5 = new DateTime(localLastMeasurement.state.toString).plusDays(5).toString("yyMMdd")

val temp_day = newArrayList(
       if (logYes==1) logInfo ("owm.rules", "DebugPoint xz: Before Initial If " + temp_day.get(1))
//       if (logYes==0) logInfo ("owm.rules", "DebugPoint xz: Before Initial If " + day1((temp_day.get(1))))

But that was not my favorite solution, as Maps seemed to be very slow (or need a lot of resources) on my RPi and even a lot of scripting lines but the “processing”-code will be shorter.


I suspect you had something else going wrong because Maps are nearly the fastest type of dates structure for reason access there is in all of computer science. Where you trying to lead it up with primitives? Are you on an RPi 1 or 0W?

Sorry Rich. My fault. My first intention was not right. Maybe there was a knot in my “spaghetti-code” or/ and in my brain :thinking: :crazy_face:. But after you have pushed me (one more time) into the right direction, I first read (as you said) in the DP an then began thinking about recreation of the Rule.
Today I was reading a lot about Maps and how to deal with( correctly! - I hope).
After all, at the moment I’m sitting here and recreate my Rule, using Maps and a state machine. And it looks quite good. At the moment it looks like the new rule will have more than 50 lines less than before and it works much faster as the old rule.

At the moment I have a RPi 3b and I’m thinking about a RPi 4 4GB.


1 Like