Global Variables for IP-Addresses possible?

Hi guys,
I am new to the openHAB community and absolutely amazed how fast one can see first results.

Doing my first steps in VS w/ openHAB plugin. Is there a way to store variables globally, e.g., the hue bridge’s IP or user name?

If globally is not possible than I’m sure there must be the possibility of local vars, right?
Will

var ip_hue_bridge = "192.168.0.15"

work?

I hate magic numbers, and filling in an IP-adress de-centrally a multitude of times would be considered terrible coding practice in other environments:

hueDimmerSwitch.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueDimmerSwitch.updateInterval=5000

hueMotionSensorTemperature.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorTemperature.updateInterval=60000

hueMotionSensorPresence.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorPresence.updateInterval=5000

hueMotionSensorIlluminance.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorIlluminance.updateInterval=15000

Edit: And how would I mesh up variables and URL-Strings? Intuition would command:

"http://"+ ip_hue_bridge+"/api/"+ id_hue_username + "/sensors/" + id_hue_sensor_kitchen

I’m sure that’s not how it works here, right? :wink:

Thanks a bunch and cheers,
Chris

https://docs.openhab.org/configuration/rules-dsl.html#the-syntax

A rule file is a text file with the following structure:

  • Imports
  • Variable Declarations
  • Rules

    The Variable Declarations section can be used to declare variables that should be accessible to all rules in this file. You can declare variables with or without initial values and modifiable or read-only. For further details, please see the Xtend documentation for variable declarations.

So inside of Rules files you can create a variable that is global to that .rules file.

However, you appear to be refrencing a config file. There is no way to define a variable that can be reused in a config file. This isn’t all that unusual in many environments. Config files are, after all, where the magic numbers get defined. But I believe there are some issues being worked to add some features that might make something like this possible.

That being said, EVERY network has a way to put an IP address into a variable: DNS. As my personal system has grown I cannot express how important static IPs and DNS has become not just to my home automation but all my other services.

Hi Rich,
I’m sorry I lost track of this thread. Thank you for your response!
Have you set up an internal DNS in your local network? I have googled a bit but can’t find an easy solution (maybe I’m stupid).
Can you point me to where those issues re config files are being discussed?

Yes. I had a DD-WRT wifi router that included a local DNS server, though come to think of it I think the original firmware for the router also had DNS.

Now I run a pfSense firewall between the Internet and my network and have moved the DHCP and DNS to that.

But if you don’t have any of these then you can run BIND and configure your DHCP server to use the machine where you run BIND as one of your DNS servers to query.

Search Google for BIND tutorial and you will find more than you need to get started.

Or you can do the poor man’s approach and edit the hosts file on all of your machines, or at least the relevant machines (just the OH server perhaps?) with the mapping between hostname and IP address.

Many Linux and Windows machines will advertise their hostname to the DHCP server which means you may not have to do anything. If you can ping a host by name then you can use that name in the Oh configs.