Announcement

Collapse
No announcement yet.

Script Help!

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

    Script Help!

    I have a script that runs as part of my wife's morning routine. It does very well and has been running great for a while now. I have four different scripts that can be called, depending on the day and temperature. My problem is it's starting to get a bit unwieldy. What I'm trying to do is to lift out the section that is between the dashes and put that into an ini file to be read by any of the four scripts, since that part is identical. This will make it much easier to keep track of since I'll only have one ini file to maintain, instead of every change having to be made on four different scripts.

    Can anyone help someone who knows just enough to be dangerous with scripts?

    PHP Code:
    Public Sub Main(ByVal Parms As Object
        
    Dim speakMsg As String "" 
        
    Dim s as string
        speakMsg 
    speakMsg & ("Cynthia, the temperature is " hs.DeviceValue("Z3") & " degrees this " TimeOfDay())
        
    speakMsg speakMsg & (" . ")
        
    speakMsg speakMsg hs.Plugin("BLRandom").RandomizeGroup("Warm Car")
        
    speakMsg speakMsg & (" . . ")
        
    speakMsg speakMsg & (". Right now we have ")
        
    speakMsg speakMsg hs.DeviceString("#10")
        
    speakMsg speakMsg & (" conditions outside. ")
        
    speakMsg speakMsg & (" . ")
        
    speakMsg speakMsg.Replace("Precip""Precipatation ")
    '------------------------------------------------------------------------------
    s=hs.DeviceString("#10")
    if s ="Cloudy" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Cold")
    elseif s ="Mostly Cloudy" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Cold")
    elseif s ="Partly Cloudy" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Cold")
    elseif s ="Cloudy and Windy" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Windy-Cold")
    elseif s ="Fair" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Fair-Cold")
    elseif s ="Sunny" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Sunny-Cold")
    elseif s ="Light Rain" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Rain-Cold")
    elseif s ="Heavy Rain" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Rain-Cold")
    elseif s ="Rain" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Rain-Cold")
    elseif s ="Light Rain and Windy" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Rain-Cold")
    elseif s ="Fog" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Fog")
    elseif s ="Wintry Mix" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Wintry Mix")
    elseif s ="Light Snow" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Light Snow")
    elseif s ="Unknown Precip" then
        speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Unknown")
    end if
    '
    ------------------------------------------------------------------------------
        
    hs.Speak(speakMsgTrue)
        
    speakMsg Nothing
    End Sub 
        
    Function myTime() As String 
            
    Return Now.ToString("h:mm tt").Replace("PM""P.M.").Replace("AM""A.M.").Replace(":0"", o"
        
    End Function 
        Function 
    TimeOfDay() As String 
            Select 
    Case Now.Hour 
                
    Case Is 12 
                    
    Return "Morning" 
                
    Case 12 To 17 
                    
    Return "Afternoon" 
                
    Case 18 To 24 
                    
    Return "Evening" 
                
    Case Else 
                    Return 
    "Error" 
            
    End Select 
        End 
    Function 
    Marty
    ------
    XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
    Playing with HS3 a bit but it's just play at this point.

    #2
    In order to make the middle section read a little better you could consider using the "Select Case" statement instead of all the multiple "else if" statements.

    You could also take the middle bit and make it a procedure/script in itself that could then be called from the four separate scripts.

    Does this help?

    Nicolai
    Nicolai L

    Comment


      #3
      It may be easier to consolidate the four scripts into one then have some sort of function to do what you are after - then you can pass the temp/day in as a parameter or something

      Comment


        #4
        I'm sure you're both right but my problem is, the script above is the absolute pinnacle of my scripting abilities.

        Nicolai, I did use the "select case" further down but never thought of it it for my problem section, of course it wasn't a problem when there were only a couple "elseif" statements. You're right it would clean it up a bit but it still leaves me with my original problem and why I thought an ini file would simplify things a bit. A second script called from the first script, i guess would work the same as the ini file. Would you have a small example of how to do that.

        mrhappy, you're right it would be simpler to combine, however my wife's morning routine is quite extensive, took years of small adjustments and to be honest I'm afraid to screw with it or the WAF.
        Marty
        ------
        XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
        Playing with HS3 a bit but it's just play at this point.

        Comment


          #5
          Marty

          I'm doing this off the top of my head as I don't have access to my compiler at the moment so the syntax may be a little off, but you could do something like this:

          Public Sub CommonProc(ByVal Parms As Object)
          ' Add in the common bit between the four scripts here
          End Sub

          Public Sub Option1(ByVal Parms As Object)
          ' Add in the lines from the original script one here
          ' e.g.
          Dim speakMsg As String = ""
          Dim s as string
          speakMsg = speakMsg & ("Cynthia, the temperature is " & hs.DeviceValue("Z3") & " degrees this " & TimeOfDay())
          ' etc.

          ' then call common procedure
          CommonProc()

          ' then do the last few lines hs.Speak(speakMsg, True)
          speakMsg = Nothing
          End Sub

          You could then have another the procedures (taken from the other three original scripts) within this same new procedure, e.g.

          Public Sub Option2(ByVal Parms As Object)
          ' various lines here
          ' then call common procedure
          CommonProc()
          ' final lines etc.
          End Sub

          Public Sub Option3(ByVal Parms As Object)
          ' various lines here
          ' then call common procedure
          CommonProc()
          ' final lines etc.
          End Sub

          Public Sub Option4(ByVal Parms As Object)
          ' various lines here
          ' then call common procedure
          CommonProc()
          ' final lines etc.
          End Sub


          That way you would have all four separate scripts in one and a common procedure they all use.

          When you then setup the call from a HS Event you need to make sure you call the right one.

          So from HS setup a call to a script, use the Run Script action. pick the new script and in the "Optional parameters" box type something like the following: ("Option1","") that should do it.

          Let me know if you need any clarification on the above.

          Cheers
          Nicolai
          Nicolai L

          Comment


            #6
            Nicolai, thank you, i'm going to play with this a bit as it's at the moment over my head a bit but I think i understand where you're going.
            Marty
            ------
            XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
            Playing with HS3 a bit but it's just play at this point.

            Comment


              #7
              Marty

              Let me know if you get stuck. You can always send me the new script you are putting together and I can take a look when I'm back home on my own PC.

              It might seem a bit daunting at first but you've actually already done the hardest bit with the initial scripts.

              Cheers
              Nicolai
              Nicolai L

              Comment


                #8
                I appreciate the hints and the encouragement as well as the fall back position in case i need it. I'm reaching here but it's a learning process too.
                Marty
                ------
                XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
                Playing with HS3 a bit but it's just play at this point.

                Comment


                  #9
                  I had a chance to play with this a bit last night. Seems I got stuck on a place I didn't expect and it's giving me fits. I cut the size of the script down a little just to make it easier to play with and I only have one option section in so far.

                  My current problem is.... I'm able to call the script just fine but I get stuck on the "s=hs.DeviceString("#10")" portion. Obviously it doesn't go there but darn if I can figure out where it needs to be. I keep getting the error message
                  "Script compile error: Name 's' is not declared.on line 16".

                  I'm not even sure if it needs to be in the Sub Common part or does it need to be in each of the Sub Optionx parts? I've tried it in both but to no avail. I sure could use a little hint here.

                  PHP Code:
                  Public Sub CommonProc(ByVal Parms As Object)

                      
                  s=hs.DeviceString("#10")

                  '------------------------------------------------------------------------------
                  if s ="Cloudy" then
                      speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Cold")
                  elseif s ="Fair" then
                      speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Fair-Cold")
                  elseif s ="Sunny" then
                      speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Sunny-Cold")
                  elseif s ="Unknown Precip" then
                      speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("BLW-Unknown")
                  end if
                  '
                  ------------------------------------------------------------------------------

                  End Sub

                  Public Sub Option1(ByVal Parms As Object)

                      
                  Dim speakMsg As String "" 
                      
                  Dim s as string

                      speakMsg 
                  speakMsg & ("Cynthia, the temperature is " hs.DeviceValue("Z3") & " degrees this " TimeOfDay())
                      
                  speakMsg speakMsg & (" . ")
                      
                  speakMsg speakMsg hs.Plugin("BLRandom").RandomizeGroup("Warm Car")
                      
                  speakMsg speakMsg & (" . . ")
                      
                  speakMsg speakMsg & (". Right now we have ")
                      
                  speakMsg speakMsg hs.DeviceString("#10")
                      
                  speakMsg speakMsg & (" conditions outside. ")
                      
                  speakMsg speakMsg & (" . ")
                      
                  speakMsg speakMsg.Replace("Precip""Precipatation ")

                      
                  CommonProc()

                      
                  hs.Speak(speakMsgTrue)
                      
                  speakMsg Nothing

                  End Sub 

                      
                  Function TimeOfDay() As String 
                          Select 
                  Case Now.Hour 
                              
                  Case Is 12 
                                  
                  Return "Morning" 
                              
                  Case 12 To 17 
                                  
                  Return "Afternoon" 
                              
                  Case 18 To 24 
                                  
                  Return "Evening" 
                              
                  Case Else 
                                  Return 
                  "Error" 
                          
                  End Select 
                      End 
                  Function 
                  Marty
                  ------
                  XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
                  Playing with HS3 a bit but it's just play at this point.

                  Comment


                    #10
                    You need to declare the variable s before you can use it. It's declared in the Option1 sub routine, but not in CommonProc.

                    Add 'Dim s as string' to the CommonProc sub.

                    Note that variables can be declared globally (outside the routines for all to reference), or locally as in your case. By declaring 's' in each routine they have absolutely nothing in common (other than having the same name).
                    Mike

                    Comment


                      #11
                      The commonproc needs to be a function. Here is how I would change it. Note that it will return the condition which you would add to your speak message in the main subroutine

                      Code:
                      Public Function CommonProc() as string
                      
                      dim s as string =hs.DeviceString("#10") 
                      dim thisMsg as string 
                      '------------------------------------------------------------------------------ 
                      if s ="Cloudy" then 
                          thisMsg = hs.Plugin("BLRandom").RandomizeGroup("BLW-Cloudy-Cold") 
                      elseif s ="Fair" then 
                          thisMsg  hs.Plugin("BLRandom").RandomizeGroup("BLW-Fair-Cold") 
                      elseif s ="Sunny" then 
                          thisMsg =  hs.Plugin("BLRandom").RandomizeGroup("BLW-Sunny-Cold") 
                      elseif s ="Unknown Precip" then 
                          thisMsg =  hs.Plugin("BLRandom").RandomizeGroup("BLW-Unknown") 
                      end if 
                      return thisMsg
                      '------------------------------------------------------------------------------ 
                      
                      End Sub 
                      
                      Public Sub Option1(ByVal Parms As Object) 
                      
                          Dim speakMsg As String = ""  
                          Dim s as string 
                      
                          speakMsg = speakMsg & ("Cynthia, the temperature is " & hs.DeviceValue("Z3") & " degrees this " & TimeOfDay()) 
                          speakMsg = speakMsg & (" . ") 
                          speakMsg = speakMsg & hs.Plugin("BLRandom").RandomizeGroup("Warm Car") 
                          speakMsg = speakMsg & (" . . ") 
                          speakMsg = speakMsg & (". Right now we have ") 
                          speakMsg = speakMsg & hs.DeviceString("#10") 
                          speakMsg = speakMsg & (" conditions outside. ") 
                          speakMsg = speakMsg & (" . ") 
                          speakMsg = speakMsg.Replace("Precip", "Precipatation ") 
                      
                      
                          hs.Speak(speakMsg & CommonProc(), True) 
                          speakMsg = Nothing 
                      
                      End Sub  
                      
                          Function TimeOfDay() As String  
                              Select Case Now.Hour  
                                  Case Is < 12  
                                      Return "Morning"  
                                  Case 12 To 17  
                                      Return "Afternoon"  
                                  Case 18 To 24  
                                      Return "Evening"  
                                  Case Else  
                                      Return "Error"  
                              End Select  
                          End Function  
                      James

                      Running HS 3 on Win10 .

                      Comment


                        #12
                        Also, I don't think you need the dim s string on the option1 subroutine. If you want an explanation on any of it of if I misunderstood just let us know.
                        James

                        Running HS 3 on Win10 .

                        Comment


                          #13
                          Originally posted by mwaite View Post
                          You need to declare the variable s before you can use it. It's declared in the Option1 sub routine, but not in CommonProc.

                          Add 'Dim s as string' to the CommonProc sub.

                          Note that variables can be declared globally (outside the routines for all to reference), or locally as in your case. By declaring 's' in each routine they have absolutely nothing in common (other than having the same name).
                          The other problem I saw was that I think he was expecting the speakmsg to be a global variable and was concatenating the string from the common proc. Unless he passed it by reference he wouldn't have access to it in option1 subroutine.
                          James

                          Running HS 3 on Win10 .

                          Comment


                            #14
                            Interesting, I didn't know you could combine the "dim s as string =hs.DeviceString("#10")
                            " into one line. It got me past the Name not declared error and give me an expression error. "Script compile error: Expression is not a method.on line 22"
                            Marty
                            ------
                            XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
                            Playing with HS3 a bit but it's just play at this point.

                            Comment


                              #15
                              Are we sure pulling from a common INI file wouldn't be simpler.
                              Marty
                              ------
                              XPpro SP3 /w HS Standard 2.5.0.80, HSTouch Server - 1.0.0.70, HSTouch Client 1.0.0.73 HSTouch Android - 1.0.0.2, HSTouch iPhone - 1.0.0.2
                              Playing with HS3 a bit but it's just play at this point.

                              Comment

                              Working...
                              X