Daylight switch season based

Posted on
Wed Oct 02, 2019 2:33 pm
mroest offline
Posts: 25
Joined: Feb 01, 2017

Daylight switch season based

Sorry to bother you but i have zero Python experience and just upgraded to 7.4 where my apple scripts are no longer supported. Would be great to get some direction with my scripts (i'll post in seperate posts) to get them working in Python. Thanks in advance.

This script change my daylight swith time depnedent of the season in the system:

Code: Select all
# Indigo no longer supports execution of embedded AppleScripts. For help
# translating AppleScripts to python see: http://indigodomo.com/applescript
#
# The original AppleScript was:
#
# tell application "IndigoServer"
#    if value of variable "strSeason" = "Winter" then
#       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) + (30 * minutes)) as string)
#       
#       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) - (30 * minutes)) as string)
#    else if {"Spring", "autumn"} contains value of variable "strSeason" then
#       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of (calculate sunrise) as string)
#       
#       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of (calculate sunset) as string)
#    else -- Summer
#       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) - (30 * minutes)) as string)
#       
#       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) + (30 * minutes)) as string)
#    end if
#    
# end tell

Posted on
Wed Oct 02, 2019 2:35 pm
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

This is the script i used to set the variables in the system (it rund every night scheduled):

Code: Select all
# Indigo no longer supports execution of embedded AppleScripts. For help
# translating AppleScripts to python see: http://indigodomo.com/applescript
#
# The original AppleScript was:
#
# -- Retrieve current date-time into variable
# set dteCurDateTime to current date
#
#
# tell application "IndigoServer"
#    -- Set strDay to current weekday
#    set value of variable "strDay" to weekday of dteCurDateTime as string
#    log "Update variable strDay to " & value of variable "strDay" using type "Schedule: SetDateTimeVariables"
#    
#    -- Set strMonth to current month
#    set value of variable "strMonth" to month of dteCurDateTime as string
#    log "Update variable strMonth to " & value of variable "strMonth" using type "SSchedule: etDateTimeVariables"
#    
#    set value of variable "intMonth" to month of dteCurDateTime as number
#    log "Update variable intMonth to " & value of variable "intMonth" using type "Schedule: SetDateTimeVariables"
#    
#    --Set strSeason based on month
#    if {"1", "2", "3"} contains value of variable "intMonth" then
#       set value of variable "strSeason" to "Winter"
#       
#    else if {"4", "5", "6"} contains value of variable "intMonth" then
#       set value of variable "strSeason" to "Spring"
#       
#    else if {"7", "8", "9"} contains value of variable "intMonth" then
#       set value of variable "strSeason" to "Summer"
#       
#    else if {"10", "11", "12"} contains value of variable "intMonth" then
#       set value of variable "strSeason" to "Autumn"
#       
#    end if
#    log "Update variable strSeason to " & value of variable "strSeason" using type "Schedule: SetDateTimeVariables"
#    
#    --Set strDayType based on strDay
#    if value of variable "intOverride" > 0 then
#       -- set day to value of variable strOverride" and reduce variable by 1
#       set value of variable "strDayType" to value of variable "strOverride"
#       set value of variable "intOverride" to (value of variable "intOverride") - 1
#    else
#       -- determine daytype regular
#       if {"Monday", "Tuesday"} contains value of variable "strDay" then
#          set value of variable "strDayType" to "werk"
#          
#       else if {"Wednesday"} contains value of variable "strDay" then
#          set value of variable "strDayType" to "thuiswerk"
#          
#       else if {"Thursday", "Friday"} contains value of variable "strDay" then
#          set value of variable "strDayType" to "thuis"
#          
#       else
#          set value of variable "strDayType" to "vrij"
#          
#       end if
#    end if
#    log "Update variable strDayType to " & value of variable "strDayType" using type "Schedule: SetDateTimeVariables"
# end tell
#

Posted on
Wed Oct 02, 2019 2:36 pm
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

And the last one to actually swith sunUp/ Down variable.

Code: Select all
# Indigo no longer supports execution of embedded AppleScripts. For help
# translating AppleScripts to python see: http://indigodomo.com/applescript
#
# The original AppleScript was:
#
# tell application "IndigoServer"
#    -- Set blnZonOp to 1 depending on season
#    if {"Spring", "Autumn"} contains value of variable "strSeason" then
#       set value of variable "blnZonOnder" to 1 as boolean
#       set value of variable "blnZonOp" to 0 as boolean
#       log "Switched variables blnZonOp and blnZonOnder for " & value of variable "strSeason" using type "Dagritme - blnZonOnder"
#    end if
# end tell

All help is really appreciated!
#

Posted on
Wed Oct 02, 2019 3:33 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Daylight switch season based

Hi Marc, can you edit each post and describe what you're trying to do? You have a lot of AppleScript code there and it will likely be easier for us to help if we can just hear exactly what it is you're trying to do.

[EDIT] I added code tags around your scripts so they are easier to read, but I still think adding a complete description of what you're trying to do will result in faster help.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Oct 02, 2019 3:42 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Daylight switch season based

And please use the CODE tags instead of the # character.

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

Posted on
Wed Oct 02, 2019 3:44 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Daylight switch season based

Also, you might consider switching back to 7.3 until you get these scripts replaced, as I expect a bunch of your stuff just doesn't work right anymore.

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

Posted on
Thu Oct 03, 2019 12:40 am
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

Thanks for your replies!

Actually i have 2 important things that i do many tmes.

One is i fill variables based on the month in the year (season) and the day of the week (workday, weekend day).
Based on these variables i set the SunUp and SunUnder relative to the actual time (so in the summer 1 hour after dtSunSet and in the Winter an hour before.

If i have an example how to do that i can fix the rest myself i thinkg.

So i need to get the dateparts from the current date (month, day)
And adjust variables with these values

Thanks again.

Pasting the scripts without # doesn't work since it doesn't recognize applescript unfortunately.
Code: Select all
 tell application "IndigoServer"
    if value of variable "strSeason" = "Winter" then
       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) + (30 * minutes)) as string)
       
       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) - (30 * minutes)) as string)
    else if {"Spring", "autumn"} contains value of variable "strSeason" then
       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of (calculate sunrise) as string)
       
       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of (calculate sunset) as string)
    else -- Summer
       set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) - (30 * minutes)) as string)
       
       set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) + (30 * minutes)) as string)
    end if
 end tell

