Page 1 of 1

Python and Scenes

PostPosted: Mon Jul 01, 2019 9:52 am
by Mark
I can find this:
https://wiki.indigodomo.com/doku.php?id ... numeration

But I've yet to learn how to use that info to built a script (is there a tutorial for that?).

How does one turn on/off a scene within a Python script?

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

Scratch that...

I looked for 1/2 hour before I posted, then stumbled on it two minutes after I did!!

indigo.insteon.sendSceneOn(11, sendCleanUps=False)

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

OK, don't mind me, I'm just talking to myself. Since I figured out "Part A" of my Python script, maybe someone in-the-know could help me with "Part B."

In my Action Group "TV Time", which sets up my AV gear for TV viewing, I also want the room lighting set, but only after dark. I can get that to work with the "isDaylight" variable, but what I really want is for lights to come on if it's 1/2 before sunset. I have these:

#indigo.server.calculateSunset()
#indigo.server.getTime()

And I know there is something about "delta" I can use, but how to put it all together escapes me.

So basically, what is Python for:

Code: Select all
if sunsetTime - currentTime  < 30 minutes:
     indigo.insteon.sendSceneOn(sceneNumber, sendCleanUps=True)

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 10:39 am
by jay (support)
This should be close:

Code: Select all
from datetime import timedelta
target_time = indigo.server.calculateSunset() - timedelta(minutes=30)
if indigo.server.getTime() > target_time:
    indigo.insteon.sendSceneOn(sceneNumber, sendCleanUps=True)

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 10:52 am
by Mark
Awesome, thanks Jay!

This is what I came up with while I was waiting for a reply:

Code: Select all
import datetime

tSunset = indigo.server.calculateSunset()
tCurrentTime = indigo.server.getTime()
tDelta = tSunset - tCurrentTime
tMinutes = tDelta.total_seconds() / 60

if tMinutes < 30:
   indigo.insteon.sendSceneOn(sceneNumber, sendCleanUps=True)


Seems to be about the same thing... though I like yours better.

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 10:58 am
by Mark
As I continue to learn Python...

I noticed you wrote:
Code: Select all
from datetime import timedelta

Is it more efficient that way, as opposed to:
Code: Select all
import datetime

In my code, could I have (should I have) used:
Code: Select all
from datetime import total_seconds

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 11:53 am
by jay (support)
Mark wrote:
In my code, could I have (should I have) used:
Code: Select all
from datetime import total_seconds


No, because total_seconds() is a method defined in the timedelta object (which is what was returned by the subtraction of the two datetime objects returned by the indigo.server calls). So in this instance you can't import total_seconds because it's not a general function of the datetime module but rather a method (member function) of the timedelta object defined in the datetime module.

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 12:02 pm
by Mark
Yikes. OK, thanks. I've got some studying to do... :)

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 1:56 pm
by jay (support)
Lots of beginning Python tutorials out on the net - we highly recommend you work through one if you're going to be writing scripts. Python version 2.7.

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 3:44 pm
by Mark
Got any suggested links? I’m cruising some of them already, but if there’s one you like better than others...

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 5:29 pm
by jay (support)
They're all pretty much the same I think.

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 8:45 pm
by matt (support)
In this particular case I would just create a new variable in Indigo, sunsetWith30MinDelta, and then create two Schedules in Indigo to set it to True (30 mins before sunset; the Schedules UI has an offset field you can use) and False at sunrise. You can then just reference that variable from Python. Also makes tweaking it easier since you can do so in the Schedule UI, and you can reuse that variable in other places if needed.

Re: Python and Scenes

PostPosted: Mon Jul 01, 2019 9:17 pm
by Mark
Thanks Matt. Good idea!

While searching for a solution I found another guy's post asking how he could code for both sunset-minus-delta AND cloudy weather to determine when it's dark enough outside to light something up, which is really what I'm after. I'm trying to teach my house when it's dark enough in my living room to turn on some lights. That variable (I'd call it dark_time, or something like that) could be manipulated by the sunset/sunrise times, and cloudy skies.

Just thinkin' out loud... :)

Re: Python and Scenes

PostPosted: Tue Jul 02, 2019 12:13 am
by howartp
Also look at the Group Trigger and Group Listener plugins; they might eliminate need for Python altogether.


Sent from my iPhone using Tapatalk Pro

Re: Python and Scenes

PostPosted: Tue Jul 02, 2019 10:24 am
by Mark
Well, last night my "TV Time" Action Group didn't work. No lights came on after sunset. :(

I didn't bother to troubleshoot why. I've ditched the Python "time math" and tonight I'll try Matt's idea. I named my variable "isDark" and setup the two schedules: false at sunrise, and true at 30 minutes before sunset. I originally liked the idea of nice-neat Python and no extra schedules or variables, but Matt's idea has the advantage of being more understandable/reliable, and as he pointed out, I may be able to make use of an isDark state for more than just this.

If this works out, I may look into a third schedule, one that checks the cloud cover an hour or two before sunset, and sets my variable "isDark" to true if it's gloomy out. But one step at a time...

Ooh, I just thought of another Action that can make use of "isDark" instead of "not isDaylight". I'm off to reprogram that Action!! :D

Re: Python and Scenes

PostPosted: Sat Nov 16, 2019 12:41 am
by lazlohollyfeld
UPDATE: that was fast, i solved it by forcing it to an int:
Code: Select all
indigo.insteon.sendSceneOn(int(desiredScene.value), sendCleanUps=True)


Is it possible to set the scene from a variable?
tried below code, but I get this error:
Script Error embedded script: specified powerlinc scene not found

Code: Select all
currentScene = indigo.variables[1233943159] #OfficeCurrentScene
# 16
desiredScene = indigo.variables[705613619] #OfficeDesiredScene
# 17

   if (currentScene.value != desiredScene.value):
      indigo.variable.updateValue(indigo.variables[1233943159], value= indigo.variables[705613619].value)
      indigo.insteon.sendSceneOn(desiredScene.value, sendCleanUps=True)

Re: Python and Scenes

PostPosted: Sat Nov 16, 2019 11:37 am
by jay (support)
Right - variable values are always strings, though you can attempt to get them in any type you want:

Code: Select all
currentSceneNumber = indigo.variables[1233943159].getValue(int)