Announcement

Collapse
No announcement yet.

Terminating a script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pocster
    replied
    Originally posted by AllHailJ View Post
    Look on your custom page as shown below. The file should be in ..../scripts.

    Click image for larger version

Name:	Screenshot from 2020-06-25 09-13-14.png
Views:	159
Size:	96.3 KB
ID:	1396630

    Here is the file

    Code:
    IMPORTS System.IO
    IMPORTS System.net
    
    ' This is the startup script
    ' It is run once when HomeSeer starts up
    '
    ' You may also have Startup.vb and it will be run instead of this script.
    '
    Sub Main(Parm As Object)
    
    Dim SpeakClients As String = ""
    Dim errst
    Dim FQPN As String = "Watering6120.ini"
    Dim ZoneTime As Integer = hs.GetINISetting("Watering", "ZoneTime", "540", FQPN) 'Create a default
    Dim FlowerTime As Integer = hs.GetINISetting("Watering", "FlowerTime", "960", FQPN)'Create a default
    Dim sCycles As Integer = hs.GetINISetting("Watering", "SoakCycles", "1", FQPN) 'Create a default
    
    'Use simplified method to calculate the Epoch time representing the start of the seasons
    ' Used the Method given at: https://www.aa.quae.nl/en/antwoorden/seizoenen.html
    'This is good until the year 2100 within a margin of error of 19 minutes.
    'This is in UCT (GMT or Zulu)
    Dim FQPN1 As String = "Seasons.ini"
    Dim CurrentYear As DateTime = Now()
    Dim Year = CInt(CurrentYear.Year)
    Dim Spring As Long = hs.GetINISetting("Season", "Spring", "1111318399", FQPN1) 'Create a default
    Dim Summer As Long = hs.GetINISetting("Season", "Summer", "1119335951", FQPN1) 'Create a default
    Dim Fall As Long = hs.GetINISetting("Season", "Fall", "1127427394", FQPN1) 'Create a default
    Dim Winter As Long = hs.GetINISetting("Season", "Winter", "1135190091", FQPN1) 'Create a default
    
    'These are the Dates and time for the Start of Spring, Summer, Fall and Winter.
    Dim ESpring As Long = 1111318399
    Dim ESummer As Long = 1119335951
    Dim EFall As Long = 1127427394
    Dim EWinter As Long = 1135190091
    
    'These are the seconds for each type of year.
    Dim SpringYear As Long = 31556942
    Dim SummerYear As Long = 31556877
    Dim FallYear As Long = 31556911
    Dim WinterYear As Long = 31556973
    
    'The timespamps for the current year
    Dim CurrentSpring As Long = ESpring + (Year-2005)*SpringYear
    Dim CurrentSummer As Long = ESummer + (Year-2005)*SummerYear
    Dim CurrentFall As Long = EFall + (Year-2005)*FallYear
    Dim CurrentWinter As Long = EWinter + (Year-2005)*WinterYear
    Dim sCurrentSpring, sCurrentSummer, sCurrentFall, SCurrentWinter As String
    Dim MyTime As DateTime
    Dim MyETime As New DateTime(1970, 1, 1)
    
    'Save to the .ini file
    hs.SaveINISetting("Season", "Spring", CStr(CurrentSpring), FQPN1)
    hs.SaveINISetting("Season", "Summer", CStr(CurrentSummer), FQPN1)
    hs.SaveINISetting("Season", "Fall", CStr(CurrentFall), FQPN1)
    hs.SaveINISetting("Season", "Winter", CStr(CurrentWinter), FQPN1)
    
    'Create and save to global variables
    errst = hs.CreateVar("CurrentSpring")
    errst = hs.CreateVar("CurrentSummer")
    errst = hs.CreateVar("CurrentFall")
    errst = hs.CreateVar("CurrentWinter")
    'Now in String format
    errst = hs.CreateVar("sCurrentSpring")
    errst = hs.CreateVar("sCurrentSummer")
    errst = hs.CreateVar("sCurrentFall")
    errst = hs.CreateVar("sCurrentWinter")
    
    errst=hs.SaveVar("CurrentSpring", CurrentSpring)
    errst=hs.SaveVar("CurrentSummer", CurrentSummer)
    errst=hs.SaveVar("CurrentFall", CurrentFall)
    errst=hs.SaveVar("CurrentWinter", CurrentWinter)
    
    'Convert to string
    MyTime = MyETime.AddSeconds(CurrentSpring)
    sCurrentSpring = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentSummer)
    sCurrentSummer = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentFall)
    sCurrentFall = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentWinter)
    sCurrentWinter = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    
    'Now in String format
    errst = hs.SaveVar("sCurrentSpring", sCurrentSpring)
    errst = hs.SaveVar("sCurrentSummer", sCurrentSummer )
    errst = hs.SaveVar("sCurrentFall", sCurrentFall)
    errst = hs.SaveVar("sCurrentWinter", SCurrentWinter)
    
    ' -------------------------------------------------------------------
    'Back to the original script
    hs.WriteLog("Startup", "Scripting is OK and is now running Startup.vb")
    
    SpeakClients = hs.GetInstanceList
    If SpeakClients Is Nothing Then SpeakClients = ""
    
    If String.IsNullOrEmpty(SpeakClients.Trim) Then
    hs.WriteLog("Startup", "(Startup.vb script) No speaker clients detected, waiting up to 30 seconds for any to connect...")
    ' There are no connected speaker clients, so let's wait 30 seconds
    ' (the default re-connect interval for a speaker client)
    ' to see if we can get one connected, otherwise the two speak
    ' commands below will not be heard.
    hs.WaitSecs(1)
    Dim Start As Date = Now()
    Do
    SpeakClients = hs.GetInstanceList
    If SpeakClients Is Nothing Then SpeakClients = ""
    If String.IsNullOrEmpty(SpeakClients.Trim) Then
    hs.WaitSecs(1)
    Else
    Exit Do
    End If
    Loop Until Now.Subtract(Start).TotalSeconds > 30
    End If
    
    
    ' Speak - comment the next line if you do not want HomeSeer to speak at startup.
    ' hs.Speak("Home Seer Restarted - House Ready", True)
    
    ' speak the port the web server is running on
    'Dim port As String = hs.GetINISetting("Settings", "gWebSvrPort", "")
    'If port  "80" Then
    'hs.Speak("Web server port number is " & port)
    'End If
    
    ' You may add your own commands to this script.
    ' See the scripting section of the HomeSeer help system for more information.
    ' You may access help by going to your HomeSeer website and clicking the HELP button,
    ' or by pointing your browser to the /help page of your HomeSeer system.
    
    'Call Additonal scripts - jlg 5/7/2019
    '
    hs.RunScript("Easter.vb",True,True) 'Find out the date for easter, Create a Global variable and write the date to it.
    'hs.RunScript("Seasons.vb",True,True)'Determine the Start of each seson for the current year.
    
    '-------------------------------------------
    'JLG - 5/23/2019
    'Create the global variables needed
    '7-3-2019 Add Watering Global variables.
    '-------------------------------------------
    
    errst = hs.CreateVar("zntime")
    errst = hs.CreateVar("znflwrtime")
    errst = hs.CreateVar("soakcycles")
    errst = hs.CreateVar("Instances")
    
    
    
    errst = hs.SaveVar("zntime",ZoneTime)
    errst = hs.SaveVar("znflwrtime",FlowerTime)
    errst = hs.SaveVar("soakcycles",sCycles)
    errst = hs.SaveVar("Instances", "Initialization")
    
    End Sub
    Hope this helps
    Thank you !!!

    Leave a comment:


  • AllHailJ
    replied
    Look on your custom page as shown below. The file should be in ..../scripts.

    Click image for larger version

