Recipes in the javascript adapter: ready-made automations with your own devices

Recipes in the javascript adapter: ready-made automations with your own devices

Light on motion, heating down while a window is open, a message when the washing machine is done: almost everyone builds automations like these sooner or later. Until now that meant copying an example from the forum and replacing all object IDs in it with your own. With the recipes in the script editor of the javascript adapter it works differently: you pick a ready-made automation, assign your devices, and the editor creates a finished script from it. You don't need to program for this, neither in JavaScript nor in Blockly.
We briefly mentioned the recipes in the monthly review for November. This post shows how they work, what happens when a recipe is updated, and how you can share your own automation as a recipe with everyone.

What a recipe is

A recipe is a script in Blockly, JavaScript or TypeScript with placeholders instead of fixed object IDs, plus a list of inputs: which devices, which times, which thresholds. When the script is created, the editor puts your devices and values into the placeholders. The result is a completely normal script of the same kind in the folder Rezepte (script.js.Rezepte.*) that you can look at, understand and change like any other. There is no runtime of its own; the javascript adapter runs the script like all the others.
The recipes come from the catalog ioBroker.recipes on GitHub. Anyone can read a Blockly recipe, even without programming skills. What is hard to express with blocks is written as a recipe in JavaScript or TypeScript. When you apply a recipe, that makes no difference to you: you choose devices and values, and the editor does the rest.
⚠️ Version: Recipes are available from javascript 10.6.1, recipes in JavaScript and TypeScript from 10.7.0. The stable repository still has 10.4.4. If you want to try the recipes, switch the repository to “latest” in the system settings and update the javascript adapter.
In just over two minutes our video shows the whole process (in German): choosing a recipe, assigning devices, creating the script and adjusting it.

Applying a recipe

Everything starts where you always create a new script: with the plus in the script editor. Above the tiles for Rules, Blockly and the others there is now a line that suggests starting with a ready-made automation, with the button Browse recipes to its right. It is easy to miss, because your eye goes straight to the big tiles.

1. Choose a recipe

On the left is the list of recipes, with a search field and tags such as “licht” (light), “fenster” (window) or “energie” (energy) above it. Next to every recipe you see the language it is written in. On the right you see the description of the chosen recipe, what you will have to select, and a preview of the blocks or the code. We first show the steps with the Blockly recipe “Light on motion”.

2. Assign devices

This is where the real convenience lies. For a device input, the list only offers devices that fit, sorted by room. They are detected with the type detector, just like in the device lists of ioBroker. For the recipe “Light on motion”, the lamps input therefore shows only lights and dimmers, and the motion sensor input only motion sensors. The type is shown next to every device.
Some inputs are not a device but a single state, for example the power of a washing machine. For those, the object browser opens, already filtered by a matching role and type. Times, thresholds and texts come with sensible defaults that you can keep or change. At the bottom left you see what is still missing before you can continue.

3. Check and create

Before anything is saved, the last step shows the finished blocks with your devices. If you like, you can also expand the generated JavaScript below them. Create puts the script into the folder Rezepte and starts it right away if you want.
In this example, one dimmable and one switchable lamp are selected. The recipe takes care of that by itself: the dimmer gets a brightness of 100 percent, the simple lamp gets “true”.

The finished script

The script looks like any other Blockly script; only a notice above it tells you which recipe and which version it comes from. You can edit it, rename it or move it to another folder.
In the object of the script, the editor stores under common.recipe which recipe it was created from: the name and version of the recipe, the selected devices (only their IDs), the values you set, and a checksum (SHA-256) of the generated code. It needs the checksum later to tell whether you changed the script.

Recipes in JavaScript and TypeScript

You apply a recipe in JavaScript or TypeScript in exactly the same way. The catalog contains, for example, Average temperature, a recipe in TypeScript: it writes the average of several temperature sensors into a state whenever one of them changes. Sensors without a value are left out. Instead of blocks, the preview shows the code with its placeholders.
You assign the devices as usual, here thermostats and temperature sensors, and a state as the target, for example in 0_userdata.0. The step Check then shows the finished code: every placeholder is replaced by a complete value, an ID by a text in quotes, a number by a number.
The result is an ordinary TypeScript script in the folder Rezepte. Here, too, the editor remembers which recipe it came from and offers updates later.

Updates for recipes

Recipes get improved, for example when someone finds a special case or a device type is added. If the catalog has a newer version of your recipe, a notice with the button Update appears above the script.
The update creates the script again with your stored settings. So you don't have to select all devices again; you are only asked for inputs the new version needs in addition. Before saving, a comparison shows what changes.
If you changed the script yourself after it was created, the dialog warns you that these changes will be lost. In the picture below, the switch-off time was set to 300 seconds by hand; the update would set it back to the 120 seconds from the settings.
If you want to keep your changes, click Detach instead. The script then stays exactly as it is, but gets no more updates from the catalog.

