Script in 4.1.2 does not work

I want to create a script in OH 4.1.2.
I created a script similar to the example from the 4.1.2 documentation:

logInfo("rules", "Rule Transfer KNX 0/5/0 State to HTTP ON/OFF Control");

  var item = items.HTTP_LED_Wohnzimmer_Decke_EG_Wohnzimmer_OmOff;
logInfo("rules", "Itemstate: " + item.state);
items.HTTP_LED_Wohnzimmer_Decke_EG_Wohnzimmer_OmOff.sendCommand(EG_Schaltaktor_11020_EG_Wohnzimmer_Decke_LED.STATE);

The script runs with the following error:

2024-09-15 17:26:27.918 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘d8d07a4f1c-LED-Wohnzimmer-Decke-knx-control’ failed: logInfo(“rules”, “Rule Transfer KNX 0/5/0 State to HTTP ON/OFF Control”);

var item = items.HTTP_LED_Wohnzimmer_Decke_EG_Wohnzimmer_OmOff;
logInfo(“rules”, "Itemstate: " + item.state);
items.HTTP_LED_Wohnzimmer_Decke_EG_Wohnzimmer_OmOff.sendCommand(EG_Schaltaktor_11020_EG_Wohnzimmer_Decke_LED.STATE);

  1. The method or field items is undefined; line 3, column 88, length 5
  2. The method or field items is undefined; line 5, column 187, length 5
  3. The method or field STATE is undefined for the type SwitchItem; line 5, column 296, length 5
 What is the problem here?

best

Heiko

You are combining two different script languages here. It’ not clear which one you intended to use, but you have both javascript and rulesDSL elements and the script is trying to run as rulesDSL.

So:

This line is full DSL and produces no errors

This line is javascript, because in the js based script languages you must uses the items object to access all the different items. In DSL you just use the name of the item directly. This explains the error: The method or field items is undefined.

This line gives two errors. The same one as above because you tried to use the javascript items object, and The method or field STATE is undefined for the type SwitchItem because inside the sendCommand you have the proper DSL method of just using the item name to refer to the item, but then you use STATE instead of state and properties and methods are case-sensitive in both javascript and DSL.

Which example script, on which page? I assume that the errors here are pieces that you have added, but if there are errors in the documentation we would like to know.

The forum tells us that your last post was 4 years ago which I’m guessing means that you are coming to OH4 from either OH1 or OH2 when rules DSL was the primary rules scripting language. The options have grown quite a bit in the intervening years. This means two things:

  1. When you are looking at rule examples and templates in the docs and in the forums you have to be careful to make sure you know which language you are looking at.
  2. Although DSL is not going away anytime soon, it is, at this point, probably the least of the many options. If you are starting fresh, or wish to expand your system now that you are on OH4 I would recommend that you start to move away from DSL. Blockly is probably the best option in this case as it has many features DSL does not and it’s graphical coding interface offers easy plug and play scripting and the ability to see/learn the javascript that it creates as you go.
4 Likes