Name:	Screenshot from 2020-06-25 09-13-14.png
Views:	159
Size:	96.3 KB
ID:	1396630

    Here is the file

    Code:
    IMPORTS System.IO
    IMPORTS System.net
    
    ' This is the startup script
    ' It is run once when HomeSeer starts up
    '
    ' You may also have Startup.vb and it will be run instead of this script.
    '
    Sub Main(Parm As Object)
    
    Dim SpeakClients As String = ""
    Dim errst
    Dim FQPN As String = "Watering6120.ini"
    Dim ZoneTime As Integer = hs.GetINISetting("Watering", "ZoneTime", "540", FQPN) 'Create a default
    Dim FlowerTime As Integer = hs.GetINISetting("Watering", "FlowerTime", "960", FQPN)'Create a default
    Dim sCycles As Integer = hs.GetINISetting("Watering", "SoakCycles", "1", FQPN) 'Create a default
    
    'Use simplified method to calculate the Epoch time representing the start of the seasons
    ' Used the Method given at: https://www.aa.quae.nl/en/antwoorden/seizoenen.html
    'This is good until the year 2100 within a margin of error of 19 minutes.
    'This is in UCT (GMT or Zulu)
    Dim FQPN1 As String = "Seasons.ini"
    Dim CurrentYear As DateTime = Now()
    Dim Year = CInt(CurrentYear.Year)
    Dim Spring As Long = hs.GetINISetting("Season", "Spring", "1111318399", FQPN1) 'Create a default
    Dim Summer As Long = hs.GetINISetting("Season", "Summer", "1119335951", FQPN1) 'Create a default
    Dim Fall As Long = hs.GetINISetting("Season", "Fall", "1127427394", FQPN1) 'Create a default
    Dim Winter As Long = hs.GetINISetting("Season", "Winter", "1135190091", FQPN1) 'Create a default
    
    'These are the Dates and time for the Start of Spring, Summer, Fall and Winter.
    Dim ESpring As Long = 1111318399
    Dim ESummer As Long = 1119335951
    Dim EFall As Long = 1127427394
    Dim EWinter As Long = 1135190091
    
    'These are the seconds for each type of year.
    Dim SpringYear As Long = 31556942
    Dim SummerYear As Long = 31556877
    Dim FallYear As Long = 31556911
    Dim WinterYear As Long = 31556973
    
    'The timespamps for the current year
    Dim CurrentSpring As Long = ESpring + (Year-2005)*SpringYear
    Dim CurrentSummer As Long = ESummer + (Year-2005)*SummerYear
    Dim CurrentFall As Long = EFall + (Year-2005)*FallYear
    Dim CurrentWinter As Long = EWinter + (Year-2005)*WinterYear
    Dim sCurrentSpring, sCurrentSummer, sCurrentFall, SCurrentWinter As String
    Dim MyTime As DateTime
    Dim MyETime As New DateTime(1970, 1, 1)
    
    'Save to the .ini file
    hs.SaveINISetting("Season", "Spring", CStr(CurrentSpring), FQPN1)
    hs.SaveINISetting("Season", "Summer", CStr(CurrentSummer), FQPN1)
    hs.SaveINISetting("Season", "Fall", CStr(CurrentFall), FQPN1)
    hs.SaveINISetting("Season", "Winter", CStr(CurrentWinter), FQPN1)
    
    'Create and save to global variables
    errst = hs.CreateVar("CurrentSpring")
    errst = hs.CreateVar("CurrentSummer")
    errst = hs.CreateVar("CurrentFall")
    errst = hs.CreateVar("CurrentWinter")
    'Now in String format
    errst = hs.CreateVar("sCurrentSpring")
    errst = hs.CreateVar("sCurrentSummer")
    errst = hs.CreateVar("sCurrentFall")
    errst = hs.CreateVar("sCurrentWinter")
    
    errst=hs.SaveVar("CurrentSpring", CurrentSpring)
    errst=hs.SaveVar("CurrentSummer", CurrentSummer)
    errst=hs.SaveVar("CurrentFall", CurrentFall)
    errst=hs.SaveVar("CurrentWinter", CurrentWinter)
    
    'Convert to string
    MyTime = MyETime.AddSeconds(CurrentSpring)
    sCurrentSpring = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentSummer)
    sCurrentSummer = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentFall)
    sCurrentFall = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    MyTime = MyETime.AddSeconds(CurrentWinter)
    sCurrentWinter = MyTime.ToString("MM/dd/yyyy HH:mm:ss")
    
    'Now in String format
    errst = hs.SaveVar("sCurrentSpring", sCurrentSpring)
    errst = hs.SaveVar("sCurrentSummer", sCurrentSummer )
    errst = hs.SaveVar("sCurrentFall", sCurrentFall)
    errst = hs.SaveVar("sCurrentWinter", SCurrentWinter)
    
    ' -------------------------------------------------------------------
    'Back to the original script
    hs.WriteLog("Startup", "Scripting is OK and is now running Startup.vb")
    
    SpeakClients = hs.GetInstanceList
    If SpeakClients Is Nothing Then SpeakClients = ""
    
    If String.IsNullOrEmpty(SpeakClients.Trim) Then
    hs.WriteLog("Startup", "(Startup.vb script) No speaker clients detected, waiting up to 30 seconds for any to connect...")
    ' There are no connected speaker clients, so let's wait 30 seconds
    ' (the default re-connect interval for a speaker client)
    ' to see if we can get one connected, otherwise the two speak
    ' commands below will not be heard.
    hs.WaitSecs(1)
    Dim Start As Date = Now()
    Do
    SpeakClients = hs.GetInstanceList
    If SpeakClients Is Nothing Then SpeakClients = ""
    If String.IsNullOrEmpty(SpeakClients.Trim) Then
    hs.WaitSecs(1)
    Else
    Exit Do
    End If
    Loop Until Now.Subtract(Start).TotalSeconds > 30
    End If
    
    
    ' Speak - comment the next line if you do not want HomeSeer to speak at startup.
    ' hs.Speak("Home Seer Restarted - House Ready", True)
    
    ' speak the port the web server is running on
    'Dim port As String = hs.GetINISetting("Settings", "gWebSvrPort", "")
    'If port <> "80" Then
    'hs.Speak("Web server port number is " & port)
    'End If
    
    ' You may add your own commands to this script.
    ' See the scripting section of the HomeSeer help system for more information.
    ' You may access help by going to your HomeSeer website and clicking the HELP button,
    ' or by pointing your browser to the /help page of your HomeSeer system.
    
    'Call Additonal scripts - jlg 5/7/2019
    '
    hs.RunScript("Easter.vb",True,True) 'Find out the date for easter, Create a Global variable and write the date to it.
    'hs.RunScript("Seasons.vb",True,True)'Determine the Start of each seson for the current year.
    
    '-------------------------------------------
    'JLG - 5/23/2019
    'Create the global variables needed
    '7-3-2019 Add Watering Global variables.
    '-------------------------------------------
    
    errst = hs.CreateVar("zntime")
    errst = hs.CreateVar("znflwrtime")
    errst = hs.CreateVar("soakcycles")
    errst = hs.CreateVar("Instances")
    
    
    
    errst = hs.SaveVar("zntime",ZoneTime)
    errst = hs.SaveVar("znflwrtime",FlowerTime)
    errst = hs.SaveVar("soakcycles",sCycles)
    errst = hs.SaveVar("Instances", "Initialization")
    
    End Sub
    Hope this helps

    Leave a comment:


  • Pocster
    replied
    Originally posted by AllHailJ View Post
    I generally create all of my global variables in startup.vb. This way I know they are available immediately when the system reboots. It allows you to use the global variables in hstouch. I also read all my .ini files and populate the data on startup.
    Not heard of that .
    So startup.vb is automatically started at boot ? Do I need to specify / configure it somewhere ??

    Cheers

    Leave a comment:


  • AllHailJ
    replied
    I generally create all of my global variables in startup.vb. This way I know they are available immediately when the system reboots. It allows you to use the global variables in hstouch. I also read all my .ini files and populate the data on startup.

    Leave a comment:


  • Pocster
    replied
    Originally posted by jon00 View Post

    No, it is only created once and ignored if already created. It stops errors if you try to save to a Global Variable that does not exist.
    Thanks !

    Leave a comment:


  • jon00
    replied
    Originally posted by Pocster View Post

    Just 1 question
    The global var will get created multiple times . Presumably creating the same variable just creates one iteration of it ? . Not sure how global vars ‘exist’ in HS .
    No, it is only created once and ignored if already created. It stops errors if you try to save to a Global Variable that does not exist.

    Leave a comment:


  • Pocster
    replied
    Originally posted by jon00 View Post
    What you describe all seems messy....however to answer you question.

    You can't just terminate a script from another. To do what you want, you need to monitor a variable which is available to both scripts. This could be a global variable, an ini file entry or a virtual device.

    The following code demonstrates using global variables:

    Code:
    Sub Main(ByVal Parm As Object)
    
    
    hs.CreateVar("stopme")
    hs.SaveVar("stopme","0")
    
    ' You first code here
    
    ' Wait 5 secs
    For P As Integer = 1 To 50
        If hs.GetVar("stopme") = "1" Then Exit Sub
        hs.WaitSecs(0.1)
    Next
    
    ' Your second code here
    
    End Sub
    Your second script:

    Code:
    Sub Main(ByVal Parm As Object)
    
    hs.CreateVar("stopme")
    
    'Use this line to exit other script
    hs.SaveVar("stopme", "1")
    End Sub
    Hopefully you can see that if you set the global variable "stopme" to "1", it will cause the other script to jump out of its 5 second loop and exit the script.
    Just 1 question
    The global var will get created multiple times . Presumably creating the same variable just creates one iteration of it ? . Not sure how global vars ‘exist’ in HS .

    Leave a comment:


  • Pocster
    replied
    Originally posted by jon00 View Post
    What you describe all seems messy....however to answer you question.

    You can't just terminate a script from another. To do what you want, you need to monitor a variable which is available to both scripts. This could be a global variable, an ini file entry or a virtual device.

    The following code demonstrates using global variables:

    Code:
    Sub Main(ByVal Parm As Object)
    
    
    hs.CreateVar("stopme")
    hs.SaveVar("stopme","0")
    
    ' You first code here
    
    ' Wait 5 secs
    For P As Integer = 1 To 50
        If hs.GetVar("stopme") = "1" Then Exit Sub
        hs.WaitSecs(0.1)
    Next
    
    ' Your second code here
    
    End Sub
    Your second script:

    Code:
    Sub Main(ByVal Parm As Object)
    
    hs.CreateVar("stopme")
    
    'Use this line to exit other script
    hs.SaveVar("stopme", "1")
    End Sub
    Hopefully you can see that if you set the global variable "stopme" to "1", it will cause the other script to jump out of its 5 second loop and exit the script.
    Yep my approach was messy . That’s due to being new to HS .
    Your solution is much cleaner and neater .
    Will use it - can’t see any issues with it .

    Much appreciated! 👍

    Leave a comment:


  • AllHailJ
    replied
    I do something very similar to this for watering my lawn in cycles. It will kill the script and then I run another event to shutoff the zone that is watering. Jon's is a more elegant solution but also more resource intensive.

    Code:
     Do While dvCode < 10
    KillMe = hs.DeviceValue(451)   '<---------- The boolean device to control the script running
    If KillMe = 1 Then 'Told to Die
    hs.SetDeviceValueByRef(451, 0, True) 'Reset the Device so Watering can run
    hs.CounterReset("Zone Counter") 'Set the Zone Number to 0
    hs.CounterReset("WateringCycle") 'Set the Cycle Number to 0
    hs.TriggerEvent("Shut Off Timers")
    Exit Sub 'Exit the Subroutine
    End If
    hs.TimerReset("Watering Time")
    dCode = 7681 + dvCode
    CallCAPI(dvRef, dString(dvCode)) 'Turn on for xx minutes
    hs.SetDeviceValuebyRef(dvRef, dCode, True) ' Set the Device value
    hs.WaitSecs(ZoneTime)
    CallCAPI(dvRef, "Off") 'Turn off
    hs.SetDeviceValuebyRef(dvRef, 0, True) ' Show it as Off
    hs.WaitSecs(2)
    dvCode = dvCode + 1
    hs.CounterIncrement("Zone Counter")
    Loop

    Leave a comment:


  • jon00
    replied
    What you describe all seems messy....however to answer you question.

    You can't just terminate a script from another. To do what you want, you need to monitor a variable which is available to both scripts. This could be a global variable, an ini file entry or a virtual device.

    The following code demonstrates using global variables:

    Code:
    Sub Main(ByVal Parm As Object)
    
    
    hs.CreateVar("stopme")
    hs.SaveVar("stopme","0")
    
    ' You first code here
    
    ' Wait 5 secs
    For P As Integer = 1 To 50
        If hs.GetVar("stopme") = "1" Then Exit Sub
        hs.WaitSecs(0.1)
    Next
    
    ' Your second code here
    
    End Sub
    Your second script:

    Code:
    Sub Main(ByVal Parm As Object)
    
    hs.CreateVar("stopme")
    
    'Use this line to exit other script
    hs.SaveVar("stopme", "1")
    End Sub
    Hopefully you can see that if you set the global variable "stopme" to "1", it will cause the other script to jump out of its 5 second loop and exit the script.

    Leave a comment:


  • Pocster
    replied
    To further confused myself!!

    If a script is running and it does

    Wait 5 seconds

    During that time another event\script disables the original script ( as it's still waiting 5 seconds ); after the wait if I do

    isScriptRunning will it be yes or no?. It IS still running but it has been disabled.....

    Just thinking if I stuck isScriptRunning after the wait ( and it was false ) then I could exit the script early....

    Leave a comment:


  • Pocster
    started a topic Terminating a script

    Terminating a script

    Hi

    I have a script that ...

    does stuff :-))

    Waits 5 seconds

    does stuff...

    I wish to 'kill' the script based on an event not disable it - but terminate it immediately.
    If I just disable it ; it could still be at the wait for 5 seconds and hence continues the script.

    If that's not possible any other suggestions for ending an event script welcome.

    Cheers
Working...
X