I have 2 rules which function I would like to make it general
//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken kookplaat lampen aan"
//---------------------------------------------------------------------------------------------------------------------------------------------
when
Item KookLampenAan changed
then
if (KookLampenAan.state == OFF) {
Kooklampen.members.forEach[ i | i.sendCommand (OFF)]
} else {
Kooklampen.members.forEach[ i | i.sendCommand (ON)]
}
end
//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken lampkleur kookplaat"
//---------------------------------------------------------------------------------------------------------------------------------------------
when
Item KookLampenKleur received command
then
var hue = (KookLampenKleur.state as HSBType).getHue
var sat = (KookLampenKleur.state as HSBType).getSaturation
var b = (KookLampenKleur.state as HSBType).getBrightness
val HSBType color = new HSBType(hue, sat, b)
Kooklampen.members.forEach[ i | sendCommand(i, color.toString)]
end
Is it possible to write procedures/functions who will handle this. The call could be
rule
when
Item KookLampenAan changed
then
handleLights (KookLampenAan kookLampen)
end
rule
when
Item KookLampenKleur received command
then
handleLightsColor (KookLampenKleur Kooklampen)
end
As soon as you start heading down this path it is a strong sign you are outgrowing Rules DSL. All of the other options easily support this including Blockly. Rules DSL doesn’t support this simple use case very well.
You have two options to do this with Rules DSL.
One rule to handle them all. Put the Items into a Group, use a member of rule trigger, and use the implicit variables to create one rule that handles all the members of the Group in the same way. See Design Pattern: Associated Items for steps you can take to make doing this easier through careful Item naming (for example).
//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken kookplaat lampen aan"
//---------------------------------------------------------------------------------------------------------------------------------------------
when
Member of Lampen changed // Lampen has all the Switch Items you want processed by this rule
then
Kooklampen.sendCommand(newState) // Sending a command to a Group forwards to all members of the group
end
//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken lampkleur kookplaat"
//---------------------------------------------------------------------------------------------------------------------------------------------
when
Member of Lampen received command // all Items that should trigger this rule
then
Kooklampen.sendCommand(receivedCommand) // there is no need to recreate the HSBType
end
Note that in the second case, you don’t really need the rule. Just send the command to the Group Item in the first place.
cannot reference other global variables from inside a global lambda; you must pass them as an argument
You also have access to Rules DSL Scripts but these are:
- defined in a separate file
- invoked using [`callScript`](https://www.openhab.org/docs/configuration/actions.html#openhab-subsystem-actions)
- cannot use import statements
- cannot return anything
- everything needs to be passed into them
Because of that their use is significantly more limited.
Those examples are the old javascript and won’t work with the latest version.
The new Javascript is already installed.
To use it just create a new rule and select ecmascript.
If you are not familiar with Javascript then use blockly as it is graphical.
I’m confused. The JS automation I need to load or not?
I created a file with extension js in automation/js with something to test and this is working. Is this the correct way?
rules.when().item("KoelkastLampenAan").changed().then(event => {
items.KookLampenAan.sendCommand("OFF");
console.log("Dit is een test ", items.KoelkastLampenAan.state);
// console.log(event);
}).build("Test Rule", "My Test Rule");
I have never done it the way you have done it but it obviously works.
I just use the UI and no files.
The rule example you provided can be done via the UI and no coding needed.
Like this for example:
If you want to do it the way you are doing it and it is working then that is fine. I like the easy way.
You can add your own javascript in the rules as well.
For example the THEN part of the screen shot looks like this:
//the power status gets updated every 5 minutes I will use that as the trigger it is set in the tele period of the device
if (items.getItem('Trickle_charger_power').state < 11) {
items.getItem("Trickle_charger").sendCommand("OFF");
console.log('Turn off trickle charger');
}
console.log('Trickle charger '+ items.getItem('Trickle_charger_power').state);
I have not used any files in my set up. Just UI only.
Just go to the UI and click rules and see what I mean.
Yes, created in the UI but the blocks compile to JavaScript.
The Jython add-on is deprecated but there is a great third party Python support through HABApp. Ruby, Groovy, and JavaScript are supported by OH through add-ons. Java rules support has an unofficial add-on on the marketplace.
I haven’t looked at Pascal since high school in the mid 1990s so my memory is rusty but I’d say none of them is going to be a lot like Pascal. Maybe Java comes kind of close but IIRC Pascal never went OO and Java is all about OOP.
The docs for the JavaScript Scripting add-on are quite complete and well written. I’d say they are more complete and comprehensive than the Rules DSL docs (the same can be said for jRuby’s whose docs are also quite good).
Actually it’s not installed by default. However, it is one of the add-ons that are recommended during the first run wizard. As of OH 4 no add-ons get installed by default.
That looks like a good first example for using the Rule Builder syntax of the JS Scripting add-on. If that runs you are good to go!
Note, rule builder might not be sufficient for more complex rules so pay attention to the JSRule syntax too. You’ll need that as you move towards more complex stuff.
The picture supplied has the remove option on it so it should be installed in his case?
In Openhab there is always more than one way to do the same thing. So many choices.
In my case simple GUI and minimal javascript and that does what I need.
I also learnt Turbo Pascal 6 in high school, which I believe it did have OOP extensions where earlier Pascal did not… C## was then created by the person who created Turbo Pascal (overly simplified history) and Java is using the C/C++ syntax.
Arduino development uses C/C++
ESP32 development uses C/C++
JavaScript uses C/C++ syntax
openHAB development uses C/C++ syntax in Java
Android developement uses C/C++ syntax in Java and also Kotlin
Your knowledge can be used for multiple things in the future, and we always need more devs that know Java here. If your using Android and have an interest in making apps, then app development is also Java.
JavaScript could be useful for creating advanced widgets and web pages with so this has an advantage if you learn this.