Announcement

Collapse
No announcement yet.

Trying to Create Variable

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

    Trying to Create Variable

    I'm using this code and the Catch never evaluates. Any suggestions?

    PHP Code:
    Try
    PT hs.GetVar("LRPT")
    hs.WriteLog("LR""GetVar Error")

    Catch 
    ex As Exception
    hs
    .CreateVar("LRPT")
    hs.SaveVar("LRPT"0)
    hs.WriteLog("LR""Exception" ex.ToString)

    End Try 

    The error I am getting is that LRPT is not defined



    I am trying to create a global variable and would appreciate any help.
    Michael

    #2
    Do you declare 'PT'? (Dim PT As Object)
    Mike____________________________________________________________ __________________
    HS3 Pro Edition 3.0.0.548, NUC i3

    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

    Comment


      #3
      I want to store a decimal to PT.

      I’ve tried Dim PT as single but will try object.
      Michael

      Comment


        #4
        This is the error I am getting:

        Aug-05 6:47:20 PM Error A call was made to save a global variable (LRPT) but that global variable has not been defined.
        Aug-05 6:47:20 PM PT 0
        Aug-05 6:47:20 PM LR GetVar Error
        Aug-05 6:47:20 PM Warning A call was made to retrieve a global variable (LRPT) but that global variable has not been defined.

        Michael

        Comment


          #5
          It seems you are trying to get the global variable LRPT, which doesn't exist. Have you created this one elsewhere?

          I believe you need to create a global variable using hs.CreateVar, or within the HS GUI

          Edit: Just realized that is what you are trying to do within the catch...

          Comment


            #6
            I'm not very familiar with PHP, but more so other languages...

            Could it be that the catch isn't triggered because it "only" gives a warning?

            And that only the first line after the catch actually belongs to the catch, as you are not using brackets (try { something } catch { something }.

            The Save is called as the next line after the catch - and that throws an error (which is not catched).

            Comment


              #7
              I've experienced the same issue. I now create all varialbles that I use in scripts in my Startup.vb script.
              tenholde

              Comment


                #8
                Originally posted by tenholde View Post
                I've experienced the same issue. I now create all varialbles that I use in scripts in my Startup.vb script.
                ALL variables? Or does this create a global variable?
                HomeSeer Version: HS4 Pro Edition 4.2.19.0 (Windows - Running as a Service)
                Home Assistant 2024.3
                Operating System: Microsoft Windows 11 Pro - Desktop
                Z-Wave Devices via two Z-Net G3s
                Zigbee Devices via RaspBee on RPi 3b+
                WiFi Devices via Internal Router.

                Enabled Plug-Ins
                AK GoogleCalendar 4.0.4.16,AK HomeAssistant 4.0.1.23,AK SmartDevice 4.0.5.1,AK Weather 4.0.5.181,AmbientWeather 3.0.1.9,Big6 3.44.0.0,BLBackup 2.0.64.0,BLGData 3.0.55.0,BLLock 3.0.39.0,BLUPS 2.0.26.0,Device History 4.5.1.1,EasyTrigger 3.0.0.76,Harmony Hub 4.0.14.0,HSBuddy 4.51.303.0,JowiHue 4.1.4.0,LG ThinQ 4.0.26.0,ONVIF Events 1.0.0.5,SDJ-Health 3.1.1.9,TPLinkSmartHome4 2022.12.30.0,UltraCID3 3.0.6681.34300,Z-Wave 4.1.3.0

                Comment


                  #9
                  Any variable I intend to use in a script I create at startup instead of in each script
                  tenholde

                  Comment


                    #10
                    Originally posted by BME View Post
                    I'm not very familiar with PHP, but more so other languages...

                    Could it be that the catch isn't triggered because it "only" gives a warning?

                    And that only the first line after the catch actually belongs to the catch, as you are not using brackets (try { something } catch { something }.

                    The Save is called as the next line after the catch - and that throws an error (which is not catched).
                    I think you may be right on the warning vs exception (a warning won't throw an exception, so the "catch" stuff won't be run).

                    This code is typical Visual Basic, not PHP. It is not just the first line after the "catch" that is run, it is all the code between the "catch" and the "end try".

                    I also create all my globals in a startup script file, so that I do not have to deal with it later.

                    Comment


                      #11
                      Originally posted by tenholde View Post
                      Any variable I intend to use in a script I create at startup instead of in each script
                      The only issue I have with doing this is that it means you have to maintain two different scripts, for any script you work on - and if you give the script to someone they have to know to do the same. Here's what I do to create the global variable, if needed, at the start of a script:

                      Code:
                      'Setup Global Variable if needed
                      Dim Errst
                      Errst = hs.CreateVar('Foo')
                      If Errst = "" then
                          If Debug Then hs.WriteLog("MyTestScript", "DEBUG: Created Global Variable Foo")
                      End If
                      Basically I just go ahead and attempt to create it - if the variable didn't already exist it simply creates it, if it did exist, it returns something in the result (Can't recall offhand what it returns). But to make a long story short, you won't harm anything by simply attempting to create it each time you run the script - you won't re-initialize it or anything and won't lose what it holds. the if/then statement I provided is optional, that's just so you know what happened, in the log.

                      But doing this means that your script is completely self contained, that has value to me....

                      Paul

                      Comment


                        #12
                        Originally posted by tenholde View Post
                        Any variable I intend to use in a script I create at startup instead of in each script
                        Thanks.
                        HomeSeer Version: HS4 Pro Edition 4.2.19.0 (Windows - Running as a Service)
                        Home Assistant 2024.3
                        Operating System: Microsoft Windows 11 Pro - Desktop
                        Z-Wave Devices via two Z-Net G3s
                        Zigbee Devices via RaspBee on RPi 3b+
                        WiFi Devices via Internal Router.

                        Enabled Plug-Ins
                        AK GoogleCalendar 4.0.4.16,AK HomeAssistant 4.0.1.23,AK SmartDevice 4.0.5.1,AK Weather 4.0.5.181,AmbientWeather 3.0.1.9,Big6 3.44.0.0,BLBackup 2.0.64.0,BLGData 3.0.55.0,BLLock 3.0.39.0,BLUPS 2.0.26.0,Device History 4.5.1.1,EasyTrigger 3.0.0.76,Harmony Hub 4.0.14.0,HSBuddy 4.51.303.0,JowiHue 4.1.4.0,LG ThinQ 4.0.26.0,ONVIF Events 1.0.0.5,SDJ-Health 3.1.1.9,TPLinkSmartHome4 2022.12.30.0,UltraCID3 3.0.6681.34300,Z-Wave 4.1.3.0

                        Comment


                          #13
                          Thanks, everyone. Appreciate it!
                          Michael

                          Comment

                          Working...
                          X