Announcement

Collapse
No announcement yet.

Script in HS4 is not working

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

    Script in HS4 is not working

    I've been searching for a way to create an alarm clock functionality in Homeseer. Someone in another forum put me onto a script that was written for HS3 that does what I need. When I try to use it in HS4 it doesn't work. Script is attached.


    When I run the script to create the device, the error in my log is:

    2021-01-29 9:58:17 AM HomeSeer Error 3 Running script TimeDevice.vb :Exception has been thrown by the target of an invocation.->Does entry point CreateDevice exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at Scheduler.clsRunVBNetScript.ExecuteScript()

    It sets up a device, but it doesn't quite look like my other devices
    Click image for larger version  Name:	device view.jpg Views:	0 Size:	22.5 KB ID:	1454090


    There is no status or last changed info and when I go into the device, those areas are blank, yet the buttons were created (as you can see in the picture just above)
    Click image for larger version  Name:	devicedetails.jpg Views:	0 Size:	37.7 KB ID:	1454091

    If I attempt to hit one of the buttons to test whether it changes the time of the event, I get a very similar error message:

    2021-01-29 9:57:59 AM HomeSeer Error 3 Running script TimeDevice.vb :Exception has been thrown by the target of an invocation.->Does entry point ButtonPress exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at Scheduler.clsRunVBNetScript.ExecuteScript()


    I've attempted to use tenscripting4 to try and identify the issue, but have not had any success.

    Does anyone have insight as to what might be going on that a script which supposedly works in HS3 isn't working in HS4

    Any advice on how to get this working?




    #2
    The code you posted is an absolute mess of conflicting variable types. All kinds of implicit conversions from one type to the next, relying on the default behaviour of vb.net vs vsBasic -- never a good idea.

    I've tried to add explicit conversions, guessing what is actually meant, but could not always discern what kind of parameter type was really being passed to various routines, as they were all declared as Object. This should at least compile.

    Code:
    Public Sub Main(ByVal input As Object)
    'Musk 2019
    '-----------------------------
    'The Main sub will update
    'DeviceRef, NewTime
    'Example: 3245,13: 00 -> will set device 3245 to 13:00 and the event to trigger at 13:00
    '-----------------------------
    
    Dim deviceRef As Integer = CInt(CStr(input).Split(Convert.ToChar(","))(0))
    Dim timeString As String = input.ToString.Split(Convert.ToChar(","))(1)
    Dim _time As TimeSpan = TimeSpan.Parse(timeString)
    
    UpdateDeviceAndEvent(deviceRef, _time)
    End Sub
    
    Public Sub CreateDevice(ByVal input As Object)
    Dim deviceRef As Integer = hs.NewDeviceRef("Clock Control")
    Dim dev As Scheduler.Classes.DeviceClass = CType(hs.GetDeviceByRef(deviceRef), Classes.DeviceClass)
    dev.Location(hs) = "Clock"
    dev.Location2(hs) = "Clock"
    dev.Device_Type_String(hs) = "Your event here"
    
    hs.SaveEventsDevices()
    
    AddDeviceButtons(deviceRef.ToString)
    
    Dim _time As New TimeSpan
    hs.SetDeviceString(deviceRef, _time.ToString("hh \: mm"), False)
    hs.SetDeviceValueByRef(deviceRef, _time.TotalMinutes, True)
    End Sub
    
    Public Sub ButtonPress(ByVal input As Object)
    'Dim deviceRef As Integer = input(0)
    'Dim parameter As String = input(1)
    Dim deviceRef As Integer = CType(input, Integer())(0)
    Dim parameter As String = CType(input, Integer())(1).ToString
    Dim deviceValue As Integer = hs.DeviceValue(deviceRef)
    Dim minutesToAdd As Integer = Integer.Parse(parameter)
    
    Dim newValue As Integer = deviceValue + minutesToAdd
    If newValue < 0 Then newValue = 24 * 60 + minutesToAdd
    If newValue > 24 * 60 Then newValue = minutesToAdd
    
    Dim _time As TimeSpan = New TimeSpan(0, newValue, 0)
    UpdateDeviceAndEvent(deviceRef, _time)
    End Sub
    
    Public Sub UpdateDeviceAndEvent(ByVal deviceRef As Integer, ByVal _time As TimeSpan)
    hs.SetDeviceString(deviceRef, _time.ToString("hh \: mm"), False)
    hs.SetDeviceValueByRef(deviceRef, _time.TotalMinutes, True)
    
    
    Dim eventName As String = DirectCast(hs.GetDeviceByRef(deviceRef), Scheduler.Classes.DeviceClass).Device_Type_String(hs)
    'Dim eventRef As String = hs.GetEventRefByName(eventName)
    Dim eventRef As Integer = hs.GetEventRefByName(eventName)
    hs.EventSetTimeTrigger(eventRef, New Date(1, 1, 1).Add(_time))
    hs.SaveEventsDevices()
    End Sub
    
    Public Sub AddDeviceButtons(ByVal device_ref As String)
    Dim devID As Integer = CInt(device_ref)
    hs.DeviceScriptButton_DeleteAll(devID)
    'hs.DeviceProperty_dvMISC(device_ref, HomeSeerAPI.Enums.eDeviceProperty.MISC_Set, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES)
    hs.DeviceProperty_dvMISC(devID, HomeSeerAPI.Enums.eDeviceProperty.MISC_Set, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES)
    
    Try
    hs.WriteLog("TimeDevice.vb", "Added button 1:" & hs.DeviceScriptButton_AddButton(devID, "+5 min", 5, "TimeDevice.vb", "ButtonPress", "+5", 1, 1, 1))
    hs.WriteLog("TimeDevice.vb", "Added button 2:" & hs.DeviceScriptButton_AddButton(devID, "-5 min", -5, "TimeDevice.vb", "ButtonPress", "-5", 1, 2, 1))
    hs.WriteLog("TimeDevice.vb", "Added button 3:" & hs.DeviceScriptButton_AddButton(devID, "+15 min", 15, "TimeDevice.vb", "ButtonPress", "+15", 1, 3, 1))
    hs.WriteLog("TimeDevice.vb", "Added button 4:" & hs.DeviceScriptButton_AddButton(devID, "-15 min", -15, "TimeDevice.vb", "ButtonPress", "-15", 1, 4, 1))
    hs.WriteLog("TimeDevice.vb", "Added button 5:" & hs.DeviceScriptButton_AddButton(devID, "+1 time", 60, "TimeDevice.vb", "ButtonPress", "+60", 2, 1, 1))
    hs.WriteLog("TimeDevice.vb", "Added button 6:" & hs.DeviceScriptButton_AddButton(devID, "-1 time", -60, "TimeDevice.vb", "ButtonPress", "-60", 2, 2, 1))
    Catch ex As Exception
    hs.WriteLog("TimeDevice.vb", "Error adding buttons:" & ex.Message)
    End Try
    End Sub
    tenholde

    Comment


      #3
      Where is ButtonPress called, and what does the parameters passed to it look like?
      tenholde

      Comment


        #4
        I tried this code and got error messages in my log just like before.

        Running script TimeDevice.vb :Exception has been thrown by the target of an invocation.->Does entry point CreateDevice exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Scheduler.clsRunVBNetScript.ExecuteScript()


        If it's helpful at all, here is a link to the original post where this code existed. It's in another language that has been translated but it explains the functionality which should answer your question about the ButtonPress

        https://aofzpbhjjvtlqlgck4csixikxe--...med-en-device/




        Comment

        Working...
        X