Announcement

Collapse
No announcement yet.

Is Increment and Decrement counter thread safe?

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

    Is Increment and Decrement counter thread safe?

    Hi,

    I've noticed that when I multiple events that could fire simultaneously and I increment or decrement a counter that sometimes is misses the update. I was wondering if these calls are thread safe, and if they aren't is there a way to force (or simulate) thread safety. For now I just put a different count of seconds wait before each increment, not perfect, but at least it will reduce the probablity.

    Thanks!

    #2
    Yes, that is a problem with counters. If you try to increment or decrement in a loop, not every increment or decrement will happen.

    I tend to use a virtual switch or a global variable when this might happen, avoiding using a counter.

    Comment


      #3
      aa6vh Thank you. How could you use a virtual device to count? I want to keep a counter of the number of people in the house. When the counter reaches zero I want to make sure all my doors are locked, garage doors are closed and alarm is set.

      Comment


        #4
        Originally posted by NC-Lou View Post
        aa6vhHow could you use a virtual device to count?
        Unfortunately you need to perform the increment/decrement in script when using a virtual switch or global variable. You could create a Virtual Switch, delete all the default values (or states), then create a value range, say 0 to 20 (or however many people maximum you expect in your house at any one time). One advantage to using a virtual switch is if you ever decide to implement HS Touch, you can have different icons reflecting the different value ranges.

        But rather than do that, I created a virtual switch for each resident, plus a couple of extra for guests. When each resident comes home, their virtual switch gets set appropriately. I also have a "global" virtual switch that indicates if anyone is home, or if everyone is away or everyone is on vacation, or asleep, or whatever. I use a script to set that virtual switch based on the several individuals switches, but it might be easier to just set it using the "Easy Trigger" plugin. By having that one overall switch that reflects the group status makes my event conditions easier to specify.

        Lots of options and ways to accomplish this. Others may come in with their suggestions and how they implemented their occupancy methods.

        Comment


          #5
          Here I do it with easy trigger plugin

          Comment


            #6
            MattL0, how do you do it with EasyTrigger?

            Comment


              #7
              Here I add 1 to the virtual device Hallway litterbox,, which is ref 5678.
              Attached Files

              Comment

              Working...
              X