# Function to trigger

**URL:** <https://discourse.nodered.org/t/function-to-trigger/53626>\
**Category:** General\
**Created:** [12 November 2021 09:37 UTC](https://discourse.nodered.org/t/function-to-trigger/53626 "2021-11-12T09:37:04Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [12 November 2021 09:37 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/1 "2021-11-12T09:37:04Z")

</div>

Hi all,

I have read through a few posts on the forum on how to trigger something from an ouput of a function JS function. Maybe I am even thinking of this the wrong way. Let me give you an example of what I am trying to do:

1. I am monitoring a topic from a MQTT service. The topic provides me a value of voltage of the grid.
2. What I am trying to do is I have some really simple code:

```auto
var payload = msg.payload
if (payload > 225) {
  msg.payload = "GridUp";
  node.status({fill:"green",shape:"dot",text:"connected"});
} else if (payload < 200) {
    msg.payload = "GridDown";
    node.status({fill:"red",shape:"ring",text:"disconnected"});
  }
return msg;

```

What I want to achieve is if the value is below 200 I want to trigger a notification through a notification service. So its quite a simple check if I am below 200 (volts) send notification, if above just show connected.

So my question(s) would be:

1. Is the function node the right node to do this with (for my own learning and future requirements)
2. If it is how would I trigger sending a notification (I use pushsafer and use this a lot so would just need to know how to trigger that node)
3. If the function node isn't the right node then what would be?

Thanking you

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 November 2021 10:39 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/2 "2021-11-12T10:39:41Z")

</div>

You can achieve most of that using standard nodes and no code:

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/376797b54878c5a08da5c68d547adede91cb70e0.jpeg)

I don't think it would give you the coloured node.status though ("The editor is an editor not a dashboard").

Or you can use a Function node. Whichever suits you.

I don't use Pushsaver but presumably you just have to send msg.payload to the Pushsafer node, maybe via a Filter node to block repeated notifications.

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 08:38 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/4 "2021-11-15T08:38:35Z")

</div>

Thank you for the response - So this is what I have tried:

 ![CheckFlow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f5cf5235d1f595c3163302a5104bfb0a6b1b058.jpeg)

Basically I am testing for two values:

![GridValues](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31cdebe22008a17996234a94e7b9e0a69d5e9043.jpeg)

If the grid voltage drops below - 200 ( \> 200) then execute the

> Grid Disconnected

push safer message

If the Grid voltage is higher (\< 250) than a certain value then send a message.

Having set it up that way (above) what I am getting now is its constantly sending messages regardless of what the real value is example, in my test system the voltage (value) fluctuates between 225 and 230, yet I keep getting both the messages I have linked for the flow of \> 200 and for \< 250.

What I want it to do is only send me the notification should the value drop below 200 or above 250.

What am I not understanding here ☹

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [15 November 2021 08:55 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/5 "2021-11-15T08:55:56Z")

</div>

> [@mynoderocks](#):
>
> If the grid voltage drops below - 200 ( \> 200) then execute the
> 
> > Grid Disconnected
> 
> push safer message
> 
> If the Grid voltage is higher (\< 250) than a certain value then send a message.

Don't you have your \> and \< back to front?

10 \> 3  
3 \< 10

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2021 09:27 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/6 "2021-11-15T09:27:46Z")

</div>

Also you are doing string (alphabetic) comparison whereas you should be using a number comparison, but you will need to make sure the values are numbers not strings. Feed the mqtt output into a debug node to check.

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 10:24 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/7 "2021-11-15T10:24:11Z")

</div>

You right in this instance I do thank you for the point out. Even though with this change is still sends both messages regardless if its \> or \< the value.

So what I have changed to now is  
\<ValueFrom\_MQTT\> \<= 200 --- which I read as the value is smaller than or equal to 200, then send Grid Disconnected notification.

\<ValueFrom\_MQTT\> \> 250 --- which I read as the value is larger than 250, then send a Grid Overload notification.

Now you have me guessing 🙂 but I think that is the correct equation?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2021 10:36 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/8 "2021-11-15T10:36:24Z")

</div>

Add a debug node showing what is coming from the MQTT node and show us what it says.  
Did you see my previous post?

**[Edit]** Or show us what is going into the Switch node if the MQTT is not immediately before the Switch.

Also tell us which version of node-red and which version of nodejs you are using.

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 11:27 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/9 "2021-11-15T11:27:51Z")

</div>

Sure:

**Whats coming out the debug from MQTT**  
 ![StringGridStatus](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8b5e02077a3958806bb8ae4fb6270ff00623cf0.jpeg)

**Version:**

```auto
15 Nov 12:35:32 - [info] Node-RED version: v2.0.1
15 Nov 12:35:32 - [info] Node.js version: v12.22.7
15 Nov 12:35:32 - [info] Linux 5.10.63-v7l+ arm LE

```

**MQTT Node**

![MQTT_Grid](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77cb48eb77d418291c046f468f276c365ef7564d.jpeg)

**Switch Node:**  
 ![SwitchNode2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e4c6c4072b82d16898fe1c607d127429453cf1a.jpeg)

