Announcement

Collapse
No announcement yet.

Script Question: How do I check for Day of the Week?

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

    Script Question: How do I check for Day of the Week?

    Script Question: How do I check for Day of the Week?

    Hope this is an easy question. In a script, I want to check for what day of the week it is and do things based on that. Ex:


    Sub Main ()

    'If day of the week is "Monday" then

    hs.TriggerEvent "Monday Events"

    'ElseIf day of the week is "Tuesday" then

    hs.TriggerEvent "Tuesday Events"

    'Else not
    hs.TriggerEvent "Other"

    End Sub

    #2
    Originally posted by ViperJD View Post
    Script Question: How do I check for Day of the Week?

    Hope this is an easy question. In a script, I want to check for what day of the week it is and do things based on that. Ex:


    Sub Main ()

    'If day of the week is "Monday" then

    hs.TriggerEvent "Monday Events"

    'ElseIf day of the week is "Tuesday" then

    hs.TriggerEvent "Tuesday Events"

    'Else not
    hs.TriggerEvent "Other"

    End Sub
    Try x = now.DayOfWeek
    As I recall, this returns: 0= Sunday, 1= Monday... 6= Saturday.

    Edit: Thinking about this, and I haven't actually checked; it might well return 1=Sun, 2=Mon... 7=Sat.
    Last edited by Wadenut; February 3, 2015, 12:37 PM.
    Real courage is not securing your Wi-Fi network.

    Comment


      #3
      I get:

      Error Running script, script run or compile error in file: test.txt424:Object required: 'now' in line 3 More info: Object required: 'now'

      from (test.txt):

      sub main()
      X = now.DayofWeek

      If X = 0 then
      hs.speak "Sunday"
      ElseIf X = 1 then
      hs.speak "Monday"
      ElseIf X = 2 then
      hs.speak "Tuesday"
      End If

      end sub

      Comment


        #4
        I needed to have it in a .VB not .txt

        Thanks.

        Comment


          #5
          Try this as a .vb script

          Sub Main(parm as object)
          Dim x As Integer

          x = Microsoft.VisualBasic.DateAndTime.Now.DayOfWeek

          If X = 0 Then
          hs.speak "Sunday"
          ElseIf X = 1 Then
          hs.speak "Monday"
          ElseIf X = 2 Then
          hs.speak "Tuesday"
          End If
          end sub
          💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

          Comment


            #6
            I got it, thanks.

            here is the current VB Script if anyone is interested:

            Code:
            '  Good Morning 
            '  by JD Taylor
            '  -----------------------------------------------------------------------------------------------------------
            '  This script will say good morning and check weekly activities 
            '
            '  Syntax is:  GoodMorning.txt
            ' 
            '         ex:   GoodMorning.txt 
            '	 
            '  -----------------------------------------------------------------------------------------------------------
            
            Sub Main(parm as object)
            
               Dim DayOfWeek as Integer
            
            
            '	hs.WriteLog "GoodNight.txt", strLog
            
            hs.speak("Good Morning, it is ") 
            
            
            DayOfWeek = now.DayofWeek
            
            If DayOfWeek = 0 then 
              hs.speak("Sunday")
            ElseIf DayOfWeek = 1 then 
              hs.speak("Monday")  
            ElseIf DayOfWeek = 2 then 
              hs.speak("Tuesday")  
            ElseIf DayOfWeek = 3 then 
              hs.speak("Wednesday")  
            ElseIf DayOfWeek = 4 then 
              hs.speak("Thursday")  
            ElseIf DayOfWeek = 5 then 
              hs.speak("Friday")  
            ElseIf DayOfWeek = 6 then 
              hs.speak("Saturday")  
            End If
            
            If hs.DeviceValue(280) < 25 then             'Is the Feels like temp below 25 F?
               	hs.speak("Wow, it is cold out!")
            End If
            
            hs.speak("The temperature outside is " &  (hs.DeviceValue(271)) & " degrees, and feels like " & (hs.DeviceValue(280)) & " degrees")
            
            End Sub
            Output looks like:
            Code:
            System	Control Panel Immediate Script: &hs.Runscript("GoodMorning.vb",True,True)
            TTS	Speak ():Good Morning, it is
            TTS	Speak ():Tuesday
            TTS	Speak ():Wow, it is cold out!
            TTS	Speak ():The temperature outside is 32 degrees, and feels like 24 degrees

            Comment


              #7
              Here is a more direct way of doing it...

              Code:
                       Dim SpeakText As String = "Good Morning, it is " & DateTime.Now.DayOfWeek.ToString & ". "
                
                       'Is the Feels like temp below 25 F?
                      If hs.DeviceValue(280) < 25 Then
                          SpeakText &= "Wow, it is cold out! "
                      End If
                
                       SpeakText &= "The temperature outside is " & (hs.DeviceValue(271)) & " degrees, and feels like " & (hs.DeviceValue(280)) & " degrees. "
                
                       hs.Speak(SpeakText)
              In addition to less code, the script is building the message as a string and only calling hs.Speak once. That might prevent a situation in which two events fire at around the same time (same trigger maybe) and both are queuing speech text at the same time. You could end up with part of each message being interwoven into the other.

              Output...

              Code:
                TTS   Speak ():Good Morning, it is Tuesday. Wow, it is cold out! The temperature outside is -1 degrees, and feels like -1 degrees.

              Comment


                #8
                Excellent Cleavitt76, I am always looking for better ways to write code. But in this case I wanted the check for the day of the week so I could add more code to do other tasks, like reminder to take out the trash or recyclables.

                Comment

                Working...
                X