Announcement

Collapse
No announcement yet.

Countdown timer

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

  • Countdown timer

    Could anyone suggest a way to create a countdown timer? I want to set the timer and have it trigger an event when it gets to 0

  • #2
    Originally posted by tccug221 View Post
    Could anyone suggest a way to create a countdown timer? I want to set the timer and have it trigger an event when it gets to 0
    I do not think you will find a way. Unfortunately timers can only increase.

    There are kludgy ways to implement one, most involve writing a short script.

    Did I mention that it was kludgy?

    Comment


    • #3
      One way to do it:

      Create a timer that runs for one minute. At the end of the minute, check a counter, if zero, trigger your actual event, if greater than zero, decrement. Then restart the timer.

      The problem with using a counter is that the counter cannot be set to a specific value using a script.

      So I use a global variable, which can be set in script. But global variables can not be decremented with event actions, so the decrement must be done in script. And you cannot test globals in event conditions, so again, you have to test your event conditions in script (i.e in script test when the global reaches zero).

      Did I mention that it was kludgy?

      Comment


      • #4
        Originally posted by tccug221 View Post
        Could anyone suggest a way to create a countdown timer? I want to set the timer and have it trigger an event when it gets to 0
        Is there any reason it can't count upward? If you want a 30 minute countdown timer, an event can restart the timer (set to zero and start) and another event can trigger when it hits 30 minutes. Have the 2nd event restart or stop the timer if appropriate, or just let it run until the first event runs again and restarts the timer.

        If you'll give details of what you're trying to accomplish we can give more specific suggestions.
        -Wade

        Comment


        • #5
          The reason I want it to count down is so I can make have an event trigger when the time is expended. This is a NAP timer, i store a value in a SQL database that represents the length of the nap. Currently, I have it process some events...like turning the light off and then it grabs the wait value from the database and i simply use hs.WaitSecs to cause the delay between the lights going off and lights coming back on. The problem is there is no way to cancel this delay once it is started (As Far as I Know), so my thinking is if there where some sort of countdown timer that i could set to any value or reset....know what i mean?

          Comment


          • #6
            I may not be fully understanding but how about set a virtual device to the wait time value from the database then trigger the event when the timer value = virtual device value?
            -Wade

            Comment


            • #7
              It may sound kludgey, but why couldn't you create a virtual device that's the countdown timer's value?

              You could run a recurring (maybe every minute) event that gets the value of that device, subtracts 1 from it and then sets the string value to the new number?
              Once the device's value is 0, execute another event with the trigger that the virtual counter changed and became 0.
              Makes sense in my head if I understood your question.

              I'd bet there's an easy scripting method that would take care of it - I'm not a programmer, just a long-time HS user that learned to adapt and overcome with virtual devices and events.



              Edit: I see cc4005 replied as I was typing. I think were both thinking the same way.

              Comment


              • #8
                I was curious about this and I had some time this morning so I created a simple script that may do what you want. It uses a counter, a timer, and two virtual devices. You can set a value in seconds, start the countdown, stop, and restart as the countdown as needed, and it triggers an event when it reaches zero by toggling a flag.

                Code:
                '
                ' GW CountDown Timer - Ver 1.0a (03/29/2019)
                '
                Public Sub Main(param As Object)
                
                ' ###############################################################################################
                    Dim Countdown_Status_Ref as integer = 401       ' Set to Ref ID of your devices/counter
                    Dim Countdown_Trigger_Ref as integer = 402
                    Dim Countdown_Seconds_Ref as integer = 403
                    Dim Timer as string = "CountDown Timer"         ' Name of Timer to use
                ' ###############################################################################################
                
                    Dim FlagOFF as integer = 0                      ' Flag Disabled or OFF
                    Dim FlagON as integer = 100                     ' Flag Enabled or ON
                
                    Dim StartValue as integer = hs.DeviceValue(Countdown_Seconds_Ref)   ' Get initial start value
                    Dim SecondsRemaining as integer = StartValue
                
                    hs.TimerReset(Timer)                            ' Reset and start Timer
                
                    ' Run While loop until we reach zero on Counter or the Status Flag is turned OFF
                    '   Turning Countdown Status flag device OFF and ON will stop and start the countdown script
                    While SecondsRemaining > 0 and hs.DeviceValue(Countdown_Status_Ref) = FlagON
                        hs.WaitSecs(1)
                        SecondsRemaining = StartValue - hs.TimerValue(Timer).TotalSeconds
                        hs.SetDeviceValueByRef(Countdown_Seconds_Ref, SecondsRemaining, True)
                    End While
                
                    ' If we reached zero on Counter, clean up Status Flag, Counter, and then Trigger event Flag
                    If SecondsRemaining <= 0 then
                        hs.SetDeviceValueByRef(Countdown_Status_Ref, FlagOFF, True)
                        hs.SetDeviceValueByRef(Countdown_Seconds_Ref, 0, True)
                        hs.SetDeviceValueByRef(Countdown_Trigger_Ref, FlagON, True)
                        hs.WaitSecs(2)
                        hs.SetDeviceValueByRef(Countdown_Trigger_Ref, FlagOFF, True)
                    End If
                
                End Sub
                
                '--------------
                ' Requirements:
                '--------------
                ' Create HS3 Counter/Timer/Virtual Devices as follows:
                '   Timer   - Countdown Timer
                '   Counter - Countdown Seconds (time in seconds; set to intial value in Setup Event)
                '   Device  - Countdown Status (0 = OFF; 100 = ON; script is paused when OFF, runs when ON)
                '   Device  - Countdown Trigger (0 = OFF; 100 = ON; set ON for 2 secs when counter reaches zero)
                '
                ' Script changes
                '   Change the Ref IDs (3) and Timer Name (1) in lines 7 - 10 to match your system values
                '   Save script in HS3 "scripts" directory
                '
                ' Events to Create
                '   Countdown Timer - Setup (Manually triggered)
                '       1) Set counter Countdown Seconds to time delay value in seconds
                '       2) Set Countdown Status flag to On
                '   Countdown Timer - Start  (Triggered when "Countdown Status" changes and becomes On)
                '       1) Run script (i.e. C:/Program Files (x86)/HomeSeer HS3/scripts/GW_CountDownTimer.vb)
                '   Countdown Timer - Execute Task  (Triggered when "Countdown Trigger" changes and becomes On)
                '       1) Do whatever you want when timer expires
                '
                Note: Ver 1.0a - Edited code to include additional comments for clarity. No change to functionality.

                Here is a snapshot of the devices and the events:

                Devices:
                Click image for larger version  Name:	Devices.jpg Views:	1 Size:	36.6 KB ID:	1295844

                Events:
                Click image for larger version  Name:	Events.jpg Views:	1 Size:	104.2 KB ID:	1295845

                Hope this helps and feedback on this approach is welcomed.

                Comment


                • #9
                  Originally posted by tccug221 View Post
                  The reason I want it to count down is so I can make have an event trigger when the time is expended. This is a NAP timer, i store a value in a SQL database that represents the length of the nap. Currently, I have it process some events...like turning the light off and then it grabs the wait value from the database and i simply use hs.WaitSecs to cause the delay between the lights going off and lights coming back on. The problem is there is no way to cancel this delay once it is started (As Far as I Know), so my thinking is if there where some sort of countdown timer that i could set to any value or reset....know what i mean?
                  Delayed actions are stored in the delayed action table. A temporary table that is created on the fly (when you delay an action) and deleted automatically when the action runs. You can cancel the delay anytime if needed.

                  Comment


                  • #10
                    Originally posted by GlennWo View Post
                    I was curious about this and I had some time this morning so I created a simple script that may do what you want. It uses a counter, a timer, and two virtual devices. You can set a value in seconds, start the countdown, stop, and restart as the countdown as needed, and it triggers an event when it reaches zero by toggling a flag.
                    Have you tried your code? As I stated earlier, there does not seem to be a way to set a counter to a specific value in script.

                    I tried your:

                    hs.SetDeviceValueByRef(iRef, SecondsRemaining, True)

                    but it has no effect on the counter.

                    I had also tried incrementing the counter inside a loop, but the increments interfere with each other unless you introduce a delay between increments (not practical).

                    Comment


                    • #11
                      Yes, code was tested and worked (at least on my system). Take another look at the Devices and Events snapshot I pasted above on how I set this up.

                      I set the initial counter value in the "Countdown Timer - Setup" event (30 seconds in the example event snapshot I posted). I also modify the seconds remaining in the counter each time through the loop using "hs.SetDeviceValueByRef(Countdown_Seconds_Ref, SecondsRemaining, True)" and you can watch the counter decrement in the devices dashboard once per second which I did.

                      The requirements at the bottom of the script document what is needed for this script to work but for a simple test, you can:
                      1) create the counter, timer, and devices
                      2) put the script in the scripts directory
                      3) create the "Countdown Timer - Start" event that runs the script when Countdown Status changes to ON
                      4) manually set the Countdown Seconds to some value using the devices dashboard
                      5) manually set Countdown Status to ON to kick off the countdown process and watch the Countdown Seconds value and status flags.

                      BTW, make sure you modify the Ref ID and Timer name at the top of the script to match your system values.

                      Comment


                      • #12
                        I realized I changed the name of the counter from "Countdown Value" to "Countdown Seconds" and pasted a snapshot of the "older" version above which might have caused some confusion.
                        Here is a snapshot of my final test events.

                        Click image for larger version