What is in the catalog

Right now the catalog contains nine recipes, eight in Blockly and one in TypeScript:
  • Light on motion: turns lamps on when there is motion and off again after a time without motion, optionally only when it is darker than a threshold. It only switches off what the recipe switched on itself.
  • Staircase light: a button turns the lamps on for a set time; pressing it again restarts the time.
  • Blinds by the sun: opens in the morning with the sun, but not before a set time, and closes in the evening when the sun sets.
  • Lower the heating while a window is open: lowers the set temperature when a window or door is open too long and restores the previous temperature afterwards.
  • Window open for too long: reports every window and door that is open longer than set; tilted counts as open.
  • Washing machine is done: reports when the power stays below a threshold for a while after washing.
  • Switch on with solar surplus: turns a socket on when the photovoltaic system feeds in enough power, and off again when the surplus is gone. It only switches off what it switched on itself, and notices when someone switches the socket off by hand.
  • Water leak alarm: warns when water is detected and, if you like, switches off sockets, for example of a washing machine or a pump.
  • Average temperature (TypeScript): writes the average of several temperature sensors into a state.
The recipes write their messages to the log and, if you specify a state, to that state as well, for example telegram.0.communicate.response. That way the message goes straight to your phone.
Nine recipes are only the beginning. The catalog is meant to grow with you.

Sharing your automation as a recipe

You built an automation that would help others too? Then turn it into a recipe, whether it is written in Blockly, JavaScript or TypeScript. For that, every Blockly, JavaScript and TypeScript script has the icon Export as recipe, an open book, in the toolbar of the editor.
The dialog lists all states the script uses. For each of them you decide whether it becomes an input. If a state belongs to a detected device, the dialog suggests the device with its type; otherwise it becomes a simple state input. Add a title and description in English and German and your GitHub user name.
A Blockly recipe is downloaded as one JSON file. A recipe in JavaScript or TypeScript consists of two files that you download one by one: the JSON file with texts and inputs, and the code as .js or .ts. In the code, the export finds the IDs in texts in quotes, not in comments, and replaces them, quotes included, by placeholders. What it cannot know, such as numbers the user should choose, you change by hand afterwards.
To share the recipe with everyone, open a pull request with these files in the catalog. How exactly that works is described in CONTRIBUTING.md. You need a GitHub account for this; there is no other way.
So that a recipe also works for others, there are a few rules:
  • Every object ID comes from an input; none is written into the script.
  • Timers that are started are also stopped or replaced, so that they don't pile up.
  • The script does not depend on states it creates itself, or on other scripts.
  • Texts the user sees come from a text input, so that they can be translated.
  • Values are read in Blockly with “Get state value”, not with “value of object ID”, and in code with await getStateAsync(id), not with getState(id).val. The other way fails if the javascript instance does not subscribe to all states on start.
  • A Blockly recipe contains no blocks with free code, that is no exec and no functions with your own JavaScript. If you need code, write a recipe in JavaScript or TypeScript.
Much of this is checked automatically by the CI of the catalog as soon as the pull request is open. A maintainer looks at the rest: Does the recipe work with every device type it allows? Does it only do what the description says? Is it safe for every user, without endless loops or timers that pile up? Is it small and general enough for everyone, rather than a special case of one installation? And can the code be read? In JavaScript and TypeScript every function of the script API is allowed, so the review reads the code: no minified code, no code that is loaded or built at runtime, and commands, files or requests to other servers only where the description says why. Mention in the pull request which devices you tested with. The recipes in the catalog are published under the MIT license.
If you improve an existing recipe, raise its version. Everyone who uses it then gets the update offered as described above.

Good to know

  • Catalog: The address is in the settings of the javascript instance under Catalog of the recipes. Empty means: the official catalog. You can also enter the address of another catalog here.
  • Offline: The adapter keeps the catalog in memory for one hour and stores the version it loaded last. If the catalog cannot be reached, the editor shows that version.
  • Versions: Recipes that need a newer version of the javascript adapter are hidden. So you only see recipes in JavaScript and TypeScript from 10.7.0 on.
The detailed description is in the documentation of the adapter: Recipes: ready-made automations.

Conclusion

With recipes, a good idea from the forum becomes an automation that anyone can use with their own devices in a few steps, without programming. The result remains a normal script in the javascript adapter that you can look at and understand, and improvements to the recipe reach you as an update.
What we look forward to most are your recipes. Try them out, tell us in the forum what is missing, and if you have an automation that would help others: export it and send it as a pull request.
Stay smart!
Your ioBroker team