Author Topic: Blink hue lights and revert to prior light status  (Read 810 times)

Offline shillingcl

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Blink hue lights and revert to prior light status
« on: December 10, 2017, 02:42:37 pm »
I've been looking for an easy way to Vera to blink my hue lights with a specific color when driveway motion is detected.  Blinking the lights is fairly straightforward, but reverting them back to the prior light color and brightness is more difficult.  Some others may have suggestions for simplified ways to do this, but this was the best I could cobble together based on the limited information I could find.

Code: [Select]
-- Blink hue lights and revert to prior light settings
local blinkColorHue = "44000"
local blinkColorSat = "254"
local blinkCount = 4
-- Insert the device id's of your hue lights below to replace the 271 and 272.  Add more with additional commas
local lightIDs = {271, 272}

--Capture current light levels and color for hue lights
local curLightStatus = { }
for i, lightID in ipairs(lightIDs) do   
    local curDimLevel = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus", lightID)
    local curLampValuesString = luup.variable_get("urn:micasaverde-com:serviceId:PhilipsHue1","LampValues", lightID)
    local curLampValues = {}
        for k, v in string.gmatch(curLampValuesString, "(%w+):(%w+)") do
            curLampValues[k] = v
        end   
    local curHue = curLampValues["hue"]
    local curSat = curLampValues["sat"]
    curLightStatus[lightID] = {curDimLevel, curHue, curSat}
end

--Set lights to blink color and sat
for i, lightID in ipairs(lightIDs) do 
    luup.call_action("urn:micasaverde-com:serviceId:PhilipsHue1", "SetHueAndSaturation", {Hue=blinkColorHue ;Saturation=blinkColorSat ;Effect=""}, lightID)
    luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, lightID)
end
luup.sleep(900)

--Perform set number of blinks
for bc=1, blinkCount do
    for i, lightID in ipairs(lightIDs) do
        luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "0"}, lightID)
    end
luup.sleep(900)
    for i, lightID in ipairs(lightIDs) do
        luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, lightID)
    end
luup.sleep(900)
end

--Revert lights back to prior settings
for i, lightID in ipairs(lightIDs) do
    luup.call_action("urn:micasaverde-com:serviceId:PhilipsHue1", "SetHueAndSaturation", {Hue=curLightStatus[lightID][2];Saturation=curLightStatus[lightID][3];Effect=""}, lightID)
    luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = curLightStatus[lightID][1]}, lightID)
end

Hope this helps some others

Offline randya

  • Sr. Newbie
  • *
  • Posts: 27
  • Karma: +4/-1
Re: Blink hue lights and revert to prior light status
« Reply #1 on: August 02, 2018, 10:16:33 pm »
Nice code, shillingcl.
I am trying to do a similar thing because it is convenient to blink lights for events instead of sound (doorbell) if the TV or Music is on. 
I haven't gotten to the point of saving the state before the event but was thinking that would be needed.  So just starting simple here and I was surprised I am not getting the results I expected.  When putting my code in the scene's lua code section, it doesn't work from the motion detected event.  But if I manually trigger the scene it works.  Here is my simple code:
Code: [Select]
luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {["newTargetValue"] = "1"}, 25)
luup.call_action("urn:micasaverde-com:serviceId:Color1", "SetColorRGB", {["newColorRGBTarget"] = "255,0,0"}, 25)
luup.sleep(1500)
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {["newLoadlevelTarget"] = "0"}, 25)
luup.sleep(1500)
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {["newLoadlevelTarget"] = "100"}, 25)
luup.sleep(1500)
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {["newLoadlevelTarget"] = "0"}, 25)
luup.sleep(1500)
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {["newLoadlevelTarget"] = "100"}, 25)

I have tried variations on the order of luup calls, different sleep values, different load level values.  It seems when the scene is called from a motion event, it runs the first part, then jumps to the end.  Again, if I trigger manually it works.
Does anyone know if there is some kind of time delay because of the motion event?  or what other thing I did wrong here?

The motion sensor is a new AEOTEC multi 6 in 1, and the light is an AEOTEC LED Strip. 


Offline RichieB

  • Full Member
  • ***
  • Posts: 122
  • Karma: +1/-0
Re: Blink hue lights and revert to prior light status
« Reply #2 on: August 04, 2018, 03:39:51 am »
yeah there is something wrong with the trigger what is the state of the trigger?

here is my code - so when my projector is on it only brighter 2 lights when motion is detected then dims when there is no motion in the kitchen but when the projector is off it brightens all 4 for the period selected

*****FYI I found using a scene on the Philips hue is much more reliable than calling light individually 

Code: [Select]
dofile("/root/SceneScripts/Devices.lua")

local ProjectorState= luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", Projector) -- Get Projector State
local Dimmlvl= luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelLast", HueKitchen2)
local SwitchState= luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", KitchenLights) -- Get Get Kitchen Light State

local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID
local SD_SID = "urn:upnp-org:serviceId:Dimming1" -- Switch Dimming Service ID
local SP_SID = "urn:upnp-org:serviceId:SwitchPower1" -- Switch Power Service ID
local PH_SID = "urn:upnp-org:serviceId:althue1"
local period = 130 -- Seconds
local DefDimmlvl = 3 -- Default Dimming Load Level
local Dimthresh = 30 -- Dimming Threshhold
-- Scene Id
local Kitchenbrightall = "N0uJiME6DcilNln"
local Kitchendimall = "hNQMaFECpN0Gu9v"
local Kitchenmoviebright = "Iht1YQUJziXbdg9"

