Counting the number of alarms within a time window or number of samples

In this example we will learn how to use the count function of the Formula node. In this example, the formula is defined as <count(HEADS, 5, minutes, coin_1.state)> with the threshold 10. That means that we will count the number of times the coin flipped to HEADS in the sliding window of 5 minutes. We shall toss the coin every 1 second. This is the coin sensor used for this example. It will turn HEADS if the random value is above .5.

var randomValue = Math.random();
var state;
if(randomValue >= 0.5)
else
state = "TAILS";

value = {
observedState:  state,
rawData : {  randomValue: randomValue}
};
send(null,value);

Let’s start the task from the template count in the periodic mode like this:

curl --user apiKey:apiSecret -H "Content-Type:application/json" -X POST -d '{
"name": "Count test1",
"template": "count",
"type": "periodic",
"frequency": 1000,

Here is the state view. We can observe that Formula node, calles CountHeads at one moment counted 10 times that the coin flipped to HEADS. If we look at the formula value we can also see the moment when the count crossed 10. We can also observed that the count was going up only when the random value, used to decided whether the coin is HEAD or TAILS was greater than 0.5. Let’s now test the “randomness”. We shall keep the polling period to 1 second, but the formula will now be <count(HEADS, 20, samples, coin_1.state)> and put the threshold again to 10. This time, the count goes over number of samples, not the time window. After about 20 samples, the Function node should randomly toggle between Below and Above. Let’s have a look: Amazing! We see that the Formula node at the beging was in state Below, and then at one moment, it starts randomly toggling between Above, Equal and Below state.

Let’s see what the formula computation was doing. Indeed, we see that the count at one moment reached 10, and after that, it was fluctuating around 10. Fun! 