Announcement

Collapse
No announcement yet.

Help with converting security script to HS V2

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

  • Help with converting security script to HS V2

    Firstly I must appologise as I cannot remember who wrote this script.

    This script was written for HSV1 and worked very well. I have tried to alter it to work with HS2 without success. If anyone would like to have a go please have a look.
    Code:
    ' SecurityLights.txt
     
    Public EventONName
    Public EventOFFName
    Public SndX10
    Public DebugMode
    Public CurrDate
    Public CurrTime
    Public MaxStartDelay
    Public MinOn
    Public MaxOn
    Sub Main()
     
    MiscBitsON = &H4010	 ' Enables Do not log, delete after trigger
    CurrDate = Date 
    CurrTime = Time
     
    EventONName = " On Security Event"
    EventOffName = " Off Security Event"
    MinOn = 3	 ' Minumum time the light will be on
    MaxOn = 10	 ' Maximum time the light will be on
    MaxStartDelay = 15	 ' Maximum time to wait to turn on the light from when the event is run.
     
    hs.nolog = FALSE
    DebugMode = FALSE
    '*************
    'Put the device codes of the lghts you want to control here
    '*************
    Const X10Devs = "D14,G2,C6"	
    '*************
     
    Randomize ' Starts the radomizer
    SndX10 = Split(X10Devs,",")
    RandomDevice = CreateRndDev(ubound(SndX10)+1) ' here we get the random device we are going to turn on then off.
     
    RndEventOnTime = GetOntime() 	' we get the time the device will turn on.
    If RandomDevice = "TooManyRetries" then exit sub 'If we just can't find an available device we bail out.
    Set dev=hs.getdevice(hs.deviceexists(SndX10(RandomDevice)))
    DebugIt("Random device selected " & dev.name)
    OnEventName = dev.name & " On Security Event" ' This is the name of the ON event
    OffEventName = dev.name & " Off Security Event" ' This is the name of the OFF event
    ' ************** ON EVENT *********************
    'Here is where we actaully create the events, this is the ON event.
     
    i=hs.NewEventGetRef(OnEventName)
    set ev=hs.geteventByRef(i)
    ' i=hs.NewEvent(OnEventName)	' We create the new event
    ' set ev=hs.GetEvent(i)
    ev.ev_abs_time = 0	 ' Sets the event mode to trigger at an absulte time
    ev.misc = MiscBitsON	 ' Here we enable Do not log and delete after trigger
     
    ev.ev_time = RndEventOnTime	' Here we sent the trigger time
    ev.ev_date = CurrDate	 ' Here we sent the trigger date
     
    set df=hs.NewDevFunc	 ' Here we get the device info to add
    df.devid = dev.ref	 ' get the device ref code
    df.func = 2 'on	 ' here is the actual ON command
    ev.ev_devices.add df	 ' we add this device to the event.
     
    Debugit("New Event created " & OnEventName)
     
    ' ************** OFF EVENT *********************
    ' Here is where we actaully create the events, this is the Off event.
    i=hs.NewEventGetRef(OffEventName)
    set ev=hs.geteventByRef(i)
    ' i=hs.NewEvent(OffEventName)	' We create the new event
    ' set ev=hs.GetEvent(i)
    ev.ev_abs_time = 0	 ' Sets the event mode to trigger at an absulte time
    ev.misc = MiscBitsON	 ' Here we enable Do not log and delete after trigger
    ev.ev_time = GetOfftime(RndEventOnTime) ' Here we sent the trigger time (offset from teh on time)
    ev.ev_date = CurrDate	 ' Here we sent the trigger date
     
    set df=hs.NewDevFunc	 ' Here we get the device info to add
    df.devid = dev.ref	 ' get the device ref code
    df.func = 3 'off	 ' here is the actual OFF command
    ev.ev_devices.add df	 ' we add this device to the event.
    Debugit("New Event created " & OnEventName)
     
    end sub
    '************ This function we get the random device from the lost above.
    Function CreateRndDev(Data) ' Here is where we get the random X10 device 
    UseDevCode = FALSE
    LoopCounter = 0
    Do while UseDevCode = FALSE	 ' We loop until we get a valie device to use.
    RndDev = int(Data * rnd)	' Here we pick the random X10 device
    Set dev=hs.getdevice(hs.deviceexists(SndX10(RndDev)))
    ev_count = hs.EventCount	' Total of all events
    for i = 1 to ev_count	 ' Here we cycle through all events
    set ev=hs.geteventByRef(i)
    ' set ev=hs.getevent(i)
    hs.speak ev.name
    if ev.name = dev.name & EventONName then ' Do we already have an ON event for this device
    	UseDevCode = FALSE
    	Debugit("Can't use this code " & dev.name)
    	exit for	' Exit and look for another random device code
    elseif ev.name = dev.name & EventOFFName then ' Do we already have an OFF event for this device
    	UseDevCode = FALSE
    	Debugit("Can't use this code " & dev.name)
    	exit for	' Exit and look for another random device code
    End If
    UseDevCode = TRUE	' This device code is not used by us previously, OK to use.
    next
    LoopCounter = LoopCounter + 1
    If LoopCounter >= (Data*5) then ' This will prevent an endless loop, it will try 5 * the number of devices tries
    CreateRndDev = "TooManyRetries"
    exit do	 ' Here we just give up trying
    end if
    Loop
    CreateRndDev = RndDev
    End Function
    '************** This function we calculate the random on/start time
    Function GetOntime()
    OnTime = int((rnd * MaxStartDelay))
    GetOntime = timeserial(hour(CurrTime),(minute(CurrTime)+OnTime),second(CurrTime))
    End Function
    '************** This function we calculate the random oFF/start time, we use the other event start time as a basis (data)
    Function GetOfftime(Data)
    OffTime = int((MaxOn-MinOn) * rnd) + MinOn
    GetOfftime = timeserial(hour(Data),(minute(Data)+OffTime),second(Data))
    End Function
    ' ************** Debug routine, logs all messages to the HS ah.log file
    Function DebugIt(data) ' Debugging - to enable set DebugMode = TRUE at the top.
    If DebugMode = TRUE then hs.WriteLog "SecurityLights.txt",Data
    End Function
    sigpic
    A founder member of "The HA Pioneer Group" otherwise known as the "Old farts club!"
    HomeSeer 3 V 3.0.0.318 with ALEXA (AKA Amazon Echo)
    BL Radar V2.0.217; BLRF V2.0.85.0; HS Touch V3.0.0.1034 X10 V3.0.0.42; PHLocation V3.0.0.38; Zwave V3.0.1.110; X10 V3.0.0.42
    BLBackup V2.0.39;ImperiHome3.0.0.20 All on Windows XP Pro

  • #2
    Gogs,

    Is this event supposed to turn on/off lights randomly? I don't recognize the one you've quoted, but you're right the event creation routines are much different in HS2.

    I use the script below which Jim Doolittle wrote. I have it run every 7 minutes when we're on holiday, and it apparently does a fine job.

    --David

    Code:
      
    '  Script: my_security.txt
    '  Version: 1.15
    '  Author: Jim Doolittle
    '
    '  Description: A script that randomly changes devices for giving your house a lived in look.
    '
    '  Instructions: 
    '
    ' 1.  Create event in HomeSeer that runs this script every x minutes. Set additional 
    '	 conditions for the event such as if you only want it to run at night. 
    '  2. Specify value for a constant that limits the maximum number of lights that will 
    '	  be on at one time (maxON). 
    '  3. Specify list of dimmable and non-dimmable devices (DimBrightList & OnOffList).
    '	  Set appropriate list to empty string if you do not want devices in that list.
    '  4. There are two optional "scenes" that can be used to cause a group of lights to 
    '	  go on and off sequentially so that it looks like someone is walking through the 
    '	  house. These scenes trigger when the first device in the scene list is the device
    '	  randomly selected by the script and the randomly selected command is NOT "off".
    '	  The first device needs to be specified in either the dimmable or the non-dimmable
    '	  device list. The scene-effect will temporarily override the maxON but script will 
    '	  eventually cause devices to turn off until maxON is met. I was going to fix this, 
    '	  but realized that happens in real life (i.e., kids leave more lights on than they 
    '	  need and that you want) :).
    '
    '  Revision History:
    '
    '  1.15 - Fixed bug where I had mixed arrDevs with arrCmds
    '  1.14 - Added GetRandomNumber function
    '  1.13 - Still more bugs. Good thing I am not charging anything for this.
    '  1.12 - Fixed more bugs.
    '  1.11 - Noted script instructions that first device of scene must be in one of the two device lists.
    '		   Fixed maximum number of lights on bug.
    '  1.10 - Added max number of lights that can be on.
    '		   Distinguished between dimmable and non-dimmable lights.
    '		   Added "scene-effect" for list of lights that go on and off sequentially
    '		   so that it looks like someone is walking through the house.
    '  1.00 - Initial version.
    '*** USER CONSTANTS ***
    Const bUseLog = True
    ' Maximum number of lights to be ON
    Const maxON = 5
    ' List of dimmable devices to control  (set to empty string to disable dim/bright commands)
    Const DimBrightList = "B5,F5,D1,B1,K1,O15,D2,K2,B11"
    ' List of non-dimmable devices to control
    Const OnOffList = "B5,F5,D1,B1,K1,O15,D2,K2,B11"
    ' Scene 1 (set to empty string to disable)
    Const Scene1 = "B5,F5,D1"
    ' Scene 2 (set to empty string to disable)
    Const Scene2 = "D2,K1,S15"
    ' Number of seconds between activation of devices in the scene
    Const scene_secs = 4
    '*******************************
    Sub ProcessScene(aScene)
     Dim arrDevs
     Dim upperbound
     arrDevs = Split(aScene, ",", -1)
     upperbound = UBound(arrDevs)
     For i = 0 to upperbound - 1
      hs.ExecX10 arrDevs(i), "on", 0
      hs.WaitSecs scene_secs
      If upperbound > 0 Then hs.ExecX10 arrDevs(i+1), "on", 0
      hs.WaitSecs 1
      hs.ExecX10 arrDevs(i), "off", 0
     Next
     If bUseLog Then hs.WriteLog "Info", "Security Script has executed scene " & aScene
    End Sub
    Function GetRandomNumber(lowerbound,upperbound)
     Randomize
     GetRandomNumber = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    End Function
    Sub Main()
     Dim AllDevicesList
     Dim upperbound
     Dim lowerbound
     Dim arrCmds(3)
     Dim arrDevs
     Dim Dev
     Dim Cmd
     Dim Level
     Dim i
     Dim on_count
     AllDevicesList = ""
     If Len(DimBrightList) > 0 Then AllDevicesList = AllDevicesList & DimBrightList
     If Len(OnOffList) > 0 Then 
      If Len(AllDevicesList) > 0 Then AllDevicesList = AllDevicesList & ","
      AllDevicesList = AllDevicesList & OnOffList
     End If
     If Len(AllDevicesList) = 0 Then Exit Sub
     arrCmds(0) = "on"
     arrCmds(1) = "off"
     arrCmds(2) = "dim"
     arrCmds(3) = "bright"
     ' randomly select a command
     Cmd = arrCmds(GetRandomNumber(LBound(arrCmds),UBound(arrCmds)))
     ' if command is going to turn a light on at some level, check to 
     ' see if maximum number of lights ON is reached and, if so, 
     ' change command to "off"
     If Cmd <> "off" Then
      arrDevs = Split(AllDevicesList, ",", -1)
      upperbound = UBound(arrDevs)
      on_count = 0
      For i = 0 to upperbound
       If hs.IsOn(arrDevs(i)) Then on_count = on_count + 1
       If on_count >= maxOn Then
    	If bUseLog Then hs.WriteLog "Info", "Security Script has reached maximum number of lights on"
    	Cmd = "off"
    	Exit For
       End If
      Next
     End If
     If (Cmd = "on") Or (Cmd = "off") Then
      ' both dimmable and non-dimmable lights respond to "on" or "off"
      arrDevs = Split(AllDevicesList, ",", -1)
      Dev = arrDevs(GetRandomNumber(LBound(arrDevs),UBound(arrDevs)))
      Level = 0
     ElseIf Len(DimBrightList) > 0 Then
      ' only dimmable lights respond to "dim" or "bright"
      arrDevs = Split(DimBrightList, ",", -1)
      Dev = arrDevs(GetRandomNumber(LBound(arrDevs),UBound(arrDevs)))
      ' randomly select a dim/bright level betwen 10 and 100
      Randomize
      Level = GetRandomNumber(1,10) * 10
     End If
     If Dev = hs.StringItem(Scene1,1,",") and (Cmd <> "off") Then 
      ProcessScene(Scene1)
     ElseIf Dev = hs.StringItem(Scene2,1,",") and (Cmd <> "off") Then
      ProcessScene(Scene2)
     Else
      hs.ExecX10 Dev, Cmd, Level
      If bUseLog Then 
       If Level = 0 Then
    	hs.WriteLog "Info", "Security Script has executed " & Cmd & " command for " & Dev
       Else
    	hs.WriteLog "Info", "Security Script has executed " & Cmd & " command at " & Level & "% for " & Dev
       End If
      End If
     End If
    End Sub

    Comment


    • #3
      Originally posted by dkindred
      Gogs,

      Is this event supposed to turn on/off lights randomly? I don't recognize the one you've quoted, but you're right the event creation routines are much different in HS2.

      I use the script below which Jim Doolittle wrote. I have it run every 7 minutes when we're on holiday, and it apparently does a fine job.

      --David
      Yes thats what it did under HS V1. Thanks for posting Jim's script, I'll give it a try.
      sigpic
      A founder member of "The HA Pioneer Group" otherwise known as the "Old farts club!"
      HomeSeer 3 V 3.0.0.318 with ALEXA (AKA Amazon Echo)
      BL Radar V2.0.217; BLRF V2.0.85.0; HS Touch V3.0.0.1034 X10 V3.0.0.42; PHLocation V3.0.0.38; Zwave V3.0.1.110; X10 V3.0.0.42
      BLBackup V2.0.39;ImperiHome3.0.0.20 All on Windows XP Pro

      Comment

      Working...
      X