Javascript in ioBroker – Script Engine Adapter

In ioBroker, the Script Engine adapter offers several options for creating custom control structures. We can easily connect smart-home devices from different manufacturers to it.

The ioBroker Script Engine adapter gives you enormous freedom and opportunity in home automation.

Beelink SER5 Mini PC

ad

Beelink SER5 Mini PC

AMD Ryzen 7 5800H Processor,Mini Computer with 16G DDR4 RAM/500GB M.2 NVMe 2280 SSD,4K FPS/Three-Screen Display/WiFi 6/BT5.2/Support Auto Power On.

Ideal for Smart Home, Mosquitto, ioBrokers or media servers.

Install Script Engine adapter

In order to create our own scripts in ioBroker, we need to install the “Script Engine” JavaScript adapter. Go to the “Adapters” tab, enter the search term “Script Engine” in the search box, and then install a copy of the javascript adapter.

install an instance of the javascript adapter

After installation, you will be taken to the “Script Engine” adapter settings page. We can leave the default settings here. This makes the ioBroker Script Enginer adapter ready to use.

Using the Script Engine adapter

Let’s go to the “Scripts” page.

The “Scripts” editor page consists of three parts. The first is the folder view, the second is the code editing panel, and the third is the console used to display the log.

The "Scripts" panel consists of three parts

The 1st panel is the directory tree, here will be our own scripts, we can arrange them in well-separated folders. In the header of the folder view, we find a horizontal menu, the first element of which is a drop-down menu.

The first element of the horizontal menu is a drop-down menu.

“Expert mode” in the drop-down list turns on the expert mode, which provides, among other things, the possibility of debugging. Furthermore, we can import our scripts, but we can also export scripts from external sources.

You can create a new folder using the folder icon with the “+” sign in the horizontal menu of the folder view.

You can create a new folder using the folder icon with the "+" sign

You can add a new script by clicking on the “+” sign in the menu.

Users with less programming experience can use the “Rules” or “Blocky” editor. The “Javascript” or “Typescript” editor provides a freer, more professional option.

ioBroker Script Engine Editor's Choice

By clicking on the pencil icon, we can rename our folders or scripts.

The 2nd panel is the code editor, here we can edit our scripts. In the following, we will use the Javascript editor.

In the code editor, you can edit several scripts at the same time, and you can switch between them using the tabs. By clicking the dark blue button next to the tabs, you can hide the folder view panel, thereby giving you a larger editing surface.

The editor panel also has a vertical menu.

The editor panel also has a vertical menu

Let’s take the menu items one by one. Clicking on the first “location” icon from the left selects the currently edited file in the folder view. The second icon restarts the script. The third is the “Play/Pause” icon to start/stop the running of the script.

The Clock icon calls up the CRON timer panel, which allows you to easily add timers to the script.

The Clock icon brings up the CRON timer panel.

The stars icon provides information about the times of daily astrological events.

The next is the “Task” icon, with the help of which we can insert the data points of the ioBroker objects into our code.

we can insert it into our code from the data points of the ioBroker objects.

The last two icons help with troubleshooting.

Let’s see some examples of using the Javascript adapter

Let’s write our first script, let it be Hello ioBroker!

console.log("Hello ioBroker!  Hello Javascript!");
Hello ioBroker script

The “console.log()” function can be used to write to the log, which is displayed on the console. You can also see these messages on the “Logs” page. This function can be useful for debugging, for example.

In the following example, we use the “getState()” function to query the state of the living room lighting, and accordingly the “setState()” function turns the light on or off in the living room.

var wohnzimmerlicht ='mqtt.0.Wohnzimmerlicht'/*Wohnzimmerlicht*/;
var wohnzimmerlicht_state = String(getState(wohnzimmerlicht).val);

if(wohnzimmerlicht_state == "true")
{
  setState(wohnzimmerlicht, "false");
}
else
{
    setState(wohnzimmerlicht, "true");
}

The code above is not very useful because it only works when running the script. Let’s change the code so that when some event happens, for example, a button is pressed, the light turns on.

The “on()” function is used to monitor changes in the data point stored in the “schalter” variable. if a state change occurs, the function runs and the light switches on. With this, we have automated the operation of the script.

var wohnzimmerlicht ='mqtt.0.Wohnzimmerlicht'/*Wohnzimmerlicht*/;
var schalter = 'mqtt.0.App.Schalter'/*Schalter*/

on({id: schalter}, function (obj)
{
  var wohnzimmerlicht_state = String(getState(wohnzimmerlicht).val);
  
  if(wohnzimmerlicht_state == "true")
  {
    setState(wohnzimmerlicht, "false");
  }
  else
  {
    setState(wohnzimmerlicht, "true");
  }
});

CanaKit Raspberry Pi 4 8GB Extreme Kit – 128GB Edition (8GB RAM)

Includes Raspberry Pi 4 8GB Model B with 1.5GHz 64-bit quad-core CPU (8GB RAM), 128GB Samsung EVO+ Micro SD Card (Class 10), USB MicroSD Card Reader, Premium High-Gloss Raspberry Pi 4 Case with Integrated Fan, CanaKit 3.5A USB-C Raspberry Pi 4 Power Supply, Heat Sinks, 2 Micro HDMI to HDMI Cables

ad

Includes Raspberry Pi 4 8GB Model B with 1.5GHz 64-bit quad-core CPU (8GB RAM), 128GB Samsung EVO+ Micro SD Card (Class 10), USB MicroSD Card Reader, Premium High-Gloss Raspberry Pi 4 Case with Integrated Fan, CanaKit 3.5A USB-C Raspberry Pi 4 Power Supply, Heat Sinks, 2 Micro HDMI to HDMI Cables

The following simple example uses the CRON timer to check the apartment temperature every two minutes. If the temperature drops below 15 degrees, an alarm will be sent using the Pushsafer service to the specified mobile phone. The pushsafer adapter is required for this.

var warning_once = false;

schedule(' */2 * * * *', function()
{
  var temp = Number(getState('mqtt.0.WohnzimmerTemp'/*WohnzimmerTemp*/).val);
  var text = 'The temperature of the apartment ' + temp;

  if(temp < 15)
  {
    if(warning_once == false)
    {
      sendTo("pushsafer", {
        message:   text,
        title:     'Low temperature warning',
        device:    'xxxxx',
        sound:     '5',
        icon:      '1',
        vibration: '1',
        priority:  '2',
      });
      warning_once = true;
    }
  }
  else
  {
    warning_once = false;
  }
})