Openhab3 rule port to openhab 4

Hi,

I´m trying to port my old openhab rule to openhab 4, but it does not work. I´m getting this error in the events.log:

**[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'TrocknerFertigmeldung' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "Gosund3_Gosund3Verbrauch" is not defined**

The item exists and is working. This is my code which I´m trying to port to openhab 4 using Chatgpt:

// Triggers:
// - When Gosund3_Gosund3Verbrauch changed

// Konstanten definieren
const MODE_OFF = "MODE_OFF";
const MODE_ACTIVE = "MODE_ACTIVE";
const MODE_FINISHED = "MODE_FINISHED";
var gosund3Verbrauch = items.getItem("Gosund3_Gosund3Verbrauch");

// Trockner_OpState definieren oder initialisieren
if (typeof Trockner_OpState === 'undefined') {
    var Trockner_OpState = "MODE_OFF";
}

// Zyklus-Variable definieren oder initialisieren
if (typeof cycle === 'undefined') {
    var cycle = 0;
}

// Script-Logik
if (Gosund3_Gosund3Verbrauch.state < 10 && Trockner_OpState.state == MODE_FINISHED && cycle == 0) {
    Trockner_OpState.postUpdate(MODE_OFF);
} else if (Gosund3_Gosund3Verbrauch.state > 100 && Trockner_OpState.state == MODE_OFF) {
    Trockner_OpState.postUpdate(MODE_ACTIVE);
    //Trockner_Laufzeit.sendCommand(1);
} else if (Gosund3_Gosund3Verbrauch.state < 10 && Trockner_OpState.state == MODE_ACTIVE && cycle == 0) {
    // Es gibt keine direkte Methode für Thread.sleep in JavaScript
    // Stattdessen könnte dies als separater Zeitsteuerungscode implementiert werden.
    // Hier wird eine Simulation eines kurzen Delays eingebaut
    setTimeout(() => {
        cycle = 1;
    }, 5000);
} else if (Gosund3_Gosund3Verbrauch.state < 10 && Trockner_OpState.state == MODE_ACTIVE && cycle == 1) {
    Trockner_OpState.postUpdate(MODE_FINISHED);
    cycle = 0;
}

Any idea?

edit: the “Gosund3_Gosund3Verbrauch” item is a MQTT item of a Gosund3 Thing.

Here’s another example for why ChatGPT is not suitable to use with OH for the creation of rules unless you are skilled enough to write said rules from scrtatch in the first place.

Please show your original working OH 3 rule. If it’s ambiguous, please include what language the rule is in.

Also mention what language you intend to use with OH 4 (you don’t have to change and I don’t recommend changing the language of your rules as part of the transition to OH 4 and instead, if that’s something desired, to convert rules over time). Include whether you want to have UI rules (aka managed rules) or file based rules.

If you are not proficient with coding I recommend Blockly.

The error implies you are attempting to create a JS Scripting UI rule. But it is not clear that is really what you want.

How have you defined this rule and what is this code copied from (e.g. did you create this as a JSRule in a .js file but pasted the contents of the Script Action from the UI)?

1 Like

this is the original code:

// Triggers:
// - When Trockner_TrocknerVerbrauchAktuell changed

// context: trockner-1
if (Trockner_TrocknerVerbrauchAktuell.state < 10 && Trockner_OpState.state == MODE_FINISHED && cycle==0) 
	{
	Trockner_OpState.postUpdate(MODE_OFF)
	}
	else if (Trockner_TrocknerVerbrauchAktuell.state > 100 && Trockner_OpState.state == MODE_OFF)
		{
		Trockner_OpState.postUpdate(MODE_ACTIVE)
		sendCommand('Trockner_Laufzeit', 1)
		}
		else if (Trockner_TrocknerVerbrauchAktuell.state < 10 && Trockner_OpState.state == MODE_ACTIVE && cycle == 0)	//> 100
		{
		Thread::sleep(5000)
		cycle=1
		}
		else if (Trockner_TrocknerVerbrauchAktuell.state < 10 && Trockner_OpState.state == MODE_ACTIVE && cycle == 1)
		{		
		Trockner_OpState.postUpdate(MODE_FINISHED)
		cycle=0
}

In OH 4 I created a new rule using the UI:

  • added a trigger “When Gosund3_Gosund3Verbrauch was updated”
  • Then execute an inline script
  • added the code

I realised it using blockly, thread can be closed.

1 Like