Loop Counter not storing/ retrieving value properly

I am trying to set up a loop so that after an error is reported a certain number of times it sends a message to the admins.

Right now I have a mutate node that sets
{{Count}} = {{Count}}+1

and when count is greater than 3 I want it to send a sms, to monitor what is happening I have it make an event each time it goes through the loop:

The latest 3 messages are after implementing the {{count}} +1 logic. My question is how can I store the value of Count and compare it in my loop to make sure the message will get sent after 3 loops?

image

If it was working properly I would think it would look like

count = 2
count = 1
count = 3
count = 2
count = 1

Hey @Jackson_Miller,

I would suggest using Workflow Storage. You would create a number type key with the name count. Then in your workflow, you can use the Storage: Get to get the value, use a conditional node to check if it is above a certain value, and if it isn’t, then you can use the Storage: Set with the Increment Value option.

Let me know if this is something that will work for you!

Thank you,
Heath

1 Like

Is this per session on the Workflow? Like if we have 2 parallel devices triggering a workflow will they have their own individual Storage Item value for that Session?

Hi @Avinash_Gade and welcome to the Losant forums!

Workflow storage values update dynamically across simultaneous instances of a workflow.

In your example, if Device A and Device B have both triggered a workflow, any workflow storage values set in one instance of the workflow will immediately be available to the other instance of the workflow.

However, you should be cautious to avoid a race condition in which simultaneous workflow instances referencing the same storage key can interfere with each other. This usually happens when you use the Storage: Get Value Node to pull a value, run that value through one or more other nodes, and then use Storage: Set Value to update it. As a basic example, let’s say that you were trying to maintain a storage key called totalCount that is increased by a given number value in each execution of a workflow:

  1. Workflow Instance A and Workflow Instance B both use the Storage: Get Value Node to retrieve the totalCount value in workflow storage, which we’ll say is 100.

  2. Workflow Instance A takes the stored valued of 100 and manipulates this value (using, for example, the Math Node) to add 50 and then uses the Storage: Set Value Node to update totalCount to 150.

  3. Workflow Instance B, which pulled the value of 100 before Workflow Instance A updated the stored value to 150, manipulates this value with the Math Node to add 50 and then uses the Storage: Set Value Node to update totalCount to 150.

  4. The totalCount storage value is now 150 rather than 200, as the changes in Workflow Instance B overwrote the changes made by Workflow Instance A.

The above scenario might not always happen - if Workflow Instance A updated the stored value before it was pulled by Workflow Instance B, it would have the correct result.

These types of conflicts can be avoided by using the configuration options available in the Storage: Set Value Node. In the example above, if we had instead used the Increment Value option to increase the stored value by 50, the totalCount would have been saved properly as 200, regardless of the order in which the operations in the workflow were executed. This is because most single node operations are atomic.

Hopefully this answers your question!

1 Like