In an earlier post I described how I am replicating the paradigm SmartThings uses for what they title Location Mode . That post used an Z-Box enumerated variable that, in turn, is used in Block Scenes to replicate functionality that used to be handled by my old SmartThings hub.
The resulting scenes work fine for me, but I had no way to change the color of the button LED on our ZEN32 Scene Controller in the living room to reflect the currently active Profile/Location_Mode. In my application, button #1 on the ZEN32 is used to switch to the Night profile before heading to bed or to switch back to Home profile if I get up early (before it is automatically changed by another time-based Block Scene).
In checking with Z-Box Support, it turns out this functionality is not (yet) available in the Block Scene Builder. They sent me some rudimentary Lua code samples demonstrating how to use hub functions available in a Lua Scene to tweak device settings.
Perfect! Except the simple case would result in four Lua Scenes
After brushing up on my dormant Lua skills and trying a few experiments, I came up with the following code…
-- Example Lua Scene: onLocationModeChange
--
--[ DECLARATIONS ]-
{
conditions = { {
isTrigger = true,
operator = "anyValue",
property = "Location_Mode",
type = "global-variable"
} },
operator = "all"
}
--[ ACTIONS ]--
-- Scene 83: "onLocationModeChange"
local colors = {
Home = 0, -- white
Evening = 2, -- green
Night = 3, -- red
Away = 1 -- blue
}
local sc = 171 -- Scene Controller Device ID
local mode = hub.getGlobalVariable("Location_Mode")
local color = colors[mode]
hub.debug("Scene83", "LED Color for "..mode.." is "..color)
-- Set LED Parameters for Scene Controller Button #1
fibaro.call(sc, "setConfiguration", 7, 1, colors[mode])
--fibaro.call(sc, "setConfiguration", 2, 1, 3) -- set Always ON
--fibaro.call(sc, "setConfiguration", 12, 1, 1) -- set Brightness
In this scene (named onLocationModeChange, number 83 on my hub), the DECLARATIONS section describes the condition which triggers the scene to execute as any change to the global variable Location_Mode. (Note that one can change this to be a change in something else–any global variable, an event, a profile change, or some change in the state of a device.) We don’t care about matching an actual value–that would require four scenes, one for each state–but just that said value has changed.
The real work happens in the ACTIONS section of the Lua Scene. Here, we grab the actual value of the Location_Mode variable and use it as an index into a table that maps the current Location_Mode value into an LED color. For the ZEN32, the colors setting values for any button are the same and I chose White for ‘Home’ (parameter value of 0), Green for ‘Evening’ (parameter value of 2), and so on. You can use the same table for any button on this device.
My ZEN32 Scene Controller in the Living Room is Device #171. Note that for devices with multiple Device IDs, you use the base ID (the switch entity, NOT the remote control entity, in the case of a ZEN32 device). The device number is assigned to a local variable called ‘sc’ in this example, so we can use it multiple times in this scene.
The example code, above, shows a hub.debug() function call to help you debug your code. When you are done, comment it out by putting two dashes before it at the beginning of the line–you really don’t need it any more.
After the setConfiguration call to set the LED color, there are two more calls presently commented out that set the LED on/off state and brightness. I decided I didn’t need them in my application, but kept them handy should I want to change either in the future.
Finally, make a few settings changes to the device configuration. Since we’re not adjusting on/off state or brightness in the Lua Scene, go to the device Parameters and set the LED on/off state (parameter 2 for button #1) to ‘Always on’ and the brightness to the value of your choice (parameter 12 for button #1).
It is also not really necessary for the scene controller LEDs to flash (does it add much of a delay? I don’t know, but no need to make the device any busier than it needs to be!) by changing parameter 23.
Note that you can mark this scene as ‘Hidden’ as it won’t normally be run manually. It just sits there, hidden in the background, quietly waiting to update your scene controller LEDs as appropriate.