Announcement

Collapse
No announcement yet.

HS2 to HS3 Scripting Question

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

    HS2 to HS3 Scripting Question

    I'm trying to move a very simple script that I used in HS2 to HS3. Unfortunately I'm having some difficulty with it. I've read some various threads on what I might need to change, but so far nothing has worked. The script is very simple, it is designed to progressively increase brightness of a light in small increments over a period of time. The script, as written for HS2 looks like this:

    Code:
    Sub Main()
     
    	dim time
    	dim sunrise
    	dim threshold
    	
    	threshold = TimeValue("6:45:00 AM")
    	sunrise = TimeValue(hs.Sunrise)
    	
    	if (sunrise > threshold) then
    		hs.execx10 "C3", "extended", 1,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 3,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 6,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 9,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 13,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 18,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 24,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 30,49
    		hs.WaitSecs 300
    		hs.execx10 "C3", "extended", 35,49
    		hs.WaitSecs 600
    		hs.execx10 "C3", "extended", 40,49
    	end if
     
    end sub
    I know that I can't use "hs.execx10" any more and I need to make some changes. Based on some information gleaned from this forum, I changed the file from 'wakeup.txt' to 'wakeup.vb' and the code to look like this:

    Code:
    Sub Main()
    
    	Dim X10plugin As HomeSeerAPI.PluginAccess = New HomeSeerAPI.PluginAccess(hs, "X10", "")
     
    	Dim sunrise as Date
    	Dim threshold as Date
    	
    	threshold = TimeValue("6:45:00 AM")
    	sunrise = TimeValue(hs.Sunrise)
    	
    	if (sunrise > threshold) then
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 1, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 3, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 6, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 9, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 13, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 18, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 24, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 30, 49, False})
    		hs.WaitSecs 300
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 35, 49, False})
    		hs.WaitSecs 600
    		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 40, 49, False})
    	end if
     
    end sub
    When I try to run the event now, in the log I see things like this:

    Code:
    "Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\wakeup.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases."
    and a bunch of these:

    Code:
    "Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\wakeup.vb: Method arguments must be enclosed in parentheses."
    Clearly I don't quite have a handle on the syntax and I don't understand the 'System.Core' message. Since I'm not trying to do anything really complicated, just sending an 'extended' command to an X10 device (I'm using the X10 plugin), I'm hoping that someone can point out my obvious mistakes and help me to get this script to work.

    My environment configuration is this:

    Code:
    Current Date/Time: 11/30/2016 5:09:58 PM
    HomeSeer Version: HS3 Standard Edition 3.0.0.297
    Operating System: Microsoft Windows 10 Pro - Work Station
    System Uptime: 0 Days 4 Hours 15 Minutes 58 Seconds
    IP Address: 192.168.0.186
    Number of Devices: 16
    Number of Events: 7
    Available Threads: 200
    
    Enabled Plug-Ins
    3.0.0.68: HSTouch Server
    3.0.0.36: X10
    Thanks in advance!

    Jim

    #2
    Hi Jim,

    The system.core error message is typically generated when there are other errors. When you solve the other errors, that message will disappear to. The syntax for the plugin functions have changed too: http://homeseer.com/support/homeseer...infunction.htm. You are also mixing vb.net and vbscript syntax. By saving as a .vb extension, HS is expecting vb.net syntax. In vb.net, you would need parentheses around the 300 in the hs.waitsecs command, hence the error message complaining about that. Use a .txt extension for vbscript. I would recommend coding in vb.net going forward.

    You may be able to use CAPI commands for what you are trying to do. Here's a sample script using vb.net syntax:

    Code:
    Sub Main(ByVal Parms as String)
     
    	dim Debug as Boolean = False
    	Dim logName As String = "Dim Script"
    	dim targetDev as Double = 1234		'reference ID of the device
    	dim targetDim as Double = 65            'dim value
    	hs.CAPIControlHandler(hs.CAPIGetSingleControl(targetDev,false,targetDim,false,true))
    	If Debug Then hs.writelog(logName,"Set New Dim: " & targetDim)
    
    End Sub
    Hope that helps. If not, please post any follow up questions.

    Cheers
    Al
    HS 4.2.8.0: 2134 Devices 1252 Events
    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

    Comment


      #3
      I think it is the WaitSecs that is getting you.


      Try: hs.WaitSecs(3)


      tenholde
      tenholde

      Comment


        #4
        Thanks for the tips. I did fix the reference to the wait and now my code looks like this:

        Code:
        Sub Main()
        
        	Dim X10plugin As HomeSeerAPI.PluginAccess = New HomeSeerAPI.PluginAccess(hs, "X10", "")
        
        	Dim sunrise as Date
        	Dim threshold as Date
        	
        	threshold = TimeValue("6:45:00 AM")
        	sunrise = TimeValue(hs.Sunrise)
        	
        	if (sunrise > threshold) then
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 1, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 3, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 6, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 9, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 13, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 18, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 24, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 30, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 35, 49, False})
        		hs.WaitSecs(300)
        		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 40, 49, False})
        	end if
         
        end sub
        Now what happens when I run it is that I see this in my log:

        Code:
        VB.Net script exception(0), re-starting: Object reference not set to an instance of an object.
        Based on some experimentation, it appears my first line where I define X10plugin. I copied this code from another post so I'm not sure what might be wrong with it. I'm sure I'm just doing something incredibly stupid. I've programmed in many languages, just never VB or VB.net to any extent.

        The other thing I don't quite understand is that this is a warning and the script restarts. Apparently I have to exit HS3 and restart in order to kill this script? Is there a better way? Also, I had assumed that when a script runs from an event, it basically gets called and exits, so I'm not sure what is going on here.

        Any additional tips or guidance would be most helpful.

        Jim

        Comment


          #5
          I think I may have figured it out, and yes, I was doing something stupid (I guess). The vbscript file that this originated from just used

          Code:
          sub Main()
          When I changed the file to use vb.net, apparently I needed to do this:

          Code:
          sub Main(parms as object)
          That at least seems to get my script to execute without errors. Now, it still isn't doing exactly what I want, but at least that is progress. My next task is to figure out why my X10 commands are doing what I expect them to do.

          Under HS2, when I executed the code to do an X10 'extended' command, my light was able to be progressively made brighter. With what I think are the equivalent commands (but obviously not), my light just turns on.

          Code:
          		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 1, 49, False})
          		hs.WaitSecs(300)
          		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 3, 49, False})
          		hs.WaitSecs(300)
          		X10plugin.PluginFunction("ExecX10", {"C3", "extended", 6, 49, False})
          		hs.WaitSecs(300)
          It's been so long I don't even remember how the extended command actually worked so I guess I'm not surprised that things aren't working properly in HS3. Is there something special I need to do in the Device configuration to use extended commands?

          Thanks again for any help.

          Jim

          Comment


            #6
            After doing some more digging on this board I've found a solution that works for me using CAPI. What I did was change my script to use a function that another user has posted and it seems to do exactly what I want. For future reference, here is what I came up with:

            Code:
            Sub Main(ByVal strDevRef As String)
            	Dim objDev As Object
            	Dim intDevRef as Integer
            	Dim intCount As Integer	
            	Dim sunrise as Date
            	Dim threshold as Date
            
            	intDevRef = hs.GetDeviceRefByName(strDevRef)
            	if intDevRef < 1 then
            		hs.WriteLog("Error", "Could not find device: " & strDevRef)	
            	else
            		threshold = TimeValue("6:45:00 AM")
            		sunrise = TimeValue(hs.Sunrise)
            
            		if (sunrise > threshold) then
            			CallCAPI(intDevRef, "Brightness 5%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 10%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 15%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 20%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 25%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 30%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 35%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 40%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 50%")
            			hs.WaitSecs(300)
            			CallCAPI(intDevRef, "Brightness 60%")
            		end if
            	end if
            End Sub
            
            Function CallCAPI(ByVal intDevRef As Integer, ByVal strDevCmd As String) As CAPIControlResponse
            	CallCAPI = CAPIControlResponse.Indeterminate
            	For Each objCAPIControl As CAPIControl In hs.CAPIGetControl(intDevRef)
            		If LCase(objCAPIControl.Label) = LCase(strDevCmd) Then
            			CallCAPI = hs.CAPIControlHandler(objCAPIControl)
            			Exit For
            		End If
            	Next
            End Function
            Thanks!

            Jim

            Comment


              #7
              Jim,

              If you are going to do scripting in HS3, you will continue to wrestle with the CAPI beast.

              The tool tenScriptAid can be a big help in understanding any HS3 device info, including all of the CAPI commands each device will accept. You can try executing each CAPI string within tenScriptAid to ascertain that it does indeed operate as expected, and then you can automatically generate the vb.net code onto the Windows Clipboard so you can paste it directly into your script code.

              Give it a try. Be sure to watch the video on the web page.

              tenholde
              tenholde

              Comment

              Working...
              X