Javascripting addon

As far as i know it’s not possible to create things using openhab-js library you can only get existing things
openhab-js
but still i want to auto-create things base on my js rules / items - the idea is to create a thing file using .js but not sure how import fs module to be able to use
writeFile() method ?

ok got it
.js:

FileWriter = Java.type('java.io.FileWriter');
FileWriter = new FileWriter("/etc/openhab/automation/js/filewriter.txt");

FileWriter.write("data 1");

FileWriter.close();

This is a very odd use case. Why do you want to create Things from rules? Items can make sense, especially for rule templates, but Things represent devices. Usually the devices drive the rules, not the other way around.

This smells of “if the only tool you have is a hammer, all problems look like nails.”

to speed up and make no mistake by manually creating thinkgs/linking channels etc. a bit corner case probably but if i want to create 12 thermostates by hand, 200 items, i need to manually do it in thigns/items, rules, and sitemaps. i can do it right new with a single jss script

1 Like

I currently do this using JRuby with more things and about 900 items. Instead of writing to .things and .items file like my approach in the past, JRuby can create the things and items directly into openhab core runtime.

The benefit is a lot easier maintenance, addition, removal and modifications of devices which translate to things and items, because many devices are similar, e.g I have about 20 esphome wall switches all sharing similar structure of things and items, zigbee2mqtt based 10 motion sensors that are exactly the same, etc.

It is significantly easier for me to maintain them using a series of templates, one for each type of device. Even though it took some effort to initially create the framework to do so.

I don’t know how other people cope with having hundreds of items without tedious work. I guess they rely on auto discovery for things. Then creating 900+ items out of those? I really don’t know how. How many tedious clicks would be involved? Then if something needs to change, eg I want to add a tag to all 50 of my light switch items, do they go through, search the items, click to go into it, click to add tag, click out, back to the list, find the next item, click back in, click edit, add the same tag, and so on 50 times?

With my system, it’s as simple as a few edits of either the device list or the device template (depending on whether it’s a change to select items or a universal change to all items of the same device type) and all the relevant items will get the new tag. To add one tag, I’ll need to type in the tag once for all 50 switches.

A lot of my things are Mqtt and the channels are set up manually, not via discovery. I much prefer it this way.

Just chiming in to show that @Jacek_Kaczmarczyk isn’t alone in wanting to do this. I’ve been doing it this way since openhab 2 I think.

So it just a one off role you run once? It still seems odd to use a rule for that instead of a script, the console, .things files, etc.

I don’t question doing this programatically. I question doing it with a rule.

The rule runs on openhab startup to load my yaml device definition, and also whenever the device definition changes, so it triggers also on file changes.

It is basically an alternative version of the core .things / .items provider.