Limits of scripting

Hi everyone,
I’m approaching now the openhab3 plattform and am currently trying to understand what I can and cannot do via scripting in the automation rules.

I’m a software developer so I’m not new at programming.

As said before I would like to understand the limits of the in-platform scripting (like Ecmascripting).
My goal is to have a central processing unit that is executed constantly (or periodically) and, given the items status, takes decisions and sends commands to other items to entirely controll my smarthome.

The following questions arose after using a bit the in-built Ecmascripting in Openhab:

  • Can I save data in the filesystem (like an array, a JSON-like structure or something like that) from within the script?
    Example:

    FileManager.saveCsv(dataStructure.toCsv(), “file.csv”);
  • Can I import any java/javascript library into my scripts? (for example to run a machine learning algorithm)
  • Can I open a socket or connection to an extarnal server to exchange data?
  • Can I connect to a local or remote database and execute queries (select and insert)?
  • How much can my script interact with openhab? Is there API’s documentation that lists all available functions for this pourpose?
    (I mean, what can I do other than managing items?)

If this is too much for the scripting pourposes, how should I procede? Working directly on Openhab code seems too much, but maybe I could create my own addon and integrate it with Maven as described in the documentation.

Any answer partially or fully covering my questions is immensely appreciated.

Thank you!

P.S. At the moment my OH installation is running in a Docker container on a raspberry Pi.

As you come to gain experience with openHAB, you’ll find that it works best as an events driven system. For some with previous programming, it can be hard to get into the mindset at first.
But it boils down to -
“This happened, if X is true do That”
as a style of working. As opposed to keep looking to see if This is happening.

openHAB provides a choice of relatively simplistic database services, which serve most home needs - charting, averaging, accumulating. It would make a rubbish airline booking system on it’s own, but you can always add scripts for exotic queries.

1 Like

The JavaScript is used for rules. Rules are short (usually couple of seconds) actions that are executed when e.g. an item changes its state.
But if I read your thread it seems as if you want to to a bit more than that - maybe something like a closed loop in combination with machine learning?

I believe there are better systems/software for this purpose and you can use openHAB’s REST API to get item information from openhab and send data to other items.
So openHAB itself provides the data but some complex control/learning algorithms are running elsewhere.

1 Like

This is somewhat of an antipattern for openHAB. openHAB’s rules are event driven. An event occurs and a rule (or rules) will run to do something in response to that event.

You could code it the way you describe but I suspect you’ll be frustrated with it and over all the code will be way longer and more complex than it needs to be.

Yes but…

openHAB mostly deals with it’s own internal state so it doesn’t really provide mechanisms to easily read from and write to files. In JavaScript you’d probably need to resort to using the the java.io classes from Java (all the scripting is embedded in Java so all of standard Java is available to you). If you used Python, there is more stuff built into the language for reading and writing files.

Java, no. You can import stuff from openHAB and core Java. Theoretically you can load JavaScript libraries but the JavaScript is version 5.1 and Nashorn (the embedded JavaScript in Java) really only supports loading them which can cause all sorts of problems.

Work is going on to let us use GraalVM JavaScript as a replacement which will support libraries in a much better way but that’s some time off.

With Python the situation is a little bit better but even there it doesn’t handle making sure all the dependencies are installed so we can to install the manually. Again, this should become better when we get Graal Python support. Note that Python is provided using Jython which is version 2.7, not Python 3, which can cause some problems with library availability.

It’s worth noting the existing of HABApp which is a separately running rules engine built in pure Python 3. But because it is separate it can only do what is exposed through the openHAB REST API.

You could but the event driven nature of the rules will make it somewhat awkward as you’d have to open the socket each time the rule runs. Also you won’t be able to listen for and accept connections. Again you’d probably use the core Java libraries to implement it. Finally, this isn’t what rules are for, that’s something that bindings do. I believe a new TCP/UDP binding is in work for OH 3.

openHAB supports database connectivity via it’s Persistence service. But it does so in a highly abstracted way. It does not directly expose the interactions with the database and instead it works only on Item states and it provides a small set of query methods (averageSince, lastUpdate, previousState, etc.).

If you want to access non-openHAB data from a database you are likely out of luck. You’ll have to write something separate to push the data into openHAB. If you want to perform more complex queries of the database here too you may have to do that outside of openHAB.

However, note that openHAB does have bindings and actions to make HTTP requests and execute command line commands. So it may not be that far outside of openHAB.

Anything can can be done through any of openHAB’s UIs can be done through it’s REST API. OH 3 comes with interactive documentation for the REST API which you will find under Developer Tools. In OH 2.5 you’ll have to install the docs as a separate addon.

The list of stuff that can be done it too long to list here but in general the creation/removal/configuration of pretty much anything can be done through HTTP queries. For events openHAB provides an SSE stream.

Over all it kind of sounds like you already have an overall architecture for how you want to build your home automation in mind, and that approach is in some ways antithetical to how openHAB is designed to work. I recommend downloading and starting to play with it. Go through the Getting Started tutorial (starts here and continues here) and I think you will find that everything you want to do is possible, but it might be better/easier to do it in a different way than you have envisioned.

1 Like