Hi,
it’s a general question about openHAB 3 and Rules. I have desperately researched, but found nothing zufriendstellendes. Supposedly this would work if I would use JavaScript. But I simply do not want to.
I have a rule that worked in openHAB 2 without any problems:
import java.util.List
import java.util.ArrayList
var List<GenericItem> hueLampsMWColor = null
var List<GenericItem> hueLampsMWToggle = null
var List<GenericItem> hueLampsMWCT = null
var List<GenericItem> hueLampsMWBright = null
var List<HSBType> rainbowColors = null
rule "Initialize"
when
// Rule is executed every time system is restarted or rules-file is reloaded
System started
then
// Initialize array with mw lamps color items in correct order
if(hueLampsMWColor === null){
hueLampsMWColor = new ArrayList<GenericItem>
hueLampsMWColor.add(iMultimedia_Hue_Lampe1_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe2_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe3_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe4_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe5_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe6_Farbe)
logInfo("rules", "List: hueLampsMWColor initialized")
}
// Initialize array with mw lamps toggle items in correct order
if(hueLampsMWToggle === null){
hueLampsMWToggle = new ArrayList<GenericItem>
hueLampsMWToggle.add(iMultimedia_Hue_Lampe1_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe2_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe3_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe4_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe5_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe6_Schalter)
logInfo("rules", "List: hueLampsMWToggle initialized")
}
// Initialize array with mw lamps temperature items in correct order
if(hueLampsMWCT === null){
hueLampsMWCT = new ArrayList<GenericItem>
hueLampsMWCT.add(iMultimedia_Hue_Lampe1_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe2_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe3_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe4_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe5_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe6_Farbtemperatur)
logInfo("rules", "List: hueLampsMWTemperature initialized")
}
// Initialize array with mw lamps brightness items in correct order
if(hueLampsMWBright === null){
hueLampsMWBright = new ArrayList<GenericItem>
hueLampsMWBright.add(iMultimedia_Hue_Lampe1_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe2_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe3_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe4_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe5_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe6_Helligkeit)
logInfo("rules", "List: hueLampsMWBright initialized")
}
// Initialize hand selected rainbow colors
if(rainbowColors === null){
rainbowColors = new ArrayList<HSBType>
rainbowColors.add(HSBType.fromRGB(255,0,0))
rainbowColors.add(HSBType.fromRGB(255,127,0))
rainbowColors.add(HSBType.fromRGB(255,255,0))
rainbowColors.add(HSBType.fromRGB(127,255,0))
rainbowColors.add(HSBType.fromRGB(0,255,0))
rainbowColors.add(HSBType.fromRGB(0,255,127))
rainbowColors.add(HSBType.fromRGB(0,255,255))
rainbowColors.add(HSBType.fromRGB(0,127,255))
rainbowColors.add(HSBType.fromRGB(0,0,255))
rainbowColors.add(HSBType.fromRGB(127,0,255))
rainbowColors.add(HSBType.fromRGB(255,0,255))
rainbowColors.add(HSBType.fromRGB(255,0,127))
logInfo("rules", "List: rainbowColors initialized")
}
logInfo("rules", "Initialization of global states is done")
end
rule "WindowsOpen"
when
Item iKonferenz_Homematic_Fenster1_Position changed to OPEN or
Item iKonferenz_Homematic_Fenster2_Position changed to OPEN or
Item iKonferenz_Homematic_Fenster3_Position changed to OPEN or
Item iKonferenz_Homematic_Fenster4_Position changed to OPEN or
Item iKonferenz_Homematic_Fenster5_Position changed to OPEN or
Item iKonferenz_Homematic_Fenster6_Position changed to OPEN or
Item iMultimedia_Homematic_Fenster1_Position changed to OPEN or
Item iMultimedia_Homematic_Fenster2_Position changed to OPEN or
Item iMultimedia_Homematic_Fenster3_Position changed to OPEN
then
logInfo("rules", "Fenster 1 state {}", iKonferenz_Homematic_Fenster1_Position.state.toString)
logInfo("rules", "Fenster 2 state {}", iKonferenz_Homematic_Fenster2_Position.state.toString)
logInfo("rules", "Fenster 3 state {}", iKonferenz_Homematic_Fenster3_Position.state.toString)
logInfo("rules", "Fenster 4 state {}", iKonferenz_Homematic_Fenster4_Position.state.toString)
logInfo("rules", "Fenster 5 state {}", iKonferenz_Homematic_Fenster5_Position.state.toString)
logInfo("rules", "Fenster 6 state {}", iKonferenz_Homematic_Fenster6_Position.state.toString)
logInfo("rules", "Windows open")
if((iKonferenz_Homematic_Fenster1_Position.state == "TILTED") ||
(iKonferenz_Homematic_Fenster2_Position.state == "TILTED") ||
(iKonferenz_Homematic_Fenster3_Position.state == "TILTED") ||
(iKonferenz_Homematic_Fenster4_Position.state == "TILTED") ||
(iKonferenz_Homematic_Fenster5_Position.state == "TILTED") ||
(iKonferenz_Homematic_Fenster6_Position.state == "TILTED") ||
(iMultimedia_Homematic_Fenster1_Position.state == "TILTED") ||
(iMultimedia_Homematic_Fenster2_Position.state == "TILTED") ||
(iMultimedia_Homematic_Fenster3_Position.state == "TILTED")) {
logInfo("rules", "One or more windows are tilted or opened")
// Show red lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.RED)
}
} else {
logInfo("rules", "One or more windows are opened")
// Show red lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.RED)
}
}
logInfo("rules", "Windows open")
end
rule "WindowsTilted"
when
Item iKonferenz_Homematic_Fenster1_Position changed to TILTED or
Item iKonferenz_Homematic_Fenster2_Position changed to TILTED or
Item iKonferenz_Homematic_Fenster3_Position changed to TILTED or
Item iKonferenz_Homematic_Fenster4_Position changed to TILTED or
Item iKonferenz_Homematic_Fenster5_Position changed to TILTED or
Item iKonferenz_Homematic_Fenster6_Position changed to TILTED or
Item iMultimedia_Homematic_Fenster1_Position changed to TILTED or
Item iMultimedia_Homematic_Fenster2_Position changed to TILTED or
Item iMultimedia_Homematic_Fenster3_Position changed to TILTED
then
logInfo("rules", "Fenster 1 state {}", iKonferenz_Homematic_Fenster1_Position.state.toString)
logInfo("rules", "Fenster 2 state {}", iKonferenz_Homematic_Fenster2_Position.state.toString)
logInfo("rules", "Fenster 3 state {}", iKonferenz_Homematic_Fenster3_Position.state.toString)
logInfo("rules", "Fenster 4 state {}", iKonferenz_Homematic_Fenster4_Position.state.toString)
logInfo("rules", "Fenster 5 state {}", iKonferenz_Homematic_Fenster5_Position.state.toString)
logInfo("rules", "Fenster 6 state {}", iKonferenz_Homematic_Fenster6_Position.state.toString)
logInfo("rules", "Windows tilted")
if((iKonferenz_Homematic_Fenster1_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster2_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster3_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster4_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster5_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster6_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster1_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster2_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster3_Position.state == "OPEN")) {
logInfo("rules", "One or more windows are tilted or opened")
// Show red lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.RED)
}
} else {
logInfo("rules", "One or more windows are tilted")
// Show green lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.GREEN)
}
}
logInfo("rules", "Windows tilted")
end
rule "WindowsClosed"
when
Item iKonferenz_Homematic_Fenster1_Position changed to CLOSED or
Item iKonferenz_Homematic_Fenster2_Position changed to CLOSED or
Item iKonferenz_Homematic_Fenster3_Position changed to CLOSED or
Item iKonferenz_Homematic_Fenster4_Position changed to CLOSED or
Item iKonferenz_Homematic_Fenster5_Position changed to CLOSED or
Item iKonferenz_Homematic_Fenster6_Position changed to CLOSED or
Item iMultimedia_Homematic_Fenster1_Position changed to CLOSED or
Item iMultimedia_Homematic_Fenster2_Position changed to CLOSED or
Item iMultimedia_Homematic_Fenster3_Position changed to CLOSED
then
logInfo("rules", "Fenster 1 state {}", iKonferenz_Homematic_Fenster1_Position.state.toString)
logInfo("rules", "Fenster 2 state {}", iKonferenz_Homematic_Fenster2_Position.state.toString)
logInfo("rules", "Fenster 3 state {}", iKonferenz_Homematic_Fenster3_Position.state.toString)
logInfo("rules", "Fenster 4 state {}", iKonferenz_Homematic_Fenster4_Position.state.toString)
logInfo("rules", "Fenster 5 state {}", iKonferenz_Homematic_Fenster5_Position.state.toString)
logInfo("rules", "Fenster 6 state {}", iKonferenz_Homematic_Fenster6_Position.state.toString)
logInfo("rules", "Windows closed")
if((iKonferenz_Homematic_Fenster1_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster1_Position.state === NULL || iKonferenz_Homematic_Fenster1_Position.state == UNDEF) &&
(iKonferenz_Homematic_Fenster2_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster2_Position.state === NULL || iKonferenz_Homematic_Fenster2_Position.state == UNDEF) &&
(iKonferenz_Homematic_Fenster3_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster3_Position.state === NULL || iKonferenz_Homematic_Fenster3_Position.state == UNDEF) &&
(iKonferenz_Homematic_Fenster4_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster4_Position.state === NULL || iKonferenz_Homematic_Fenster4_Position.state == UNDEF) &&
(iKonferenz_Homematic_Fenster5_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster5_Position.state === NULL || iKonferenz_Homematic_Fenster5_Position.state == UNDEF) &&
(iKonferenz_Homematic_Fenster6_Position.state == "CLOSED" || iKonferenz_Homematic_Fenster6_Position.state === NULL || iKonferenz_Homematic_Fenster6_Position.state == UNDEF) &&
(iMultimedia_Homematic_Fenster1_Position.state == "CLOSED" || iMultimedia_Homematic_Fenster1_Position.state === NULL || iMultimedia_Homematic_Fenster1_Position.state == UNDEF) &&
(iMultimedia_Homematic_Fenster2_Position.state == "CLOSED" || iMultimedia_Homematic_Fenster2_Position.state === NULL || iMultimedia_Homematic_Fenster2_Position.state == UNDEF) &&
(iMultimedia_Homematic_Fenster3_Position.state == "CLOSED" || iMultimedia_Homematic_Fenster3_Position.state === NULL || iMultimedia_Homematic_Fenster3_Position.state == UNDEF)) {
logInfo("rules", "All windows are closed")
// Switch lamps off
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.WHITE)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(OFF)
}
} else {
if((iKonferenz_Homematic_Fenster1_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster2_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster3_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster4_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster5_Position.state == "OPEN") ||
(iKonferenz_Homematic_Fenster6_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster1_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster2_Position.state == "OPEN") ||
(iMultimedia_Homematic_Fenster3_Position.state == "OPEN")) {
logInfo("rules", "One or more windows are opened")
// Show red lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.RED)
}
} else if((iKonferenz_Homematic_Fenster1_Position.state == "TILTED" && iKonferenz_Homematic_Fenster1_Position.state != "OPEN") ||
(iKonferenz_Homematic_Fenster2_Position.state == "TILTED" && iKonferenz_Homematic_Fenster2_Position.state != "OPEN") ||
(iKonferenz_Homematic_Fenster3_Position.state == "TILTED" && iKonferenz_Homematic_Fenster3_Position.state != "OPEN") ||
(iKonferenz_Homematic_Fenster4_Position.state == "TILTED" && iKonferenz_Homematic_Fenster4_Position.state != "OPEN") ||
(iKonferenz_Homematic_Fenster5_Position.state == "TILTED" && iKonferenz_Homematic_Fenster5_Position.state != "OPEN") ||
(iKonferenz_Homematic_Fenster6_Position.state == "TILTED" && iKonferenz_Homematic_Fenster6_Position.state != "OPEN") ||
(iMultimedia_Homematic_Fenster1_Position.state == "TILTED" && iMultimedia_Homematic_Fenster1_Position.state != "OPEN") ||
(iMultimedia_Homematic_Fenster2_Position.state == "TILTED" && iMultimedia_Homematic_Fenster2_Position.state != "OPEN") ||
(iMultimedia_Homematic_Fenster3_Position.state == "TILTED" && iMultimedia_Homematic_Fenster3_Position.state != "OPEN")) {
logInfo("rules", "One or more windows are tilted")
// Show green lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.GREEN)
}
} else {
logInfo("rules", "One or more windows are opened or tilted")
// Show red lamps
for(var int i = 0; i < hueLampsMWToggle.size(); i++){
hueLampsMWToggle.get(i).sendCommand(ON)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWBright.size(); i++){
hueLampsMWBright.get(i).sendCommand(100)
}
Thread::sleep(100)
for(var int i = 0; i < hueLampsMWColor.size(); i++){
hueLampsMWColor.get(i).sendCommand(HSBType.RED)
}
}
}
logInfo("rules", "Windows Closed")
end
What I could find was that in all the rules my “global” variables were zero. In openHAB 2, as I said, the approach was that I had a trigger with System started that I could use to initialize the variables for all the Rules. I can not do that outside of this rule, because at a system start, for example, not all items would be loaded. Otherwise I could do the whole thing outside of the rule. That’s for sure not the solution:
import java.util.List
import java.util.ArrayList
var List<GenericItem> hueLampsMWColor = null
var List<GenericItem> hueLampsMWToggle = null
var List<GenericItem> hueLampsMWCT = null
var List<GenericItem> hueLampsMWBright = null
var List<HSBType> rainbowColors = null
// Initialize array with mw lamps color items in correct order
if(hueLampsMWColor === null){
hueLampsMWColor = new ArrayList<GenericItem>
hueLampsMWColor.add(iMultimedia_Hue_Lampe1_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe2_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe3_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe4_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe5_Farbe)
hueLampsMWColor.add(iMultimedia_Hue_Lampe6_Farbe)
logInfo("rules", "List: hueLampsMWColor initialized")
}
// Initialize array with mw lamps toggle items in correct order
if(hueLampsMWToggle === null){
hueLampsMWToggle = new ArrayList<GenericItem>
hueLampsMWToggle.add(iMultimedia_Hue_Lampe1_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe2_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe3_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe4_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe5_Schalter)
hueLampsMWToggle.add(iMultimedia_Hue_Lampe6_Schalter)
logInfo("rules", "List: hueLampsMWToggle initialized")
}
// Initialize array with mw lamps temperature items in correct order
if(hueLampsMWCT === null){
hueLampsMWCT = new ArrayList<GenericItem>
hueLampsMWCT.add(iMultimedia_Hue_Lampe1_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe2_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe3_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe4_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe5_Farbtemperatur)
hueLampsMWCT.add(iMultimedia_Hue_Lampe6_Farbtemperatur)
logInfo("rules", "List: hueLampsMWTemperature initialized")
}
// Initialize array with mw lamps brightness items in correct order
if(hueLampsMWBright === null){
hueLampsMWBright = new ArrayList<GenericItem>
hueLampsMWBright.add(iMultimedia_Hue_Lampe1_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe2_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe3_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe4_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe5_Helligkeit)
hueLampsMWBright.add(iMultimedia_Hue_Lampe6_Helligkeit)
logInfo("rules", "List: hueLampsMWBright initialized")
}
// Initialize hand selected rainbow colors
if(rainbowColors === null){
rainbowColors = new ArrayList<HSBType>
rainbowColors.add(HSBType.fromRGB(255,0,0))
rainbowColors.add(HSBType.fromRGB(255,127,0))
rainbowColors.add(HSBType.fromRGB(255,255,0))
rainbowColors.add(HSBType.fromRGB(127,255,0))
rainbowColors.add(HSBType.fromRGB(0,255,0))
rainbowColors.add(HSBType.fromRGB(0,255,127))
rainbowColors.add(HSBType.fromRGB(0,255,255))
rainbowColors.add(HSBType.fromRGB(0,127,255))
rainbowColors.add(HSBType.fromRGB(0,0,255))
rainbowColors.add(HSBType.fromRGB(127,0,255))
rainbowColors.add(HSBType.fromRGB(255,0,255))
rainbowColors.add(HSBType.fromRGB(255,0,127))
logInfo("rules", "List: rainbowColors initialized")
}
I have a few questions about this now. Also because of such aspects as the scalability.
- Is it at all possible that multiple rules access one and the same variable in openHAB 3?
- Is it possible to access variables with the same name only within one text file or could I create multiple xxx.rules files and only in one of these files a variable is initialized?
- The cross check: Can I create global variables in two or more different xxx.rules files without them interfering with each other? So quasi if global variables are global only within one file.
What is clear to me is that val works in multiple rules because I just assign a final value outside there. As described above, I don’t want to keep null when I use var, but I want my “init” method to change this so that I don’t have to repeat this step in all three rules.
Thanks in advance.