Posted on
Thu Oct 03, 2019 12:56 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Daylight switch season based

I think the suggestion was to use the Code tag (Selection Style) when composing your post e.g.:
Code: Select all
tell application "IndigoServer"
    if value of variable "strSeason" = "Winter" then
        set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) + (30 * minutes)) as string)

        set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) - (30 * minutes)) as string)
    else if {"Spring", "autumn"} contains value of variable "strSeason" then
        set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of (calculate sunrise) as string)

        set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of (calculate sunset) as string)
    else -- Summer
        set value of variable "dtSunrise" to (weekday of (calculate sunrise) as string) & " " & (time string of ((calculate sunrise) - (30 * minutes)) as string)

        set value of variable "dtSunset" to (weekday of (calculate sunset) as string) & " " & (time string of ((calculate sunset) + (30 * minutes)) as string)
    end if

end tell
it makes it easier to understand the code. :)

Posted on
Thu Oct 03, 2019 4:48 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Daylight switch season based

I think this is pretty close (untested):

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import datetime as dt

strSeason = indigo.variables['strSeason']
sunrise   = indigo.server.calculateSunrise()
sunset    = indigo.server.calculateSet()
delta     = dt.timedelta(minutes=30)

if strSeason.value == "Winter" then:
    indigo.variable.updateValue("dtSunrise", dt.datetime.strftime(sunrise + delta, "%A %H:%M")
    indigo.variable.updateValue("dtSunset", dt.datetime.strftime(sunset - delta, "%A %H:%M")

elif strSeason.value == "Summer":
    indigo.variable.updateValue("dtSunrise", dt.datetime.strftime(sunrise - delta, "%A %H:%M")
    indigo.variable.updateValue("dtSunset", dt.datetime.strftime(sunset + delta, "%A %H:%M")

else:
    indigo.variable.updateValue("dtSunrise", dt.datetime.strftime(sunrise, "%A %H:%M")
    indigo.variable.updateValue("dtSunset", dt.datetime.strftime(sunset, "%A %H:%M")

You should consider replacing Indigo variable names with their ID numbers in your scripts (the preferred approach, as ID numbers don't change when a variable is renamed.)

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 03, 2019 5:59 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Daylight switch season based

I think the other two do deserve descriptions as it may be a lot easier to do in Python than AS.

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 03, 2019 7:58 am
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

Thank you so much! this is really helping. I'll try to explain the other two..

I have this script running 4 times, dependent on the season variable it switches the variable to ZonOp/ ZonOnder (Day/Night) that variable triggers a number of scenes.
So i need to update the variable blnZonOp to 1 if the variable strSeason contains Spring or Automn. otherwise it does nothing.

Code: Select all
tell application "IndigoServer"
    -- Set blnZonOp to 1 depending on season
    if {"Spring", "Autumn"} contains value of variable "strSeason" then
       set value of variable "blnZonOnder" to 1 as boolean
       set value of variable "blnZonOp" to 0 as boolean
       log "Switched variables blnZonOp and blnZonOnder for " & value of variable "strSeason" using type "Dagritme - blnZonOnder"
    end if

Posted on
Thu Oct 03, 2019 8:03 am
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

And the largest one below.

It fills the variables strDay, strMonth and iMonth with the name of the current day, month and the number of the current month.
Then it fils the strSeason based on the month number.
Then i have a variable strDayType that is filled based on the day, Friday is workinghome etc. but if there is a number in intOverride it should set the strDayType to the value in strOverride.

So if i go on holiday i set the intOverride to e.g. 14 and the strOverride to Holiday so the system runs the holiday program for the next 14 days.

Thanks again!

Code: Select all
-- Retrieve current date-time into variable
 set dteCurDateTime to current date

 tell application "IndigoServer"
    -- Set strDay to current weekday
    set value of variable "strDay" to weekday of dteCurDateTime as string
    log "Update variable strDay to " & value of variable "strDay" using type "Schedule: SetDateTimeVariables"
   
    -- Set strMonth to current month
    set value of variable "strMonth" to month of dteCurDateTime as string
    log "Update variable strMonth to " & value of variable "strMonth" using type "SSchedule: etDateTimeVariables"
   
    set value of variable "intMonth" to month of dteCurDateTime as number
    log "Update variable intMonth to " & value of variable "intMonth" using type "Schedule: SetDateTimeVariables"
   
    --Set strSeason based on month
    if {"1", "2", "3"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Winter"
       
    else if {"4", "5", "6"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Spring"
       
    else if {"7", "8", "9"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Summer"
       
    else if {"10", "11", "12"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Autumn"
       
    end if
    log "Update variable strSeason to " & value of variable "strSeason" using type "Schedule: SetDateTimeVariables"
   
    --Set strDayType based on strDay
    if value of variable "intOverride" > 0 then
       -- set day to value of variable strOverride" and reduce variable by 1
       set value of variable "strDayType" to value of variable "strOverride"
       set value of variable "intOverride" to (value of variable "intOverride") - 1
    else
       -- determine daytype regular
       if {"Monday", "Tuesday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "werk"
         
       else if {"Wednesday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "thuiswerk"
         
       else if {"Thursday", "Friday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "thuis"
         
       else
          set value of variable "strDayType" to "vrij"
         
       end if
    end if
    log "Update variable strDayType to " & value of variable "strDayType" using type "Schedule: SetDateTimeVariables"
 end tell

Posted on
Thu Oct 03, 2019 8:23 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Daylight switch season based

mroest wrote:
Thank you so much! this is really helping. I'll try to explain the other two..

I have this script running 4 times, dependent on the season variable it switches the variable to ZonOp/ ZonOnder (Day/Night) that variable triggers a number of scenes.
So i need to update the variable blnZonOp to 1 if the variable strSeason contains Spring or Automn. otherwise it does nothing.

Code: Select all
tell application "IndigoServer"
    -- Set blnZonOp to 1 depending on season
    if {"Spring", "Autumn"} contains value of variable "strSeason" then
       set value of variable "blnZonOnder" to 1 as boolean
       set value of variable "blnZonOp" to 0 as boolean
       log "Switched variables blnZonOp and blnZonOnder for " & value of variable "strSeason" using type "Dagritme - blnZonOnder"
    end if


I think this one really doesn't need to be a script. This should be easy to do as a action with conditionals.

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

seasonVar = indigo.variables['strSeason']

if seasonVar.value in ['Spring', 'Autumn']:
    indigo.variable.updateValue("blnZonOp", "0")
    indigo.variable.updateValue("blnZonOnder", "1")

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

Posted on
Thu Oct 03, 2019 8:50 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Daylight switch season based

I broke this into two sections for readability, but it should all be one script.

See https://docs.python.org/2/library/datetime.html for the use of the formatting strings to get day/date/month/etc strings.

Code: Select all
    -- Set strDay to current weekday
    set value of variable "strDay" to weekday of dteCurDateTime as string
    log "Update variable strDay to " & value of variable "strDay" using type "Schedule: SetDateTimeVariables"
   
    -- Set strMonth to current month
    set value of variable "strMonth" to month of dteCurDateTime as string
    log "Update variable strMonth to " & value of variable "strMonth" using type "SSchedule: etDateTimeVariables"
   
    set value of variable "intMonth" to month of dteCurDateTime as number
    log "Update variable intMonth to " & value of variable "intMonth" using type "Schedule: SetDateTimeVariables"
   
    --Set strSeason based on month
    if {"1", "2", "3"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Winter"
       
    else if {"4", "5", "6"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Spring"
       
    else if {"7", "8", "9"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Summer"
       
    else if {"10", "11", "12"} contains value of variable "intMonth" then
       set value of variable "strSeason" to "Autumn"
       
    end if
    log "Update variable strSeason to " & value of variable "strSeason" using type "Schedule: SetDateTimeVariables"


Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import date
today = date.today()
indigo.variable.updateValue("strDay", today.strftime("%A"))
indigo.variable.updateValue("strMonth", today.strftime("%B"))
indigo.variable.updateValue("intMonth", today.strftime("%m"))
if today.month in [1, 2, 3]:
    indigo.variable.updateValue("strSeason", "Winter")
elif today.month() in [4, 5, 6]:
    indigo.variable.updateValue("strSeason", "Spring")
elif today.month() in [7, 8, 9]:
    indigo.variable.updateValue("strSeason", "Summer")
elif today.month() in [10, 11, 12]:
    indigo.variable.updateValue("strSeason", "Autumn")


Code: Select all
    --Set strDayType based on strDay
    if value of variable "intOverride" > 0 then
       -- set day to value of variable "strOverride" and reduce variable by 1
       set value of variable "strDayType" to value of variable "strOverride"
       set value of variable "intOverride" to (value of variable "intOverride") - 1
    else
       -- determine daytype regular
       if {"Monday", "Tuesday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "werk"
         
       else if {"Wednesday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "thuiswerk"
         
       else if {"Thursday", "Friday"} contains value of variable "strDay" then
          set value of variable "strDayType" to "thuis"
         
       else
          set value of variable "strDayType" to "vrij"
         
       end if
    end if
    log "Update variable strDayType to " & value of variable "strDayType" using type "Schedule: SetDateTimeVariables"


Code: Select all
intOverride = indigo.variables['intOverride'].getValue(int)
if intOverride > 0:
    indigo.variable.updateValue("strDayType", indigo.variables['strOverride'].value)
    indigo.variable.updateValue("intOverride", str(intOverride - 1))

else:
    if indigo.variables['strDay'].value in ['Monday', 'Tuesday']:
        indigo.variable.updateValue("strDayType", "werk")
    elif indigo.variables['strDay'].value in ['Wednesday']:
        indigo.variable.updateValue("strDayType", "thuiswerk")
    elif indigo.variables['strDay'].value in ['Thursday', 'Friday']:
        indigo.variable.updateValue("strDayType", "thuis")
    else:
        indigo.variable.updateValue("strDayType", "vrij")
Last edited by FlyingDiver on Thu Oct 03, 2019 9:25 am, edited 1 time in total.

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

Posted on
Thu Oct 03, 2019 8:52 am
mroest offline
Posts: 25
Joined: Feb 01, 2017

Re: Daylight switch season based

One more NOOB question..

i keep getting embedded script: invalid syntax errors.

When i copy/ paste a script from the site. When i type it over manually. but the hightlighted part keeps changing. So if i have a row with an error. I remove that row and type it in manually. then there is suddenly a new error at the end of an existing row or even on an empty row. Even rows starting with # gives this error.

How to paste a working script into the IndigoDomo editor without getting these strange errors? it feels like the editor isn't doing a proper check or gives random errors

Who is online

Users browsing this forum: No registered users and 1 guest

cron