How to shorten sunrise / sunset values

Posted on
Sat Sep 02, 2017 7:46 am
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

How to shorten sunrise / sunset values

Hi, I found this code on the board somewhere - I wanted to pull sunset and sunrise times into two variables for use on a control page - it works great but I don't want to see the AM/PM or seconds. Is there a way to truncate them out or should I use some other method to stuff the values?

Thanks for any help...
Attachments
2017-09-02_06-41-18.png
2017-09-02_06-41-18.png (24.59 KiB) Viewed 5427 times

_______
Norm

Posted on
Sat Sep 02, 2017 10:32 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to shorten sunrise / sunset values

If you use a Python script, you can use the date/time formatting specifier to format it any way you like:

Code: Select all
from datetime import datetime
sunrise = indigo.server.calculateSunrise()
indigo.variable.updateValue(VARIABLEIDHERE, value=datetime.strftime(sunrise, "%-I:%M"))


will (for instance) insert "7:08" into the variable.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Sep 02, 2017 10:38 am
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

Re: How to shorten sunrise / sunset values

cool - easy , and works great - is there a book you recommend I can read to help me figure this stuff out (so I don't bug you so much Jay)?

_______
Norm

Posted on
Sat Sep 02, 2017 11:09 am
jalves offline
Posts: 744
Joined: Jun 16, 2013

Re: How to shorten sunrise / sunset values

Right now I'm using an AppleScript to do something similar but I like the idea of just showing the day and hh:mm (no seconds). This script is something I found here and is triggered by any change in the "isDaylight" variable. Can somebody tell me how to modify this script to do that? Or suggest a python script?
Code: Select all
tell application "IndigoServer"
   
   set value of variable "sunrise" to "Sunrise: " & (weekday of (calculate sunrise) as string) & " " & (time string of (calculate sunrise) as string)
   
   
   set value of variable "sunset" to "Sunset: " & (weekday of (calculate sunset) as string) & " " & (time string of (calculate sunset) as string)
   
end tell

Running Indigo 2023.2 on a 24" iMac M1), OS X 14.4
Jeff

Posted on
Sat Sep 02, 2017 12:18 pm
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

Re: How to shorten sunrise / sunset values

Jeff that is the same question I asked (and the same AppleScript), Jay already answered, the python script is in the post two up from your post.. It works great - you need to add an action for sunset and sub in your ID for the variable. See below for the whole thing - my variable IDs are "1728448011 for sunrise variable" and "3027629 for my sunset variable"....

So two actions in the same schedule - one for sunrise and one for sunset - I run it just after midnight...

Code: Select all
from datetime import datetime
sunrise = indigo.server.calculateSunrise()
indigo.variable.updateValue(1728448011, value=datetime.strftime(sunrise, "%-I:%M"))


Code: Select all
from datetime import datetime
sunrise = indigo.server.calculateSunset()
indigo.variable.updateValue(3027629, value=datetime.strftime(sunrise, "%-I:%M"))

_______
Norm

Posted on
Sat Sep 02, 2017 7:29 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to shorten sunrise / sunset values

norcoscia wrote:
cool - easy , and works great - is there a book you recommend I can read to help me figure this stuff out (so I don't bug you so much Jay)?


There are tons of Python tutorials out on the net - it's a very popular language. The Indigo specifics are documented in the Technical Documentation section of our docs.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Sep 02, 2017 7:32 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to shorten sunrise / sunset values

norcoscia wrote:
So two actions in the same schedule - one for sunrise and one for sunset - I run it just after midnight...


You can do them both in one script:

Code: Select all
from datetime import datetime
sunrise = indigo.server.calculateSunrise()
indigo.variable.updateValue(1728448011, value=datetime.strftime(sunrise, "%-I:%M"))
sunset = indigo.server.calculateSunset()
indigo.variable.updateValue(3027629, value=datetime.strftime(sunset, "%-I:%M"))

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Sep 02, 2017 7:35 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to shorten sunrise / sunset values

jalves wrote:
Right now I'm using an AppleScript to do something similar but I like the idea of just showing the day and hh:mm (no seconds). This script is something I found here and is triggered by any change in the "isDaylight" variable. Can somebody tell me how to modify this script to do that? Or suggest a python script?


A slight modification to the python script above:

Code: Select all
from datetime import datetime
sunrise = indigo.server.calculateSunrise()
indigo.variable.updateValue(SUNRISEVARIABLEIDHERE, value=datetime.strftime(sunrise, "%A %-I:%M"))
sunset = indigo.server.calculateSunset()
indigo.variable.updateValue(SUNSETVARIABLEIDHERE, value=datetime.strftime(sunset, "%A %-I:%M"))


You can use a lowercase '%a' rather than the uppercase '%A' in the format specifier to get the 3 character version of the day.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 03, 2017 5:07 am
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

Re: How to shorten sunrise / sunset values

Thanks Jay. Where is the information about using things like "%A %-I:%M" or the fact that you can use "%a" - I can't find that in the indigo information? I would like to understand these since I'm starting to work more on control pages and pulling the info in the right format seems like the least painful way to get it ready for display...

_______
Norm

Posted on
Sun Sep 03, 2017 5:40 am
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

Re: How to shorten sunrise / sunset values

I found this floating out on the web - I'm assuming this is what I should use - is it in the indigo documentation?

[MODERATOR NOTE] removed attachment as it was not specifically for Python.

_______
Norm

Posted on
Sun Sep 03, 2017 9:25 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to shorten sunrise / sunset values

norcoscia wrote:
Thanks Jay. Where is the information about using things like "%A %-I:%M" or the fact that you can use "%a" - I can't find that in the indigo information? I would like to understand these since I'm starting to work more on control pages and pulling the info in the right format seems like the least painful way to get it ready for display...


It's in a link I provided in a post above (it's always recommended to read the FULL text of posts responding to questions and follow any links).

norcoscia wrote:
I found this floating out on the web - I'm assuming this is what I should use - is it in the indigo documentation?


This is part of the standard Python library, which we do not replicate in our documentation for obvious reasons.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 03, 2017 9:32 am
norcoscia offline
User avatar
Posts: 1206
Joined: Sep 09, 2015

Re: How to shorten sunrise / sunset values

Thanks, Jay, and apologies - I missed the link for the date/time formatting specifiers :-(

_______
Norm

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests