Comparing 2 dates

Posted on
Sun Aug 08, 2021 4:48 am
Bildhauer offline
Posts: 160
Joined: Sep 30, 2019
Location: 22113 Oststeinbek (near Hamburg), Germany

Comparing 2 dates

Hello all,

for the logic of a script managing my irrigation system I would like to do the following:

    1. Each time the irrigation takes plays save the date in a variable
    2. take the timestamp as a date of today.
    3. tell me the amount of days between today and the lasst irrigation
To be honest: I was not able to write this simple script!

Best regards

Bildhauer

Posted on
Thu Aug 12, 2021 8:17 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Comparing 2 dates

Bildhauer wrote:
Hello all,

for the logic of a script managing my irrigation system I would like to do the following:

    1. Each time the irrigation takes plays save the date in a variable
    2. take the timestamp as a date of today.
    3. tell me the amount of days between today and the lasst irrigation
To be honest: I was not able to write this simple script!

Best regards

Bildhauer


As an alternative to storing all these variables and writing a script, you could consider using the Timers and Pesters https://wiki.indigodomo.com/doku.php?id=indigo_7_documentation:plugins:timersandpesters plugin to:

    1. Start a timer when the first irrigation occurs
    2. Reset the timer on each irrigation
    3. Show your timer value on a control page, or Alexa, or however you want to find out the info

Posted on
Thu Aug 12, 2021 1:54 pm
Bildhauer offline
Posts: 160
Joined: Sep 30, 2019
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: Comparing 2 dates

Thx, but I need a solution as described.

But thank you for help!

Best regards

Bildhauer1963

Posted on
Thu Aug 12, 2021 3:35 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Comparing 2 dates


Posted on
Thu Aug 12, 2021 5:05 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Comparing 2 dates

Code: Select all
import datetime as dt

#  date_1 = indigo.variables[12345].value()  # This is how you get the value of an Indigo variable
date_1 = "2021-08-09 12:34:30.12345"  # Indigo variables are strings, so they must be converted
date_2 = "2021-08-12 23:45:00.54321"

d1 = dt.datetime.strptime(date_1, "%Y-%m-%d %H:%M:%S.%f")  # This is how you convert the strings to date objects
d2 = dt.datetime.strptime(date_2, "%Y-%m-%d %H:%M:%S.%f")

diff = d2 - d1

indigo.server.log(str(diff.days))

If you want fractional days, the last line would require a bit more effort (but not a lot more effort).

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests