Migrating my LUA scripts to Xtend

Hello there!

After trying out openHAB for a few days I must say I had no idea what I’ve been missing until now. Realizing that, I’m slowly getting aware about that migrating all my LUA scripts that I had in another HA system will take me more than a few days. It’s quite slow in the beginning since every small step takes like ages. Even a simple IF statement is a challenge now, but I’m sure it will improve.

I’d like to take the opportunity to ask you guys in case you’d like to help me a bit.

In LUA I used to create tables for configuration data as this example:

twilightDevices = {
	['Civil Twilight'] = {['enabled'] = true, ['sensorType'] = 'civil_twilight'},
	['Nautical Twilight'] = {['enabled'] = true, ['sensorType'] = 'nautical_twilight'},
	['Astronomical Twilight'] = {['enabled'] = true, ['sensorType'] = 'astronomical_twilight'}

It’s just an example, I’m sure you get my point.

Anyway, then I could easily loop through my table in the following manner:

		for dDev, devConfig in pairs(twilightDevices) do
			if devConfig.enabled then
				-- Do something here

It’s just a simple key value pair Map. I have actually tried to figure out how to do the same thing in Xtend but I haven’t found much useful examples of this. Maybe you have an idea and like to share how this is best done in Xtend?


Take a look at immutable maps, my example here:

Create a class:
with your properties. Then use them in your map:

Map<String, YOURCLASSHERE> = ImmutableMap.<String, YOURCLASSHERE>builder()

You can use Map itself but I prefer ImmutableMap since it has a build method so you can easily instantiate the member.

You can also create a quick LUA to JSON parser, not necessarily in openHAB, but in programming lang you like. As I see on your config above, this “text” can be easily converted to valid JSON format just by using regex. You can then parse that JSON formatted text in openHAB. Better yet, just copy paste that whole config text, use js transformation or regex in rules to convert it to JSON, then use JSONPATH to get values.