# Trigger Alarm If Element Zero in Array is Less Than -3

**URL:** <https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018>\
**Category:** General\
**Tags:** function-node\
**Created:** [9 April 2022 03:20 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018 "2022-04-09T03:20:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 03:20 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/1 "2022-04-09T03:20:32Z")

</div>

Hello All,

I need to trigger an alert if the element zero in the array is less than -3, I created a function but it is not displaying the alert. What I'm doing wrong? Thank you all.

```auto
[
    {
        "id": "181547422299f50a",
        "type": "inject",
        "z": "0685c3c587aa4084",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "start",
        "payload": "1",
        "payloadType": "num",
        "x": 130,
        "y": 320,
        "wires": [
            [
                "c32fdfcd6980d88b"
            ]
        ]
    },
    {
        "id": "f42e3850419a1abf",
        "type": "csv",
        "z": "0685c3c587aa4084",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "",
        "multi": "mult",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 550,
        "y": 280,
        "wires": [
            [
                "845aa6dfe380c903"
            ]
        ]
    },
    {
        "id": "845aa6dfe380c903",
        "type": "function",
        "z": "0685c3c587aa4084",
        "name": "graph generate",
        "func": "let payload=msg.payload;\n\nlet data = [[],[]];\nfor(let i=0;i<payload.length;i++){\n let time = new Date(\"1970-01-01T\"+payload[i].TIME.replace(\".\",\":\").padEnd(8,\"0\"));\n data[0].push({\"x\": time, \"y\": payload[i].IA});\n data[1].push({\"x\": time, \"y\": payload[i].IB});\n}\nlet labels = msg.columns.split(\",\").slice(1);\n\nmsg.payload=[{\n\"series\": labels,\n\"data\":data,\n\"labels\": labels\n}];\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 800,
        "y": 320,
        "wires": [
            [
                "980b469730d08203",
                "66c6aff13893e03c",
                "bee42052adfe9674"
            ]
        ]
    },
    {
        "id": "980b469730d08203",
        "type": "ui_chart",
        "z": "0685c3c587aa4084",
        "name": "",
        "group": "2ac8e3fa.8b8584",
        "order": 5,
        "width": 0,
        "height": 0,
        "label": "Time Graph Test",
        "chartType": "line",
        "legend": "true",
        "xformat": "HH:mm:ss",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": "10",
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#400000",
            "#ff8000",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 880,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "66c6aff13893e03c",
        "type": "debug",
        "z": "0685c3c587aa4084",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1070,
        "y": 200,
        "wires": []
    },
    {
        "id": "c32fdfcd6980d88b",
        "type": "template",
        "z": "0685c3c587aa4084",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "TIME,IA,IB\n09:21.3,-3.984,2.188\n09:22.3,-2.984,1.406\n09:23.3,-1.75,0.703",
        "output": "str",
        "x": 360,
        "y": 320,
        "wires": [
            [
                "f42e3850419a1abf"
            ]
        ]
    },
    {
        "id": "bee42052adfe9674",
        "type": "function",
        "z": "0685c3c587aa4084",
        "name": "triggerAlarm",
        "func": "var payload=msg.payload[0].IA;\nvar alarm_flag=context.get(\"alarm_flag\");\nif(typeof alarm_flag==\"undefined\")\nalarm_flag=false;\n\nif (payload<-3 && !alarm_flag)\n{\n alarm_flag=true;\n msg.alarm=1;\n context.set(\"alarm_flag\",alarm_flag);\n return msg;\n}\nif (payload<=-3 && alarm_flag)\n{\n alarm_flag=false;\n msg.alarm=0;\n context.set(\"alarm_flag\",alarm_flag);\n return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 870,
        "y": 440,
        "wires": [
            [
                "66c6aff13893e03c"
            ]
        ]
    },
    {
        "id": "2ac8e3fa.8b8584",
        "type": "ui_group",
        "name": "Default",
        "tab": "ebb1ed1c.5ebe2",
        "order": 1,
        "disp": true,
        "width": "12",
        "collapse": false,
        "className": ""
    },
    {
        "id": "ebb1ed1c.5ebe2",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 April 2022 07:35 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/2 "2022-04-09T07:35:04Z")

</div>

After the first function `msg.payload[0].IA` does not exsist. Try `msg.payload[0].data[0][0].y`  
Or move the wire to the alarm function to before the chart function.

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 13:36 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/3 "2022-04-09T13:36:49Z")

</div>

Thank you. I tried it but it is not displaying in the debug window the number 1 when detects a value less than -3. It's not triggering the alarm.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 April 2022 13:48 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/4 "2022-04-09T13:48:59Z")

</div>

Its returned 1 and 0 in msg.alarm for me.  
But i have altered your code after I ran it, as the second if should be an if else, as only one can be true at a time. I also removed the returns from inside the ifs, as if none where true the function would never return.  
returned message

```auto
{"_msgid":"af46189f.0c9a08","payload":[{"series":["IA","IB"],"data":[[{"x":"1970-01-01T08:21:30.000Z","y":-3.984},{"x":"1970-01-01T08:22:30.000Z","y":-2.984},{"x":"1970-01-01T08:23:30.000Z","y":-1.75}],[{"x":"1970-01-01T08:21:30.000Z","y":2.188},{"x":"1970-01-01T08:22:30.000Z","y":1.406},{"x":"1970-01-01T08:23:30.000Z","y":0.703}]],"labels":["IA","IB"]}],"topic":"start","columns":"TIME,IA,IB","alarm":1}

```

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 14:12 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/5 "2022-04-09T14:12:33Z")

</div>

I'm not sure If I understood you properly. This is the current function I have. Thank you.

```auto
var payload=msg.payload[0].data[0][0];
var alarm_flag=context.get("alarm_flag");
if(typeof alarm_flag=="undefined")
alarm_flag=false;

if (payload<-2 && !alarm_flag)
{
    alarm_flag=true;
    msg.alarm=1;
    context.set("alarm_flag",alarm_flag);
    return msg;
}
if (payload<=-2 && alarm_flag)
{
    alarm_flag=false;
    msg.alarm=0;
    context.set("alarm_flag",alarm_flag);
    return msg;
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 April 2022 14:14 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/6 "2022-04-09T14:14:34Z")

</div>

```auto
[{"id":"c32fdfcd6980d88b","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"TIME,IA,IB\n09:21.3,-3.984,2.188\n09:22.3,-2.984,1.406\n09:23.3,-1.75,0.703","output":"str","x":330,"y":2720,"wires":[["f42e3850419a1abf"]]},{"id":"181547422299f50a","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"start","payload":"1","payloadType":"num","x":100,"y":2720,"wires":[["c32fdfcd6980d88b"]]},{"id":"f42e3850419a1abf","type":"csv","z":"bf9e1e33.030598","name":"","sep":",","hdrin":true,"hdrout":"","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":520,"y":2680,"wires":[["845aa6dfe380c903"]]},{"id":"845aa6dfe380c903","type":"function","z":"bf9e1e33.030598","name":"graph generate","func":"let payload=msg.payload;\n\nlet data = [[],[]];\nfor(let i=0;i<payload.length;i++){\n let time = new Date(\"1970-01-01T\"+payload[i].TIME.replace(\".\",\":\").padEnd(8,\"0\"));\n data[0].push({\"x\": time, \"y\": payload[i].IA});\n data[1].push({\"x\": time, \"y\": payload[i].IB});\n}\nlet labels = msg.columns.split(\",\").slice(1);\n\nmsg.payload=[{\n\"series\": labels,\n\"data\":data,\n\"labels\": labels\n}];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":2720,"wires":[["bee42052adfe9674"]]},{"id":"bee42052adfe9674","type":"function","z":"bf9e1e33.030598","name":"triggerAlarm","func":"var payload=msg.payload[0].data[0][0].y;\nvar alarm_flag=context.get(\"alarm_flag\");\nif(typeof alarm_flag==\"undefined\")\nalarm_flag=false;\n\nif (payload<-3 && !alarm_flag)\n{\n alarm_flag=true;\n msg.alarm=1;\n context.set(\"alarm_flag\",alarm_flag);\n}\nelse if (payload<=-3 && alarm_flag)\n{\n alarm_flag=false;\n msg.alarm=0;\n context.set(\"alarm_flag\",alarm_flag);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":2840,"wires":[["66c6aff13893e03c"]]},{"id":"66c6aff13893e03c","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1040,"y":2600,"wires":[]}]

```

here i edited your flow.

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 14:15 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/7 "2022-04-09T14:15:53Z")

</div>

Let me check. Thank you!

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 14:21 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/8 "2022-04-09T14:21:36Z")

</div>

I got it! Thank you. However, if I have many values less than -3 in x amount of time. How can I trigger them?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 April 2022 14:59 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/9 "2022-04-09T14:59:30Z")

</div>

I would ask how you receive this reading, as it may be easier to do it as you receive them.

Best to ask these questions at beginning, rather than keep moving the goal post.

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 17:33 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/10 "2022-04-09T17:33:56Z")

</div>

Thanks! I understand. The idea is to get the reading from a raspberry pi using MQTT

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 April 2022 18:26 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/11 "2022-04-09T18:26:55Z")

</div>

If the value comes in via mqtt, You could join them and out put the join in x seconds, then just count the number of values under -3.  
e,g.

```auto
[{"id":"bd7aaa2d.cbfbd8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-3.7","payloadType":"num","x":210,"y":2220,"wires":[["68e32702.51b48"]]},{"id":"68e32702.51b48","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":2240,"wires":[["ff762ade.84ece"]]},{"id":"ff762ade.84ece","type":"switch","z":"bf9e1e33.030598","name":"","property":"$count($$.payload[$<-3])","propertyType":"jsonata","rules":[{"t":"gte","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":2240,"wires":[["034205c1528902a8"]]},{"id":"dca7cb12.187888","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-2","payloadType":"num","x":210,"y":2260,"wires":[["68e32702.51b48"]]},{"id":"034205c1528902a8","type":"debug","z":"bf9e1e33.030598","name":"4 messages below -3 in 5 seconds","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":2300,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![peterblue22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peterblue22/32/59753_2.png) [@peterblue22](https://discourse.nodered.org/u/peterblue22)\
**Post date:** [9 April 2022 18:58 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/12 "2022-04-09T18:58:33Z")

</div>

Cool! Thank you! Let me try. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 April 2022 18:59 UTC](https://discourse.nodered.org/t/trigger-alarm-if-element-zero-in-array-is-less-than-3/61018/13 "2022-04-23T18:59:26Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
