Javascript script

  • Platform information:
    • Hardware: Intel, 16GB RAM
    • OS: Windows 10
    • Java Runtime Environment: Zulu newest LTS
    • openHAB version: 3.0.0.0
  • Issue of the topic:
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Hello,

I would like to access openhab.variables in my Javascript script:

print(ā€˜EZ_DoNotDisturb.state.toStringā€™);
print(Staubsauger_Kueche.state.toString);

if (Staubsauger_Kueche == OFF) {
raum = 17;
print(raum);
}

But the state of the variable is not printed. Could you please help me to find the syntax errors?

Addion to that, is there somehow a documentation how to programm Javascript scripts in Openhab? I just found a very tiny one. Some examples would be great.

Thanks a lot in advance,
Peter

Welcome to the openHAB forum :wave:

Read This
The search functionon this forum could be your friend!

Nice, you helped me :slight_smile:

For example:
// Get the state of an Item
logger.info("The state of ServiceStatuses is " + items.ServiceStatuses);

Thank you!

If I run the following Javascript script:

print(items.Staubsauger_Kueche);

if (items.Staubsauger_Kueche == ON) {
  var raum = 17;
  print(raum);
}

if (items.Staubsauger_Flur == ON) {
  var raum = raum + ',' + 18;
  print(raum);
}

events.sendCommand(event.EZ_ExecuteCommand, "app_segment_clean[18]");  

I got following error:
ON
17
17,18

23:54:52.508 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID ā€˜443f42942aā€™ failed: ReferenceError: ā€œeventā€ is not defined in at line number 28

Does someone have a hint for me to fix the error?

event only exists when the rule is triggered by an Item event. If it was triggered manually (by pressing the play button), from another rule, or via a time based trigger.

And it appears you are misusing event here. To send a command to an Item, pass the name of the Item as a String.

events.sendCommand("EZ_ExecuteCommand", "app_segment_clean[18]");