No Global Variables? Time to use Node-Red

So I love OpenHab, but currently, I am forced to only use it as a hue and zwave device server, because any heavy scripting of the whole automation system is pretty much crippled by the lack of a system-wide variables system. Alternatives to using variables, such as creating dummy/fake items just to hold a variable is a cheesy, cumbersome workaround at best. It would not be that difficult to implement a system-wide variables system. That being said, there is a WHOLE LOT of things you simply can not do with your home automation without access to system accessible values across multiple rules and scripts. That being said, for those of you who have more advanced needs, such as myself, I would highly recommend converting all of your rules and scripts over to Node-Red and use it as your rules engine. You can continue to use OpenHab as the hub for all of your actual devices, and simply use the Node-Red OpenHab2 nodes to read the event stream and react to everything that is going on and send commands back to devices. Node-Red also allows for a more visual means of seeing your rules and automation and has access to far more 3rd party features and services with the addition of THOUSANDS of nodes that can be installed to accomplish many, many things that are not yet possible in OpenHab.

Just the personal opinion of one man in need of more than OpenHab alone is able to accomplish!

Can you provide a concrete example of something that can’t be done without global variables?

I’m not challenging your opinion, I’m just curious. I haven’t felt limited at all by the rules engine, so I suspect that I don’t fit into your definition of having advanced needs (and I’m okay with that).

I think it would be valuable for us to articulate where the breakpoint is, so that new users can determine which direction they should go.

Why? Who cares what a variable is called, just so long as it accomplishes the goal?
I’m not disagreeing with you, but if the architecture works, who cares what stuff is called

I think node red looks really cool, can you tell me more about your set? do you run a server?

1 Like

Lots of users use Node Red. It’s a good system that has good OH integration.

I am an advanced user and I’ve not encountered a situation where I’ve needed a global variable. Honestly, I don’t even have that many dummy Items (I hate that term, there is nothing dummy or fake about these Items, they are just Items not linked to a device). As Russell asked, I too would be interested to understand more about the nature of the problem. Nothing can ever get fixed without more detail.

I find that most users that have this sort of experience with the Rules DSL have some experience with coding before coming to OH. To put it bluntly, you have to be willing to bend to the Rules DSL or you should use something else. Rules DSL will not bend to the way you want to do something. If you bend, I find it a relatively nice language to code Rules in. If you try to make it work like languages you are used to (though I’m not sure global variables are all that well supported in any modern language to be hionest) you will be unhappy.

Luckily there are at least half a dozen other options for you. If you want to stay within the OH ecosystem there’s JSR223 Rules which does support global variables I believe, at least in-so-far as Jython, JavaScript, or Groovy support them. There is NodeRed of course. @Spaceman_Spiff has developed his own Rules engine. I know there are plenty more options.

Depending on what you need the global variables for, you have lots of options in Rules DSL as well.

There are lots of options available. Each one is more or less appropriate based on what you want to use the global variable for.

I’m not trying to convince you or anyone not to use NodeRed. But I don’t want to leave statements like “no global vars” hanging out there for future readers to assume that there is no support for what they might want to do at all.

3 Likes

With JSR223, you can also create ScriptExtensions to hold objects, which can then even be accessed in scripts using another language (e.g. pass a variable from Python to JS).

There ARE global variables in the rules DSL. But they are ony accessible by rules in the same .rule file, so just put all of your rules in one file.

1 Like

Having pored over the topics, I find myself here wondering about global variables.

I am wanting to store all my api keys and passwords in a single place such that I can obfuscate them as much as possible. I believe home assistant has a ‘secrets’ file that pretty much allows for this.

Is there any equivalent in openhab?

For many of these items I could just create a bespoke items file and create string items to hold that information, but I think I would have issues with cfg files with this approach.

Any ideas are welcome. Apologies if this has been covered elsewhere, if so, please be kind and point m in the right direction.

Many thanks…

If you use the new rule engine, scripted automation, and the helper libraries, there is a module reserved for this sort of information. It also makes it easier to share scripts and modules, since your personal data is kept inside the configuration.py.

It depends on where you need to use the secrets. If it’s in Rules, see Scott’s reply. For Rules DSL, you can use a .map file and the transform Action to select a secret by name in the rule. There are other approaches that work as well but the transform is the least amount of work for Rules DSL.

If you need them in Thing configurations and the like there is currently no way to do it.

Thank you Scott and Rich, I shall look into both of those options.

All a steep learning curve for me, but your help is very much appreciated.

HABApp brings support for parameter files.
These files can be used to define values and changes are picked up on the fly. It’s perfect for things like credentials and boundaries.

Thanks, that looks interesting.