(Advanced) Toggle a Device’s state

Posted: one year ago
For this example we have used a light switch. However you could replace the If statement with the states of any device. For example, to dim a dimmer controller you could use:
  luup.call_action("urn:upnp-org:serviceId:Dimming1","SetLoadLevelTarget",{ newLoadlevelTarget="0" },device)
1)  Find the device you want to toggle, and open its configuration
    a.  Click “Advanced” and write down the device’s ID
2)  Go to the Scenes section on the left hand navigation and click “Add Scene”
3)  Under "Step 1" create a scene as a "Manual Trigger"
4)  Skip "Step 2: Device Actions"
5)  Under "Step 3" go to "Also, execute the following Luup code:" > “No Luup Code defined”
6)  Copy and paste the following code and replace XX with your device  #ID (the example covers a light, remove the "OPT" lines for other devices since they cover the light's brightness level)

-----Copy------
local device  = XX -- device #ID via Device > Advanced
local lgtsts = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device)
if(lgtsts=="1") then -- If Device is On
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget", {newTargetValue = "0"}, device) -- Device Off
else
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget", {newTargetValue = "1"}, device) -- Device On
end

-----------------

7)  Click "Save Lua," then name your scene (i.e. Lounge Light Toggle) and click "Finish"
8)  You can test it by click the "Run" arrow icon and hopefully the light toggles on or off depending on it's previous state.


Posted: one year ago
Another way to control multiple lights would be to use this code:

---Copy---
local dID = XXX               -- Device ID of your master Switch
local slaves = {121,122,123}  -- Device IDs of slave switches
local status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",dID)
for i=1,#slaves do
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue=status},slaves[i])
end

-----------