We have moved at community.getvera.com

Author Topic: Simple user triggered scene to turn light on and off 3 times does not work  (Read 1322 times)

Offline stee1hed

  • Sr. Newbie
  • *
  • Posts: 33
  • Karma: +0/-0
Hi
I'm attempting to create a scene that is not triggered but can only be run when the user clicks 'run'.

I want to turn a dimmable light on and off 3 times, something that should be very easy with code, but here is what happens...

When I run it with the light starting in an 'off' or '0%' state, nothing happens.
When I run it with the light starting in an 'on' or '100%' state, the light turns off once.

Here is my code. For now I'm staying away from loops as I troubleshoot.
I return 'false' because the code did all the work and nothing more needs to be done.

Code: [Select]
local livingRoomLight = 5
local SP_SID = "urn:upnp-org:serviceId:Dimming1"
local delay = 7

function cycleLightOn()
    luup.call_action (SP_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, livingRoomLight)
end

function cycleLightOff()
    luup.call_action (SP_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "0"}, livingRoomLight)
end

luup.call_delay("cycleLightOn", delay)
luup.call_delay("cycleLightOff", delay)
luup.call_delay("cycleLightOn", delay)
luup.call_delay("cycleLightOff", delay)
luup.call_delay("cycleLightOn", delay)
luup.call_delay("cycleLightOff", delay)

return false

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
You're scheduling the callbacks roughly at the same time, so they all happen roughly after delay seconds?

You'd want to schedule the next callback from the current callback. See also (e.g.) the Blinking Christmas tree.
Dezwaved at the moment...

Offline stee1hed

  • Sr. Newbie
  • *
  • Posts: 33
  • Karma: +0/-0
Duh on me, thank you!

I thought they were synchronous when they are asynchronous

Offline stee1hed

  • Sr. Newbie
  • *
  • Posts: 33
  • Karma: +0/-0
This code works

**edit : and return it to it's original state**

Code: [Select]
local livingRoomLight = 5
local SP_SID = "urn:upnp-org:serviceId:Dimming1"
local delay = 3
local counter = 5

local existingLevel = luup.variable_get(SP_SID, "LoadLevelTarget", livingRoomLight)

function cycleLightOn()
    if counter > 0 then
        luup.call_action (SP_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, livingRoomLight)

        luup.call_delay("cycleLightOff", delay)
    else
        luup.call_action (SP_SID, "SetLoadLevelTarget", {newLoadlevelTarget = existingLevel}, livingRoomLight)
    end
end

function cycleLightOff()
    luup.call_action (SP_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "0"}, livingRoomLight)
   
    counter = counter - 1
    luup.call_delay("cycleLightOn", delay)
end

luup.call_delay("cycleLightOn", delay)

return false
« Last Edit: July 18, 2012, 03:34:31 pm by stee1hed »