After exploring openHAB, I’m excited to transition my existing LUA scripts to Xtend. This journey has been a bit challenging, especially with configurations and loops. In LUA, I used tables like this:
lua
local twilightDevices = {
[‘Civil Twilight’] = {enabled = true, sensorType = ‘civil_twilight’},
[‘Nautical Twilight’] = {enabled = true, sensorType = ‘nautical_twilight’},
[‘Astronomical Twilight’] = {enabled = true, sensorType = ‘astronomical_twilight’}
}
for dDev, devConfig in pairs(twilightDevices) do
if devConfig.enabled then
– Execute actions here
end
end
In Xtend, I’m trying to replicate this functionality. After some research, I found that using a Map structure and iterating over it with a for-each loop achieves the same result. Here’s how I approached it:
- Data Structure Initialization: I created a
Mapto store device configurations. - Loop Implementation: I used a
for-eachloop to iterate over theMapentries. - Condition Check: I implemented a condition to check if a device is enabled before executing actions.
Here’s an example of how it looks in Xtend:
xtend
val twilightDevices = new LinkedHashMap<String, Map<String, String>>()
twilightDevices.put(‘Civil Twilight’, new HashMap<String, String>() {{
put(‘enabled’, ‘true’)
put(‘sensorType’, ‘civil_twilight’)
}})
twilightDevices.put(‘Nautical Twilight’, new HashMap<String, String>() {{
put(‘enabled’, ‘true’)
put(‘sensorType’, ‘nautical_twilight’)
}})
twilightDevices.put(‘Astronomical Twilight’, new HashMap<String, String>() {{
put(‘enabled’, ‘true’)
put(‘sensorType’, ‘astronomical_twilight’)
}})
for (Map.Entry<String, Map<String, String>> entry : twilightDevices.entrySet()) {
val devConfig = entry.getValue()
if (devConfig.get(‘enabled’).equals(‘true’)) {
// Execute actions here
}
}
This approach has been working well for me. I’m gradually getting the hang of Xtend’s syntax and structure. If anyone has tips or alternative methods, I’d love to hear them! The openHAB community is amazing, and I’m grateful for the support.
Cheers! ![]()