Is there a way to determine the duration time remaining?

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
mrcandy
Posts: 23
Joined: Sun Nov 13, 2011 4:58 pm

Is there a way to determine the duration time remaining?

Post by mrcandy »

Within a python script if I execute indigo.device.turnOn (dev, duration=xxx), is there a way to determine how much of the duration remains before the automatic 'off' command is executed?

I am getting a trigger from a motion sensor and would like to turn a light on for a pre-determined amount of time. However, if the light was already turned on then I'd like to know if there is a delayed off pending, in which case I'd reset the duration. If there's no delayed off pending then the light was previously turned on elsewhere and I just want to leave it alone.

Code: Select all

lamp = indigo.devices["TV Room Front Lights"]
if lamp.states["onOffState"] == True :
	#
	# If there is a delayed off pending, then reset the timer here, otherwise
	# don't take any action
	#
	indigo.server.log(lamp.name + " already on, ignoring motion.")
else :
	indigo.server.log("Lower motion detected, turning " + lamp.name + " on.")
	len = int(indigo.variables["motionDuration"].value)
	indigo.device.turnOn(lamp, duration=len)
I need a way to look at the lamp device (or some schedule) to see if there is a delayed off command pending.
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Is there a way to determine the duration time remaining?

Post by matt (support) »

You have to search the list of scheduled actions looking for the correct _auto_off_ element. This should work:

Code: Select all

devName = "TV Room Front Lights"
delayedOffDur = 10   # int(indigo.variables["motionDuration"].value)

delayedName = "_auto_off_" + devName
pendingDelayed = False
for scheduleId in indigo.schedules.iterkeys():
  scheduleName = indigo.schedules.getName(scheduleId)
  if scheduleName == delayedName:
    pendingDelayed = True
    break

dev = indigo.devices[devName]
if dev.onState:
  # If already turned ON, then only reset pending delayed (by
  # sending another ON with a delayed OFF) if there was an
  # existing delayed OFF. Otherwise, someone manually turned
  # the light ON and we don't want to automatically turn it
  # OFF.
  if pendingDelayed:
    indigo.server.log("Motion detected, resetting delayed off for " + dev.name)
    indigo.device.turnOn(dev, duration=delayedOffDur)
  else:
    indigo.server.log(dev.name + " already on, ignoring motion.")
else:
   indigo.server.log("Motion detected, turning " + dev.name + " ON for " + str(delayedOffDur) + " seconds")
   indigo.device.turnOn(dev, duration=delayedOffDur)
You'll want to adjust the second line to use your variable for the duration instead of the hard coded 10 seconds.
Image
mrcandy
Posts: 23
Joined: Sun Nov 13, 2011 4:58 pm

Re: Is there a way to determine the duration time remaining?

Post by mrcandy »

Works perfect, thanks for the help.
Locked

Return to “Extending Indigo with Plugins and Python”