Java Script to openHAB script or rule

Dear all

I would like to migrate a java script from a knx touchpanel to the openHAB environment (see code below) . Since I’m not a proficient java programmer I kindly ask for support from this community. First of all, question is if th java script should be migrated into a openHAB script or rule. Second question is how the script needs to be modified so that openHAB understands it. Any help would be highly appreciated.

function Lueftung_OnAction(me, eventInfo) {

var arr1 = new Array(‘9’);
arr1 [0] = project.getTag(“4/1/2 UG Fitness CO2 (C3)”);
arr1 [1] = project.getTag(“4/2/1 EG Wohnk_che CO2 (C20)”);
arr1 [2] = project.getTag(“4/2/3 EG Bad/WC CO2 (C21)”);
arr1 [3] = project.getTag(“4/2/5 EG Tochter CO2 (C22)”);
arr1 [4] = project.getTag(“4/3/1 OG Gallerie CO2 (C40)”);
arr1 [5] = project.getTag(“4/3/3 OG Bad/WC CO2 (C41)”);
arr1 [6] = project.getTag(“4/3/5 OG Gast CO2 (C42)”);
arr1 [7] = project.getTag(“4/3/7 OG Eltern CO2 (C43)”);
arr1 [8] = project.getTag(“4/3/9 OG Kind CO2 (C44)”);

function sortNum(a,b)
return b - a;

var CO=arr1[0];
project.setTag(“4/4/1 max CO2”, arr1[0]);

var arr2 = new Array(‘9’);
arr2 [0] = project.getTag(“4/1/1 UG Fitness Feuchte (F3)”);
arr2 [1] = project.getTag(“4/2/2 EG Wohnk_che Feuchte (H20)”);
arr2 [2] = project.getTag(“4/2/4 EG Bad/WC Feuchte (H21)”);
arr2 [3] = project.getTag(“4/2/6 EG Tochter Feuchte (H22)”);
arr2 [4] = project.getTag(“4/3/2 OG Gallerie Feuchte (H40)”);
arr2 [5] = project.getTag(“4/3/4 OG Bad/WC Feuchte (H41)”);
arr2 [6] = project.getTag(“4/3/6 OG Gast Feuchte (H42)”);
arr2 [7] = project.getTag(“4/3/8 OG Eltern Feuchte (H43)”);
arr2 [8] = project.getTag(“4/3/10 OG Kind Feuchte (H44)”);

var RH=arr2[0];
project.setTag(“4/4/2 Max Feuchte”, arr2[0]);

if (CO < “400” & RH <“30”)
project.setTag(“4/4/0 Schalten Stufe”,1);

else if (CO < “800” & RH < “60” & ! (CO < “400” & RH < “30”))
project.setTag(“4/4/0 Schalten Stufe”,0);

else if (CO < “1200” & RH < “80” & ! (CO < “800” & RH < “60”))
project.setTag(“4/4/0 Schalten Stufe”,2);

else if (! (CO < “1200” & RH < “80”))
project.setTag(“4/4/0 Schalten Stufe”,3);

alert (“Dieser Zustand ist nicht definiert !”);
return false;

I would think your first step would be set up the KNX binding. Then add a bunch of Number Items for readings.

There seems to be some ‘max value’ calculation in there, which you should be able to work with Groups of your new Items.

Then have rules triggered from changes in those max that take the actions you want.

Dear Rossko57

Thanks a lot for your hints. knx binding is already implemented, but the idea to work with Groups and use the max functionality and then have a simple rule is a great idea. I will try this and will report back.

Thanks again!

Best Rolf

Dear all

So, that is what I did to get the MAX value of the items (see below). Unfortunately no max value is shown but only a -. Am I missing something? any help would be highly appreciated!

Best Rolf

Items File:
Group:Number:MAX CO2 “Max CO2 [%.0f ppm]”
Group:Number:MAX Feuchte “Max Feuchte [%.0f %%]”

Number UG_Fitness_Feuchte “Feuchte Fitness [%.0f %%]” (Feuchte) {knx="<4/1/1"}
Number UG_Fitness_CO2 “CO2 Fitness [%.0f ppm]” (CO2) {knx="<4/1/2"}

Text item=Feuchte
Text item=CO2

First I would look to see if members of the group have numeric values

Dear rossko57

Again thank you very much for your help!! The issue was that the MAX function only started to work after a reboot of openHAB. So this one is resolved.

However the next issue I’m facing is the implementation of the rule (see below). The editor tells me that (since Stufe is a “Number”) it will not accept an int (I just took the numbers 1-3 for the values of Stufe). How can the numbers 1-3 be inputed so that it is accepted as a number in the SendCommand (btw: Stufe is a 17.001 DPT)?

Is there any other obvious mistake in this rule?

Many thanks for the great support!

Best Rolf

rule "fan"
Item Feuchte received update or
Item CO2 received update
if ((CO2.state < 400) && (Feuchte.state < 30))
SendCommand(Stufe, 1) //Stufe 0
else if ((CO2.state < 800 && Feuchte.state < 60) &&! (CO2.state < 400 && Feuchte.state < 30))
SendCommand(Stufe, 0) //Stufe 1
else if ((CO2.state < 1200 && Feuchte.state < 80) &&! (CO2.state < 800 && Feuchte.state < 60))
SendCommand(Stufe, 2) //Stufe 2
else if (!(CO2.state < 1200 && Feuchte.state < 80))
SendCommand(Stufe, 3) //Stufe 3
SendCommand(Stufe, 0)

First thing is, the Action is sendCommand(), not SendCommand(), but I’m pretty sure this won’t work, because the Action needs a string, so please use the Method instead. Second is, you have to use an as DecimalType because a state is of type state but not of type number. However, it may be that openHAB will silently cast to the correct type (but I had some rules where this did not work). And maybe you will have to provide a type conversion, too, at least Smarthome Designer throws an error without the .intValue part.

rule "fan"
    Item Feuchte received update or
    Item CO2 received update
    if ((CO2.state as DecimalType).intValue < 400 && (Feuchte.state as DecimalType).intValue < 30)
        Stufe.sendCommand(1) //Stufe 0
else if (((CO2.state as DecimalType).intValue < 800 && (Feuchte.state as decimalType).intValue < 60) &&! ((CO2.state as DecimalType).intValue < 400 && (Feuchte.state as DecimalType).intValue < 30))
        Stufe.sendCommand(0) //Stufe 1
else if (((CO2.state as DecimalType).intValue < 1200 && (Feuchte.state as DecimalType).intValue < 80) &&! ((CO2.state as DecimalType).intValue < 800 && (Feuchte.state as DecimalType).intValue < 60))
        Stufe.sendCommand(2) //Stufe 2
else if (!((CO2.state as DecimalType).intValue < 1200 && (Feuchte.state as DecimalType).intValue < 80))
        Stufe.sendCommand(3) //Stufe 3

I think I would make those into changed triggers, as you don’t care if the group is updated (a member updates) but no change in MAX follows.

Sorry, I have no idea what that means.

Dear Udo, dear rossko57

I have successfully migrated the Java script to openHAB, thanks to your help!!

Thank you a lot!

Btw: Is there an easy to use debugger program where one can test rules/scripts step by step (with shown iterim states of the variables). In my old Pascal programming times we often used such debuggers (sorry for this potentially silly question)

Best Rolf