Announcement

Collapse
No announcement yet.

[VB.NET] updateEvent

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

    [VB.NET] updateEvent

    What this does:
    ------------------------------
    Update event basically is the MAIN script I use to coordinate everything in my house. updateevent is generic and as stated on the third line, takes in a delay time (default is minutes), device code (no names!), if you want it to be set to ON or OFF, then a Yes/No if the delay time should be seconds or not.

    I would like to add in delayed event triggering and DIM control. That is fire off an event after so much time has passed. Or instead of just triggering ON/OFF trigger to a DIM level.

    Right now I get around it by triggering a UPB link for the dim level I want. So, that has NOT been a priority.

    Basically the steps to the code are:

    • you trigger it
    • the script looks for anything that is in existence for the device. Remove that if it exists
    • Then create a "NewTimeEvent" for that device.
    • If you say seconds only, OR if the time delay you ask for is less than 4 minutes, create the NewTimeEvent much later in time, however make a tigger event for the NewTimeEvent, which will trigger the NewTimeEvent in seconds of resolution. Best explained by the comment in the code:

    " ' As an event cannot trigger itself in seconds of resolution
    ' while it would be noticable, just trigger the event with a
    ' seconds resolute function"
    At least this is how my experimentation seemed to show the events being fired.
    Basically, if you triggered update event at 11:30:32, then added 4 minutes to "NOW" to trigger. It would not trigger at 11:34:32, it would trigger at 11:34:00. So, I fixed that. For anything that was less than 4 minutes, the offset did not really matter in the grand scheme of things...so I do not bother with the helper event.

    Code:
    ' par should be of the format
    ' Delay Time (in minutes), device Code, On/Off, Yes/No
    
    'UPB Link Values
    'UNKNOWN = 0
    'ACTIVATE = 1
    'DEACTIVATE = 2
    'FADE ON = 3
    'FADE OFF = 4
    'SNAP ON = 5
    'SNAP OFF = 6
    'DIM to 10% = 7
    'DIM to 20% = 8
    'DIM to 30% = 9
    'DIM to 40% = 10
    'DIM to 50% = 11
    'DIM to 60% = 12
    'DIM to 70% = 13
    'DIM to 80% = 14
    'DIM to 90% = 15
    'BLINK = 16
    'LAST ON LEVEL = 17
    
    Sub updateEvent(ByVal par as object)
       
       dim delaySecs,delayMins, ev, eventName, devCode, onOff, onlySeconds
    
       delaySecs     = 0
    
       delayMins     = hs.stringitem(par, 1, "|")
       devCode       = hs.stringitem(par, 2, "|")
       onOff         = ucase(hs.stringitem(par, 3, "|"))
       onlySeconds   = ucase(hs.stringitem(par, 4, "|"))
    
       'hs.writelog("Info", par)
       'hs.writelog("Info", delayMins)
       'hs.writelog("Info", devCode)
       'hs.writelog("Info", onOff)
       'hs.writelog("Info", onlySeconds)
    
       ev = hs.GetDeviceByRef(hs.GetDeviceRef(devCode))
       eventName = ev.location & " " & ev.name & " " & onOff & " (delayed event)"
    
       ' if we need to remove the event ONLY
       if(UCASE(onlySeconds) = "REM") then
          hs.DeleteEvent(eventName)
          hs.DeleteEvent(eventName & " (delayed event)")
          'hs.writelog("Test", "deleted events!")
          'hs.writelog("Test", "Deleting: " & eventName & " (delayed event)")
          exit sub
       end if
    
       if(UCASE(onlySeconds) = "YES") then
          delaySecs = delayMins
          delayMins = 30
       else
          ' As an event cannot trigger itself in seconds of resolution
          ' while it would be noticable, just trigger the event with a
          ' seconds resolute function
          if(delayMins < 4) then
          
             delaySecs = delayMins * 60
             delayMins = 30
          
          end if
       end if
       
       if hs.EventExists(eventName) then
         hs.DeleteEvent(eventName)
       end if
    
       if hs.EventExists(eventName & " (delayed event)") then
         hs.DeleteEvent(eventName & " (delayed event)")
       end if
    
       hs.NewTimeEvent(eventName, _
          timevalue(DateAdd("n",delayMins,Now)),datevalue(DateAdd("n",delayMins,Now)), _
          1,1,1,1,1,1,1,devCode & ":" & onOff,true,"","Delayed Actions")
    
       if(delaySecs > 0) then
          hs.DelayTrigger(delaySecs, eventName)
    
          ev = hs.GetEventEx(eventName & " (delayed event)")
          '16384 turns OFF logging
          '16400 turns off logging and Delete After Execute
          ev.misc = 16400
    
          hs.EnableEvent(eventName & " (delayed event)")
    
       end if
       ev = hs.GetEventEx(eventName)
       '16384 turns OFF logging
       '16400 turns off logging and Delete After Execute
       ev.misc = 16400
       hs.EnableEvent(eventName)
    
    End Sub
    Copy the above code into a file updateevent.vb

    Example use:
    Create an event that calls the script:

    PHP Code:
    NameFan on by Door 
    PHP Code:
    Trigger:
    Device Top Master Bathroom Door changed to status *Any*  
    WHEN 
    Everyday 
    PHP Code:
      Run script:  updateEvent.vb("masterBathLighting","doorChange"
    Then, add a sub to handle the logic. You can do something like the below (which should compile and run for you, just change the variables...this is ONLY a snippet of the control code for the master bathroom). What is nice about this, you can GROUP logic by the parameter. So, ONLY when the door is changed, does the above parameter get used.

    That is this line:
    if(param = "doorChange") then

    Basically, you could have a whole host of those, one for EACH sensor in the room. In my bathrooms, I have a few motion sensors, pressure pad, and the light switches themselves. I make ONE event per trigger device, and make them trigger the script.

    So, the REAL sub for this has:
    if(param = "doorChange") then
    if(param = "showerMotion") then
    if(param = "resetOccupied") then
    if(param = "sinkPad") then

    The reset is only used to reset a few counters (you need multiple triggers in the shower to determine that someone is IN the shower).

    Code:
    sub masterBathLighting(ByVal param as String)
    
    Dim mBathDoor
    Dim mBathLight
    Dim mBathFan
    Dim mBathBrightTo100
    Dim myCount
    Dim longTime
    Dim showerLongTime
    
    '*************************************
    '*     Setup Variables               *
    '*************************************
    
    mBathDoor            = hs.GetDeviceCode("Master Bathroom Door")
    mBathLight           = hs.GetDeviceCode("Master Bathroom Overhead Light")
    mBathFan             = hs.GetDeviceCode("Master Bathroom Fan")
    mBathBrightTo100     = hs.GetDeviceCode("UPB Device Link Master Bathroom Bright to 100 Link")
    myCount              = getMyVar("showerCount")
    longTime             = 10
    showerLongTime       = 30
    
    if(param = "doorChange") then
       ' off is door closed
       if (hs.isOff(door)) then
          updateEvent(longTime & "|" & overheadLight & "|" & "OFF")
       else
          updateEvent(shortTime & "|" & overheadLight & "|" & "OFF")
       end if
    end if
    
    end sub
    So, IF the door is closed, create a timed event that waits a "longTime", if the door is opened, then create a timed event for "shortTime". In either case, if there is a timed event for overheadLight, remove it first, THEN create the new timed event.

    Discussion goes here:
    http://forums.homeseer.com/showthread.php?p=985608
    Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

    #2
    V2.0
    Added delayed Event and delayed Script launching:

    Code:
     
    'subtract 13 from error value to find proper line in code
    ' V2.0 - (C) 2011 D. Rozwood; released under GPL
    
    'UPB Link Values
    'UNKNOWN = 0
    'ACTIVATE = 1
    'DEACTIVATE = 2
    'FADE ON = 3
    'FADE OFF = 4
    'SNAP ON = 5
    'SNAP OFF = 6
    'DIM to 10% = 7
    'DIM to 20% = 8
    'DIM to 30% = 9
    'DIM to 40% = 10
    'DIM to 50% = 11
    'DIM to 60% = 12
    'DIM to 70% = 13
    'DIM to 80% = 14
    'DIM to 90% = 15
    'BLINK = 16
    'LAST ON LEVEL = 17
    
    ' par should be of the format:
    ' Delay Time (in minutes), 
    '           device Code/Name Of Event/eventName;scriptName.vb("subToExecute","parameters"), 
    '                    On/Off/Dim/Event/Script,    <-- type of delay to make
    '                            [Yes/No/REM],       <-- Yes it is seconds, No, is minutes (default), Remove the event
    '                             [DIM Value]
    Sub updateEvent(ByVal par as object)
       
       dim delaySecs,delayMins, ev, eventName, devCode, theCommand, onlySeconds, dimValue, myScriptCommand
       delaySecs       = 0
       delayMins       = hs.stringitem(par, 1, "|")
       devCode         = hs.stringitem(par, 2, "|")
       theCommand      = ucase(hs.stringitem(par, 3, "|"))
       onlySeconds     = ucase(hs.stringitem(par, 4, "|"))
       dimValue        = ucase(hs.stringitem(par, 5, "|"))
       myScriptCommand = ""
       'hs.writelog("Info", par)
       'hs.writelog("Info", delayMins)
       'hs.writelog("Info", devCode)
       'hs.writelog("Info", theCommand)
       'hs.writelog("Info", onlySeconds)
       'exit sub
       'if(theCommand = "ON" OR theCommand = "OFF" OR theCommand = "DIM" OR theCommand = "REM" OR theCommand = "EVENT") then
       
          if(theCommand = "EVENT") then
             eventName = devCode
          elseif(theCommand = "SCRIPT") then
             eventName = hs.stringitem(devCode, 1, ";")
          else
             ev = hs.GetDeviceByRef(hs.GetDeviceRef(devCode))
             eventName = ev.location & " " & ev.name & " " & theCommand & " (delayed event)"         
          end if
          
          ' if we need to remove the event ONLY
          ' test to see if this works to remove EVENT only calls
          if(UCASE(onlySeconds) = "REM") then
             hs.DeleteEvent(eventName)
             hs.DeleteEvent(eventName & " (delayed event)")
             'hs.writelog("Test", "deleted events!")
             'hs.writelog("Test", "Deleting: " & eventName & " (delayed event)")
             exit sub
          end if
       
          if(UCASE(onlySeconds) = "YES") then
             delaySecs = delayMins
             delayMins = 30
          else
             ' As an event cannot trigger itself in seconds of resolution
             ' while it would be noticable, just trigger the event with a
             ' seconds resolute function
             if(delayMins < 4) then
             
                delaySecs = delayMins * 60
                delayMins = 30
             
             end if
          end if
          
          if hs.EventExists(eventName) then
             hs.DeleteEvent(eventName)
          end if
       
          if hs.EventExists(eventName & " (delayed event)") then
             hs.DeleteEvent(eventName & " (delayed event)")
          end if
          
          if(theCommand = "DIM") then
             theCommand = "dimto-" & dimValue
          end if
          if(theCommand = "SCRIPT") then
             theCommand = ""
             myScriptCommand = hs.stringitem(devCode, 2, ";")
          end if
          
          if(theCommand = "EVENT") then
             if(delaySecs > 0) then
                delaySecs = delaySecs
             else
                delaySecs = delayMins * 60
             end if
             hs.DelayTrigger(delaySecs, eventName)
       
             ev = hs.GetEventEx(eventName & " (delayed event)")
             '16384 turns OFF logging
             '16400 turns off logging and Delete After Execute
             ev.misc = 16400
             hs.EnableEvent(eventName & " (delayed event)")
          
          else
    'hs.AddAction(hs.GetEventRefByName("Zone" & i & " Play Pause Song"),5,"dwr_winamp.vb(""PlayPause"",""" & i & """)")
             hs.NewTimeEvent(eventName, _
                timevalue(DateAdd("n",delayMins,Now)),datevalue(DateAdd("n",delayMins,Now)), _
                1,1,1,1,1,1,1,devCode & ":" & theCommand,true,myScriptCommand,"Delayed Actions")
             if(delaySecs > 0) then
                hs.DelayTrigger(delaySecs, eventName)
          
                ev = hs.GetEventEx(eventName & " (delayed event)")
                '16384 turns OFF logging
                '16400 turns off logging and Delete After Execute
                ev.misc = 16400
                hs.EnableEvent(eventName & " (delayed event)")
          
             end if
             ev = hs.GetEventEx(eventName)
             '16384 turns OFF logging
             '16400 turns off logging and Delete After Execute
             ev.misc = 16400
             hs.EnableEvent(eventName)
          end if
       'end if
          
    End Sub
    Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

    Comment

    Working...
    X