Announcement

Collapse
No announcement yet.

How to Run an Event at a Calculated Time?

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

    How to Run an Event at a Calculated Time?

    I have created a script to calculate the time of day at which I would like to raise some Lutron shades. This time corresponds to the point at which the sun passes the back face of my house, and so it varies by day. I don't see a trigger that I can use to run this event. All of the time triggers are related to sunrise and sunset. I have Easy Trigger, but I don't see anything there either. Could I accomplish this with another script? Also, I have stored the time in question in a global variable. Is that the correct approach?

    #2
    I'm not really sure if a global variable can be used to trigger an event, but you should be able to create a device that can be used as a trigger. Maybe call it something like "Shade Control" with values for Up and Down. Use you're script to update that device value, and then set your events to act based on that device change.

    Comment


      #3
      Search the HS3 online help for "EventSetTimeTrigger". That looks like it will do what you want.

      Comment


        #4
        Or create a run once event on the fly in your script ? The event will be deleted after it triggered

        Comment


          #5
          you can use easy trigger to do a daily sunrise+5 hours if you wanted to. but in order to vary with the arc of the changing seasons of the sun you may have to make a couple of different easy trigger schedules. I would think you would only need about 5 or 6 daily schedules to cover it over the course of a year. maybe even less.....

          Comment


            #6
            Here's a script snippet to add events to run at a specific times. This one creates two events, one to turn a device on at a specific time and one to turn it off.

            Code:
            Sub CreateEvent(StartTime,StartDate,Delay,DeviceName)
                dim Evref as integer
                dim Ok as Boolean
                dim CC as CAPIControl
                Dim logName As String = "Block Heater Event Creation"    'set log type for HS log
                Dim Debug As Boolean = True                'set to True if the script give you errors and it will provide additional info in the HS3 log to help troubleshoot
            
                Dim StartDateTime As Date = CDate(StartDate & " " & StartTime)
                    If Debug Then hs.writelog(logName,CStr(StartDateTime))
            
                ' create a new event
                Evref = hs.NewEventEx("Block Heater On " & Cstr(DeviceName), "Block Heaters", "Block Heaters")
                ' set the trigger time of the event
                Ok = hs.EventSetTimeTrigger(Evref, CStr(StartDateTime))
                ' set the event to delete itself after it runs
                hs.DeleteAfterTrigger_Set(Evref)
                ' add a device action to the event
                CC = hs.CAPIGetSingleControlByUse(DeviceName, ePairControlUse._On)
                hs.AddDeviceActionToEvent(Evref, CC)
                hs.EnableEventByRef(Evref)
            
                Dim StopDateTime As Date = StartDateTime.AddMinutes(Delay)
                    If Debug Then hs.writelog(logName,CStr(StopDateTime))
            
                ' create a new event
                Evref = hs.NewEventEx("Block Heater Off " & Cstr(DeviceName), "Block Heaters", "Block Heaters")
                ' set the trigger time of the event
                Ok = hs.EventSetTimeTrigger(Evref, CStr(StopDateTime))
                ' set the event to delete itself after it runs
                hs.DeleteAfterTrigger_Set(Evref)
                ' add a device action to the event
                CC = hs.CAPIGetSingleControlByUse(DeviceName, ePairControlUse._Off)
                hs.AddDeviceActionToEvent(Evref, CC)
                hs.EnableEventByRef(Evref)
            
            End Sub
            HS 4.2.8.0: 2134 Devices 1252 Events
            Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

            Comment


              #7
              Thanks for all the suggestions guys. I'll test some of this out tonight.

              sparkman Is your script snippet vbs or vb.net? I am prone to mixing the two. Is there an easy way to tell them apart?

              Comment


                #8
                socalsharky It is vb.net. Sometimes it's easy to tell based on functions, etc. that are not available in the other, but a lot of times it is not. If you need a vbs example, I should be able to provide that too as the original script I had was written in vbs and I converted it a few years ago to vb.net.
                HS 4.2.8.0: 2134 Devices 1252 Events
                Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                Comment


                  #9
                  Originally posted by sparkman View Post
                  socalsharky It is vb.net. Sometimes it's easy to tell based on functions, etc. that are not available in the other, but a lot of times it is not. If you need a vbs example, I should be able to provide that too as the original script I had was written in vbs and I converted it a few years ago to vb.net.
                  If you have it readily available, it would be instructional. I'm just getting started with scripting in HS3. Should I start off right away with vb.net, or is it OK to stick with vbs? I was brought up on BASIC about 35 years ago, but I also know a little vba and Java.

                  Comment


                    #10
                    I would go straight to vb.net at this point. I converted all my scripts to vb.net a few years ago and most examples you'll see these days are vb.net. Let me know if you still want me to post the vbs version.



                    EDIT: I just checked my archives and the only vbscript version I have of this script was the one for HS2. I must have converted it to vb.net right away when I migrated from HS2 to HS3.
                    HS 4.2.8.0: 2134 Devices 1252 Events
                    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                    Comment


                      #11
                      Originally posted by sparkman View Post
                      I would go straight to vb.net at this point. I converted all my scripts to vb.net a few years ago and most examples you'll see these days are vb.net. Let me know if you still want me to post the vbs version.



                      EDIT: I just checked my archives and the only vbscript version I have of this script was the one for HS2. I must have converted it to vb.net right away when I migrated from HS2 to HS3.
                      Thanks for checking sparkman. How did you post that scrollable window with your code? Very cool. Would you mind looking at what I have so far in vbs?

                      Comment


                        #12
                        Originally posted by socalsharky View Post

                        Thanks for checking sparkman. How did you post that scrollable window with your code? Very cool. Would you mind looking at what I have so far in vbs?
                        No problem. If you go into the Advanced Editor (A icon), then there are a lot more options and there's a button with a # sign on it which will create the tags for showing the code. Paste your code between the two tags and then it should show that way.
                        HS 4.2.8.0: 2134 Devices 1252 Events
                        Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                        Comment


                          #13
                          Originally posted by sparkman View Post

                          No problem. If you go into the Advanced Editor (A icon), then there are a lot more options and there's a button with a # sign on it which will create the tags for showing the code. Paste your code between the two tags and then it should show that way.
                          Here is the code. I've got lots of lines to post values and messages to the log as part of the debugging process.

                          Code:
                          Sub Main()
                          
                            Dim SolarNoon
                            Dim Sunrise
                            Dim Sunset
                            Dim DayLength
                            Dim SunAt153
                            Dim Offset
                          
                            SolarNoon=hs.Solarnoon
                            Sunrise=hs.SunriseDt
                            Sunset=hs.SunsetDt
                          
                            DayLength=(Sunset-Sunrise)*24
                            'DayLength in hours and Offset in seconds
                            hs.writelog "msg","Length of Day is " & DayLength & " hrs"
                            Offset=(4.764-0.3059*DayLength)*3600
                            hs.writelog "msg","Offset is " & Offset & " seconds"
                            SunAt153=DateAdd("s", -Offset, SolarNoon)
                            hs.writelog "msg","Sun is at AZ 153 at " & SunAt153
                            'hs.writelog "msg","Sun is at AZ 153 at " & FormatDateTime(SunAt153, vbLongTime)
                            hs.writelog "msg","Solar Noon is at " & SolarNoon
                          
                          End Sub

                          Comment


                            #14
                            Looks like you are using vbscript so far. One thing with vbscript is that it is very forgiving with allowing operations on variables that are different types. VB.NET is not forgiving at all, so declaring variables and converting them to other types is important. Below is the equivalent VB.NET version. I also like using a "Debug" variable that I set to True when debugging the script, and False when done debugging. That way I don't have to keep manually editing each writelog line to comment it out. I also like to use a variable for the log name entry so that I can change it one spot if I ever change it.

                            Code:
                            Sub Main(ByVal Parms As object)  
                            
                              Dim Debug As Boolean = True
                              Dim LogName As String = "msg"
                            
                              Dim SolarNoon As Date = hs.Solarnoon
                              Dim Sunrise As Date = hs.SunriseDt
                              Dim Sunset As Date = hs.SunsetDt
                              Dim DayLength As Double
                              Dim SunAt153 As Date
                              Dim Offset As Double
                            
                              If Debug Then hs.writelog(LogName,"Solar Noon is at " & SolarNoon)
                              If Debug Then hs.writelog(LogName,"Sunrise is at " & Sunrise)
                              If Debug Then hs.writelog(LogName,"Sunset is at " & Sunset)
                            
                              DayLength=DateDiff(DateInterval.Second,Sunrise,Sunset)
                              'DayLength in seconds and Offset in seconds
                              If Debug Then hs.writelog(LogName,"Length of Day is " & DayLength & " seconds")
                              Offset=4.764-0.3059*DayLength
                              If Debug Then hs.writelog(LogName,"Offset is " & Offset & " seconds")
                              SunAt153=DateAdd("s", -Offset, SolarNoon)
                              If Debug Then hs.writelog(LogName,"Sun is at AZ 153 at " & CStr(SunAt153))
                            
                            End Sub
                            HS 4.2.8.0: 2134 Devices 1252 Events
                            Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                            Comment


                              #15
                              I didn't make any progress on fully implementing this script last year, and now with a lot of free time on my hands I want to get it up and running. With sparkman 's help I have a script that will calculate the time at which the sun is past the back face of my house on any given day. Now I am trying to figure out how to use an event to trigger the blinds to raise when the Current Time > Calculated Time. I was thinking of using a virtual device to track the calculated time, but how do I compare that to the current time of day? Easy Trigger has quite a few options, but I don't see any that will work for that case.

                              Comment

                              Working...
                              X