if (tonumber (Dimmlvl) >= tonumber (Dimthresh) ) then --Checks the dimming level
    Dimmlvl = DefDimmlvl
luup.log("Dim load Level > Dimming Threshold")
end

if (SwitchState ~= "1") then -- Check Projector State
luup.log("Turn on Lights")
luup.call_action(SP_SID, "SetTarget", {["newTargetValue"] = 1}, KitchenLights) -- Brighten Turn On Kitchen Lights
end

if (ProjectorState == "1") then -- Check Projector State
luup.log("run Scene to brighten back 2 lights to 60")
luup.call_action(PH_SID, "RunHueScene", {hueSceneID= Kitchenmoviebright }, BurnsedPl)
luup.log("Kitchen number 2 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="60"}, HueKitchen2) -- Brighten Kitchen Number 2
  luup.log("Kitchen number 4 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="60"}, HueKitchen4) -- Brighten Kitchen Number 4
else
luup.log("Projector is off Brighten All Lights") -- Projector off
luup.log("run Scene to brighten all lights")
luup.call_action(PH_SID, "RunHueScene", {hueSceneID= Kitchenbrightall }, BurnsedPl) -- Brighten Scene
luup.log("Kitchen number 1 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="100"}, HueKitchen1) -- Brighten Kitchen Number 1
  luup.log("Kitchen number 2 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="100"}, HueKitchen2) -- Brighten Kitchen Number 2
luup.log("Kitchen number 3 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="100"}, HueKitchen3) -- Brighten Kitchen Number 3
  luup.log("Kitchen number 4 load leve change")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget="100"}, HueKitchen4) -- Brighten Kitchen Number 4

end

function checkLastTrip()
    local lastTripmotion = luup.variable_get (SS_SID, "LastTrip", KitchenMotion) --or os.time()
local Tripstate = luup.variable_get (SS_SID, "Tripped", KitchenMotion)
local Ostime = os.time()
local osdifftime = os.difftime (tonumber (os.time()), tonumber (lastTripmotion))
luup.log("Os Time = " .. Ostime .. ", LastTrip = " .. lastTripmotion .. ", Diff = " .. osdifftime .. ", Trip state = " .. Tripstate .. ".")
if (Tripstate == "0") then


luup.log("run Scene to dimm lights")
luup.call_action(PH_SID, "RunHueScene", {hueSceneID= Kitchendimall }, BurnsedPl) -- Brighten Scene
luup.log("Re-dimm the light Scene")
        luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen1) -- Re-dimm the light.
luup.log("Re-dimm the light.")
        luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen1) -- Re-dimm the light.
luup.log("Re-dimm the light.")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen2) -- Re-dimm the light.
luup.log("Re-dimm the light.")
        luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen3) -- Re-dimm the light.
luup.log("Re-dimm the light.")
luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen4) -- Re-dimm the light.
    else
        luup.call_delay ("checkLastTrip", period) -- Check when the sensor was last tripped every <period> seconds.
    end
end
 
luup.call_delay ("checkLastTrip", period)
 
return true


  i have both alt hue and Regular phips hue installed


I modified my motion sensor to timeout after 1 min
maybe that will help you


this works religously for me ..
« Last Edit: August 04, 2018, 04:02:27 am by RichieB »

Offline randya

  • Sr. Newbie
  • *
  • Posts: 27
  • Karma: +4/-1
Re: Blink hue lights and revert to prior light status
« Reply #3 on: August 05, 2018, 10:59:07 pm »
Thanks, RichieB.
I have looked over your code (looks nice) but can't seem to find anything that changes the behavior I am seeing.  I even tried the "return true".  So basically, I am using UI7 and setup a scene to trigger with the motion sensor detects movement.  I then am using the "advanced editor" to place my code.  The trigger seems to work and the led strip comes on and even sets the color to red.  But it does not flash the leds (dim 0, 100, 0, 100).  If I trigger the scene manually the leds flash as expected.  So I am confused on why the motion sensor can trigger the job but the code not execute correctly.  And just playing around, I had a line in the code before to change the leds to green and in that scenario, the motion sensors would trigger the light to come on, do nothing, then go green.... no blinking.  So i don't understand what is stopping the middle portion of the code from executing but that beginning and apparently last part works.  I am curious to understand this problem using a scene and the advanced editor but if there is another way to do it I would like to know where to put the code. 

Offline randya

  • Sr. Newbie
  • *
  • Posts: 27
  • Karma: +4/-1
Re: Blink hue lights and revert to prior light status
« Reply #4 on: August 05, 2018, 11:17:11 pm »
ok... this is very interesting...
I threw the "blinking" part of the code into a function (idea from RichieB's code) and it seems to work A LOT BETTER.  Reading the luup document suggests doing that and calling "call_delay" uses its own thread.   So something on the motion trigger event calling a scene is blocking but when manually trigger not blocking (???).   Anyway, I am getting better results and my lights blink in a glowing fashion as I wanted now.  I will just have to put all my scene code that is going through the advanced editor into a function and call it with call_delay. 

So now to use the other code samples and save state and color of the leds before blinking and return to same state after flashing red  :)