Function to trigger

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:
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

1 Like

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

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.

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

Basically I am testing for two values:

GridValues

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 :frowning:

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

10 > 3
3 < 10

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.

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 :slight_smile: but I think that is the correct equation?

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.

Sure:

Whats coming out the debug from MQTT
StringGridStatus

Version:

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

Switch Node:
SwitchNode2

Notification nodes

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

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

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?

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

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

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

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

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

[{"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 going to ask a really stupid question, I see you have the json for the flow:

[{"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:


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

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. 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!

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