**Notification nodes**

 ![GridStat](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83e2f19ad6337b25a2282759b1dd5cba3b57bb01.jpeg)

This seems to be the right setup now, I would have to drop the grid connection to fully test it but from what I can see this should work

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [15 November 2021 11:38 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/10 "2021-11-15T11:38:45Z")

</div>

Hmm, I don't like comparing numbers in string format  
No doubt "0" \< "200" but what about "30"?

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 11:51 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/11 "2021-11-15T11:51:39Z")

</div>

You 100% right however that's the format that's coming directly out of MQTT, any suggestions how you would approach it? Would you try some sort of converting to float first?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [15 November 2021 11:58 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/12 "2021-11-15T11:58:56Z")

</div>

If the mqtt output is not in your control, yes change it to float. I expect you can do that with jsonata in a change node (?) or else a function

```auto
msg.payload = +(msg.payload);
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2021 12:06 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/13 "2021-11-15T12:06:08Z")

</div>

Or, possibly easier to read, `msg.payload = Number(msg.payload)`.

Then change the types in the Switch node also, as I said earlier.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2021 12:12 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/14 "2021-11-15T12:12:16Z")

</div>

It shouldn't come from both of the outputs, given the data you are feeding in. I see you are using a very early 2.x version of node-red. There were a number of issues in early versions so I suggest you update to the latest.

This flow works for me, not sending to either output.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c8ee3a03ec6b300edc8b2513f69466e8acd93276.png)

```auto
[{"id":"abd5eb903e486764","type":"inject","z":"bdd7be38.d3b55","name":"String 228.4","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"228.4","payloadType":"str","x":160,"y":1120,"wires":[["5da205784765b00b"]]},{"id":"5da205784765b00b","type":"switch","z":"bdd7be38.d3b55","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"200","vt":"str"},{"t":"gt","v":"250","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":1120,"wires":[["51bc92d26d3a388a"],["cbc65de53c06b121"]]},{"id":"51bc92d26d3a388a","type":"debug","z":"bdd7be38.d3b55","name":"ONE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1080,"wires":[]},{"id":"cbc65de53c06b121","type":"debug","z":"bdd7be38.d3b55","name":"TWO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 13:50 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/15 "2021-11-15T13:50:17Z")

</div>

I am going to ask a really stupid question, I see you have the json for the flow:

```auto
[{"id":"abd5eb903e486764","type":"inject","z":"bdd7be38.d3b55","name":"String 228.4","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"228.4","payloadType":"str","x":160,"y":1120,"wires":[["5da205784765b00b"]]},{"id":"5da205784765b00b","type":"switch","z":"bdd7be38.d3b55","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"200","vt":"str"},{"t":"gt","v":"250","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":1120,"wires":[["51bc92d26d3a388a"],["cbc65de53c06b121"]]},{"id":"51bc92d26d3a388a","type":"debug","z":"bdd7be38.d3b55","name":"ONE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1080,"wires":[]},{"id":"cbc65de53c06b121","type":"debug","z":"bdd7be38.d3b55","name":"TWO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1160,"wires":[]}]

```

I am assuming I can import that and it will create the flow? If so how does one do that?

> Or, possibly easier to read, `msg.payload = Number(msg.payload)`  
> Is my understanding correct:

1. MQTT ---\> Change Node --\> `msg.payload = Number(msg.payload)`
2. Switch node then use the "Number" type to do the comparisons?

I have also updated Node Red - of course I didn't do the right thing read all the docs and lost all my flows - not an issue allows me to learn and recreate things the right way (its my Dev/Test server)

The version I have now - is this the latests - should be as its using the install script:

```auto

15 Nov 13:44:21 - [info] Node-RED version: v2.1.3
15 Nov 13:44:21 - [info] Node.js version: v12.22.7
15 Nov 13:44:21 - [info] Linux 5.10.63-v7l+ arm LE
15 Nov 13:44:22 - [info] Loading palette nodes

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2021 13:58 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/16 "2021-11-15T13:58:24Z")

</div>

> [@mynoderocks](#):
>
> lost all my flows

No you haven't, it is probably just using the wrong flows file. Look a bit further down the startup log and it will tell you what file it is using. If you look in your .node-red folder you should be able to find the original one, If you rename that to the one it is now looking for (or edit settings.js to use the original one) then you will have your flows back. You should be keeping a backup of the contents of the .node-red folder (apart from the node\_modules folder, which can be rebuilt).

To import the flow then copy it from the post (you can click on the little copy button that will appear at the right hand side of the flow if you hover over it), then in node-red select Import from the menu and import from clipboard.

The fact that you don't know that suggests that you may benefit from watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![mynoderocks](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mynoderocks](https://discourse.nodered.org/u/mynoderocks)\
**Post date:** [15 November 2021 14:21 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/17 "2021-11-15T14:21:52Z")

</div>

> [@Colin](#):
>
> The fact that you don't know that suggests that you may benefit from watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Thank you that is definitely something I will do this evening!

---

<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:** [14 January 2022 14:22 UTC](https://discourse.nodered.org/t/function-to-trigger/53626/18 "2022-01-14T14:22:40Z")

</div>

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