Time difference

Posted on
Wed Oct 19, 2022 3:11 am
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Time difference

Hi all,

I am trying to write a script to check if "now" is between sunrise and sunset, but I have problem to calculate the time difference (or maybe the time difference is not needed at all, it would be just enough to check with a IF and < >), anyway.

Code: Select all
import datetime

sunrise = indigo.server.calculateSunrise()
sunset = indigo.server.calculateSunset()
now = datetime.datetime.now()

huePlugin = indigo.server.getPlugin("com.nathansheldon.indigoplugin.HueLights")

timeDelta1 = now - sunrise # if < 0, is bright enough
timeDelta2 = now - sunset # # if > 0, it is bright enough

indigo.server.log("timeDelta1" + timeDelta1)
indigo.server.log("timeDelta2: " + timeDelta2.strftime("%H:%M:%S"))

scala1 = indigo.devices[7081105]
scala4 = indigo.devices[1015827393] # "Scala-4"

isNight = indigo.variables[1390212289]
logMsg = ""

indigo.server.log("isNight: " + isNight.value)

# from here it is to be modified and use some variables, not yet implemented

# if ((isDark and scala1.brightness == 0) or (isNight == "true" and scala1.brightness == 0)):
if (isNight == "true" and scala1.brightness == 0):
    indigo.server.log("Motion KG: Is Dark or night is ON and Scala-1 is OFF, I will switch lights ON")
    huePlugin.executeAction("setCT", deviceId=7081105, props={"brightness":10, "temperature":2000})
    huePlugin.executeAction("setCT", deviceId= 1015827393, props={"brightness":10, "temperature":2000})
    time.sleep(45)
    huePlugin.executeAction("setBrightness", deviceId=7081105, props={"brightness":0, "rate":10})
    huePlugin.executeAction("setBrightness", deviceId= 1015827393, props={"brightness":0, "rate":10})   
    indigo.server.log("45 seconds elapsed, I will switch off lights now")
   
   
else:
   logMsg = "Motion KG: None of the conditions are satisfied, I will not switch the lights ON"

indigo.server.log(logMsg)


I am trying to print those line to check the values:

Code: Select all
indigo.server.log("timeDelta1" + timeDelta1)
indigo.server.log("timeDelta2: " + timeDelta2.strftime("%H:%M:%S"))


But I get:

Code: Select all
Script Error                    trigger "Movimento Notte KG (Python)" script error in file Motion-KG.py:
   Script Error                    can only concatenate str (not "datetime.timedelta") to str
   Script Error                    Exception Traceback (most recent call shown last):

     Motion-KG.py, line 13, at top level
TypeError: can only concatenate str (not "datetime.timedelta") to str


Thanks
Marco

Posted on
Wed Oct 19, 2022 5:34 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Time difference

The error is due to the fact that you can't add a string together with a timedelta object. So instead of using a construction like this:

Code: Select all
indigo.server.log("timeDelta1" + timeDelta1)
You need to make the two elements share the same type. Since you're logging, you need to make them both strings:

Code: Select all
# In Python 3:
indigo.server.log(f"timeDelta1 {timeDelta1})

# In Python 2:
indigo.server.log(u"timeDelta1  {0}").format(timeDelta1)
You have more than one of those that you'll need to fix.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Oct 19, 2022 5:44 am
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Time difference

Is there a reason you can't use the "isDaylight" variable?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Oct 19, 2022 6:33 am
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Re: Time difference

FlyingDiver wrote:
Is there a reason you can't use the "isDaylight" variable?
Of course no reason, I totally forgot about that.
I will do it using isDaylight.

Thanks

Posted on
Thu Oct 20, 2022 11:06 am
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Re: Time difference

FlyingDiver wrote:
Is there a reason you can't use the "isDaylight" variable?


I do not understand why this statement is not working

Code: Select all
if (isDaylight == False or isNight == True):


now both of them are false, it should enter in the "if"

Posted on
Thu Oct 20, 2022 12:20 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Time difference

Indigo variables are strings, not booleans. So you need:
Code: Select all
if (isDaylight == "false" or isNight == "true"):


Probably. isDaylight will always be lower case. I don't know how isNight is getting set, so it might not be lower case.
Last edited by FlyingDiver on Thu Oct 20, 2022 12:22 pm, edited 1 time in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Oct 20, 2022 12:22 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Time difference

Also, make sure you're getting the VALUE of the Indigo variable, not the variable object.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests

cron