Python and Scenes

Posted on
Mon Jul 01, 2019 9:52 am
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Python and Scenes

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)

Posted on
Mon Jul 01, 2019 10:39 am
jay (support) offline
Site Admin
User avatar
Posts: 15467
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python and Scenes

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)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 01, 2019 10:52 am
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

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.

Posted on
Mon Jul 01, 2019 10:58 am
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

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

Posted on
Mon Jul 01, 2019 11:53 am
jay (support) offline
Site Admin
User avatar
Posts: 15467
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python and Scenes

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 01, 2019 12:02 pm
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

Yikes. OK, thanks. I've got some studying to do... :)

Posted on
Mon Jul 01, 2019 1:56 pm
jay (support) offline
Site Admin
User avatar
Posts: 15467
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python and Scenes

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 01, 2019 3:44 pm
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

Got any suggested links? I’m cruising some of them already, but if there’s one you like better than others...

Posted on
Mon Jul 01, 2019 5:29 pm
jay (support) offline
Site Admin
User avatar
Posts: 15467
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python and Scenes

They're all pretty much the same I think.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 01, 2019 8:45 pm
matt (support) offline
Site Admin
User avatar
Posts: 19129
Joined: Jan 27, 2003
Location: Texas

Re: Python and Scenes

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.

Image

Posted on
Mon Jul 01, 2019 9:17 pm
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

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... :)

Posted on
Tue Jul 02, 2019 12:13 am
howartp offline
Posts: 3592
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Python and Scenes

Also look at the Group Trigger and Group Listener plugins; they might eliminate need for Python altogether.


Sent from my iPhone using Tapatalk Pro

Posted on
Tue Jul 02, 2019 10:24 am
Mark offline
User avatar
Posts: 124
Joined: Apr 21, 2005
Location: California

Re: Python and Scenes

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests