Python script: testing time an if/then condition

Posted on
Thu Oct 10, 2019 11:50 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Python script: testing time an if/then condition

Is it possible to get the current time - hours or hours&min for use in a conditional test? - e.g. if current time is >1630 and < 2200, then...
I have fussed around with the following, but have been unable to log any values obtained.
Code: Select all
indigo.server.getTime()
indigo.server.getTime().time()
indigo.server.log("time{}".format(time))


Note - I haven't defined a variable for time.

Posted on
Fri Oct 11, 2019 5:46 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script: testing time an if/then condition

Dates and time are a little bit tricky, but once you get the hang of it, it's not too bad.

Code: Select all
t = indigo.server.getTime()
indigo.server.log(u"{0}".format(t))

>>> 2019-10-11 06:42:52.688000

This code results in t which is a datetime object:

Code: Select all
t = indigo.server.getTime()
indigo.server.log(u"{0}".format(type(t)))

>>> <type 'datetime.datetime'>

I would suggest doing a Google search for 'working with dates in python' or something similar to get a leg up.

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

[My Plugins] - [My Forums]

Posted on
Fri Oct 11, 2019 6:43 am
CliveS offline
Posts: 771
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Python script: testing time an if/then condition

SMUSEBY wrote:
Is it possible to get the current time - hours or hours&min for use in a conditional test? - e.g. if current time is >1630 and < 2200, then...
I have fussed around with the following, but have been unable to log any values obtained.


Hopefully this will help, it is a cut down of my bathroom lighting that basically sets the light to 10% brightness between 11pm and 7am otherwise set to 100%

Code: Select all
 
devLuxBath        = indigo.devices[1450400961]   # "Bathroom Fibaro Luminance Sensor"
devDiningLight    = indigo.devices[1519699078]   # "Dining Room Light"

now       = indigo.server.getTime()
time_07   = now.replace(hour= 7, minute=00, second=0, microsecond=0)
time_23   = now.replace(hour=23, minute=00, second=0, microsecond=0)

if not devDiningLight.onState:
   
   if (time_07 <= now <= time_23):
      x = 100
   else:
      x = 10   

   if  devLuxBath.sensorValue < 45:

         indigo.dimmer.setBrightness(devDiningLight,value=x)
     

For your times above you can use
Code: Select all
   
now         = indigo.server.getTime()
time_1630   = now.replace(hour=16, minute=30, second=0, microsecond=0)
time_2200   = now.replace(hour=22, minute=00, second=0, microsecond=0)

if (time_1630 <= now <= time_2200):
    #your code for time between 16:30 and 22:00
else:
    #code not between times above



CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Wed Oct 16, 2019 5:38 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

Both replies were very helpful. Thank you.
New time question:
I am attempting to insert the date.time value into email text. The following is not working:
Code: Select all
now = indigo.server.getTime()
theBody = "On %now, the house was vacant, and the front door was opened" % (now.value)
#

I've also tried (now.name) - neither work.

Posted on
Wed Oct 16, 2019 6:05 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script: testing time an if/then condition

Wrong syntax. Try this:

Code: Select all
now = indigo.server.getTime()
theBody = "On {}, the house was vacant, and the front door was opened".format(now)
#

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Oct 16, 2019 6:40 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

Time is now in the text.
Next issue:
The Indigo Python Documentation has the following regarding variables:

Create a new Indigo variable named fooMonster, change its value multiple times, and delete it:

newVar = indigo.variable.create("fooMonster", "default value")
indigo.variable.updateValue(newVar, "asleep")
indigo.variable.updateValue(newVar, "awake")
indigo.variable.delete(newVar)

It is not clear to me the relationship between 'newVar' and 'fooMonster'. The three following lines using the new variable make no mention of the variable 'fooMonster'.
When I attempt to create a variable, 'theSubject', the script error is "valueError: NameNotUniqueError".

Here is one of my failed iterations:
Code: Select all
now = indigo.server.getTime()
theAddress = indigo.variables[236514828].getValue #email address
var1 = indigo.variable.create("theSubject", "Front Door Opened")
indigo.server.sendEmailTo(theAddress, subject=theSubject, body="some text")


In the last line, substituting 'var1' for 'theSubject' renders the same script error message.

Posted on
Wed Oct 16, 2019 7:27 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script: testing time an if/then condition

What you're looking at is a Python object.

Code: Select all
newVar = indigo.variable.create("fooMonster", "default value")

The above code gets a copy of the Indigo variable object "fooMonster" and the variable 'newVar' refers to it. Because we don't have authority to access the actual Indigo object, we need to work with a copy.

Code: Select all
indigo.variable.updateValue(newVar, "asleep")

The above code tells Indigo to update the variable 'fooMonster" (which newVar refers to) to the value "asleep".

You can inspect the object yourself using many different techniques.
Code: Select all
indigo.server.log(u"{0}".format(newVar))
indigo.server.log(u"{0}".format(type(newVar)))
indigo.server.log(u"{0}".format(dir(newVar)))

Fair warning -- once you start to get the hang of Python in Indigo (and therefore its potential), your spare time will be consumed with all the new possibilities. I look forward to your first plugin!

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

[My Plugins] - [My Forums]

Posted on
Wed Oct 16, 2019 8:33 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

don't hold your breath. - but I am having fun.
Wouldn't be anywhere without the help from you and others.

Posted on
Wed Oct 16, 2019 9:39 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

I give up - can't make the email work.
BTW, I know I am making this unnecessarily complicated, but my method is to make something 'simple' work before a bigger challenge.
Why isn't this running?:
Code: Select all
now = indigo.server.getTime()
varA = indigo.variables[236514828] #email address
theAddress = "%s" % (varA.name)
indigo.server.sendEmailTo(address=theAddress, subject = "BRK House-front door opened", body = "some text")


varA.value also fails.
And what is the nature of the '%s' thing? - it's a line specific variable?

Posted on
Thu Oct 17, 2019 5:51 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script: testing time an if/then condition

Unless the name of the variable is your email address, what you want is the variable value.

You don't need to convert the variable value to a string, because it's already a string.

Also, the parameter for your email address should not be 'address'. According to the docs, the email address is a direct parameter. Try this:

Code: Select all
now = indigo.server.getTime()
varA = indigo.variables[236514828] #email address
indigo.server.sendEmailTo(varA.value, subject="BRK House-front door opened", body="some text")

Should work.

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 17, 2019 6:13 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script: testing time an if/then condition

Sorry, missed your other question.

And what is the nature of the '%s' thing? - it's a line specific variable?


The '%s' thing is used to insert things into a string. So you might have:

Code: Select all
var = 2
indigo.server.log(u"The value of the number is: %s" % var)

I would encourage you to use a different string formatting technique, which should be more future proof and has other benefits that I won't get into now..

Code: Select all
var = 2
indigo.server.log(u"The {0} of the number is: {1}".format("value", var))


This is a great description of the 'dot format' and '%s' string specifiers.

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 17, 2019 4:59 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

The email address continues to fail.
Code: Select all
now = indigo.server.getTime()
varA = indigo.variables[236514828] #email address
hseVcnt = indigo.variables[1176596514].getValue(bool)
theBody = "The house is vacant, and the front door was opened at %s" % (now.value)
theBody2 = "The house is vacant, and the front door was opened at {}" .format(now.value)
indigo.server.sendEmailTo(varA.value, subject = "BRK House-front door opened", body = "some text")

1. Address suggestion?
2. Skipping ahead to the next issue (which may or may not work, as the script above fails with theBody variable commented out, I've tried two approaches to inserting the date/time into the body. Are they correct?
3. How do I limit the time to HH:MM?

Posted on
Thu Oct 17, 2019 6:16 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script: testing time an if/then condition

1. It's tough to know how the email address is failing without the error message that you're getting. If your variable is set to a full email address, then it should work. I've tested the indigo.server.sendEmailTo() function with an email address and it does work.

2. and 3. The time component would be done like this:
Code: Select all
import datetime
now = indigo.server.getTime()
now_formatted = datetime.datetime.strftime(now, "%H:%M")

The code above converts the date/time to a string so it should insert with no problem.

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

[My Plugins] - [My Forums]

Posted on
Fri Oct 18, 2019 10:55 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Python script: testing time an if/then condition

Are you sure that you have the variable ID number correct in the script (for the email address variable)?

And do you maybe have some blank spaces or other characters before/after the email address that are causing a problem?

Image

Posted on
Fri Oct 18, 2019 7:51 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python script: testing time an if/then condition

My mistake on the address - the failure was the var 'theBody', not the address.
I have made several tries at getting the time into the body, as below - none of which worked:
Code: Select all
import datetime
now = indigo.server.getTime()
now_formatted = datetime.datetime.strftime(now, "%H:%M")
varA = indigo.variables[180461382] #email address
hseVcnt = indigo.variables[1176596514].getValue(bool)

# theBody = "The house is vacant, and the front door was opened at %s" % (now.value)
# theBody = "The house is vacant, and the front door was opened at {}" .format("value", now_formatted))
# theBody = "The house is vacant, and the front door was opened at {}" .format("value", now))
theBody = "some text"
# if hseVcnt
indigo.server.sendEmailTo(varA.value, subject = "BRK House-front door opened", body = theBody)

Who is online

Users browsing this forum: No registered users and 7 guests