Name:	Events2.jpg
Views:	109
Size:	107.6 KB
ID:	1295903

                        Look at the embedded comments and the requirements section at the bottom of the script for a detailed explanation of function and requirements. Once again, this is working well in my simple test scenarios.

                        GlennWo

                        Comment


                        • #13
                          You may want to look at what AK Smart Device plug introduces. It has count down capabilities and is available under Utility.

                          So to expand your use case.
                          1. You want an event to trigger after x minutes (this is dynamic variable).
                          2. The event would be trigger manually
                          3. The ability to cancel event triggering.

                          You could create a Virtual Switch (VS) with multiple values, ie 0, 10, 20, 30 or a series of VS for each value.

                          Create a series of event trigger based on VS and event to delete delayed event.

                          E.g If VS is set to 30 and VS is turned on then an event is trigger after 30 minutes and the event turns of VS

                          If you turn off VS the delayed action is deleted.

                          Comment


                          • #14
                            Originally posted by GlennWo View Post
                            Yes, code was tested and worked (at least on my system).
                            Alright, I think I have some sort of handle on this. Sorry for getting a little technical here:

                            The statement "hs.SetDeviceValueByRef" is setting the "label" of the counter, but is not setting the "value" of the counter. There is a difference.

                            The event actions that increment, decrement, etc are dealing with the value, not the label. In fact, if you were to set the counter's label to "20" (using "hs.SetDeviceValueByRef") and leave the counter value still at 0, then run the event action to decrement, the counter value and label will both become "-1" instead of 19 (yes, the counter's label will be reset to the counter's new value).

                            This is working above because the script is doing the decrement. In other words, your code works, but the same results would occur if a Virtual Switch was used instead of a counter.

                            And that is my complaint: There is no way to set the counter "value" to a specific number in script. And this whole thing is way too confusing.

                            Comment


                            • #15
                              I sent more detailed info to tccug221 last night here is a less detailed timer approach

                              you need 2 events, one virtual device, and a one line script for the 1st event: &hs.setdevicelastchange dev id, dateadd("n",20-1,now)

                              time above set to 20 minutes.
                              1st event turn on the virtual device and run the &hs.setdevicelastchange dev id, dateadd("n",20-1,now) ****line sets the last change on the device page to a time in the future.****


                              (devicetime on the virtual device will not report any time until 19 minutes have passed)
                              then when devicetime on the virtual device is 1 minute go to the 2nd event.

                              2nd event when virtual device time has been exactly on for 1 minute, turn off virtual device and complete your needed actions.

                              And at any time you want to shut off the timer just set the virtual device to off (no delayed actions to process)

                              Stuart

                              Comment

                              Working...
                              X