Split a value into two values

Hi guys,
today I have a very simple question for you:
I´ve a value that varies between +2600 and -2500.
I would like to distinguish this into two new values: if <0 than var "Unloading", if> 0 than var "Loading".

but unfortunately I can't do it :woozy_face:

thanks for help.

I think I got it with "split":

[{"id":"b6efc276.3b37","type":"switch","z":"9f6752b0.b55988","name":"","property":"payload.PAC3","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1430,"y":1940,"wires":[["4d18ea91.fe6d24"],["fbb45a39.7ca4d8"]]},{"id":"4d18ea91.fe6d24","type":"function","z":"9f6752b0.b55988","name":"","func":"msg.payload = msg.payload.PAC3;\nmsg.topic = \"Entladen\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1580,"y":1900,"wires":[["6d1bb8c1.0a8278"]]},{"id":"fbb45a39.7ca4d8","type":"function","z":"9f6752b0.b55988","name":"","func":"msg.payload = msg.payload.PAC3;\nmsg.topic = \"Beladen\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1580,"y":1960,"wires":[[]]}]

If you have better ideas, please tell me :slight_smile:

Hi Becker
Here is my solution. I would do it in one function like this:
Function with one output:

[{"id":"f44b690d.4c33b8","type":"function","z":"f6f2187d.f17ca8","name":"","func":"if (msg.payload.PAC3 >= 0){\n    msg = {\"payload\":msg.payload.PAC3,\"topic\":\"Entladen\"};\n    return msg;\n} else if (msg.payload.PAC3 < 0){\n    msg = {\"payload\":msg.payload.PAC3,\"topic\":\"Beladen\"};\n    return msg;\n} else {\n    return;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":100,"wires":[["1320800b.1a02e"]]}]

Function with two outputs:

[{"id":"f44b690d.4c33b8","type":"function","z":"f6f2187d.f17ca8","name":"","func":"if (msg.payload.PAC3 >= 0){\n    msg = {\"payload\":msg.payload.PAC3,\"topic\":\"Entladen\"};\n    return [msg, null];\n} else if (msg.payload.PAC3 < 0){\n    msg = {\"payload\":msg.payload.PAC3,\"topic\":\"Beladen\"};\n    return [null, msg];\n} else {\n    return;\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":640,"y":100,"wires":[["1320800b.1a02e"],["7fbc281d.69c638"]]}]```

A simple ternary conditional to set topic should be fine.

msg.topic = msg.payload.PAC3 < 0 ? "Beladen" : "Entladen";
msg.payload = msg.payload.PAC3;
return msg;
[{"id":"f2cd37f3.77f548","type":"inject","z":"5a245aa1.510164","name":"2300","props":[{"p":"payload.PAC3","v":"2300","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":3980,"wires":[["4d18ea91.fe6d24"]]},{"id":"f093cb4f.77554","type":"inject","z":"5a245aa1.510164","name":"-2300","props":[{"p":"payload.PAC3","v":"-2300","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":4040,"wires":[["4d18ea91.fe6d24"]]},{"id":"4d18ea91.fe6d24","type":"function","z":"5a245aa1.510164","name":"","func":"msg.topic = msg.payload.PAC3 < 0 ? \"Beladen\" : \"Entladen\";\nmsg.payload = msg.payload.PAC3;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":4000,"wires":[["3b67977c.a53c08"]]},{"id":"3b67977c.a53c08","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":3800,"wires":[]}]

Hi, both solutions doesn´t work :roll_eyes:

my solution works:

[{"id":"b6efc276.3b37","type":"switch","z":"9f6752b0.b55988","name":"","property":"payload.PAC3","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1350,"y":1940,"wires":[["4d18ea91.fe6d24"],["fbb45a39.7ca4d8"]]},{"id":"6d1bb8c1.0a8278","type":"debug","z":"9f6752b0.b55988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1870,"y":1820,"wires":[]},{"id":"4d18ea91.fe6d24","type":"function","z":"9f6752b0.b55988","name":"Entladen","func":"msg.payload = msg.payload.PAC3;\nmsg.topic = \"Entladen\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1480,"y":1920,"wires":[["b028ac79.76d8a"]]},{"id":"fbb45a39.7ca4d8","type":"function","z":"9f6752b0.b55988","name":"Beladen","func":"msg.payload = (msg.payload.PAC3 * -1);\nmsg.topic = \"Beladen\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1480,"y":1960,"wires":[["b028ac79.76d8a"]]},{"id":"e2b13662.528b18","type":"join","z":"9f6752b0.b55988","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1870,"y":1880,"wires":[["6d1bb8c1.0a8278"]]},{"id":"b028ac79.76d8a","type":"calculate","z":"9f6752b0.b55988","name":"Durchschnitt pro Minute","pauseType":"timed","calculation":"average","timeout":"10","timeoutUnits":"seconds","rate":"10","x":1690,"y":1940,"wires":[["f01d2ea8.e59cb"]]},{"id":"f01d2ea8.e59cb","type":"function","z":"9f6752b0.b55988","name":"runden","func":"var temp = msg.payload\nmsg.payload = parseFloat(temp.toFixed(0));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1880,"y":1940,"wires":[["e2b13662.528b18"]]}]

but there is one problem, the last value is always being stored in the object.

PAC3 is the Inverter output.
positive = unloading
negative = loading

never be both !

I need an object output with the correct value.
thats what is happening:

502 is the last value, and its being stored.
It must be set to "0" or delete.

Both solutions work
here is proof using your supplied fow and my example , they both give exactly the same output.


as seen in image both flows give same output.

Your new flow example is different and includes a non standard contrib node, so I can not add it, as you failed to name it


So without knowing the mystery node and what it is calculating, I don't think anyones example will work for you.

sry, it is only "node-red-contrib-calculate" - but you can delete it and "runden", it doesn´t matter for the result.

with out the calc node

[{"id":"f093cb4f.77554","type":"inject","z":"5a245aa1.510164","name":"-2300","props":[{"p":"payload.PAC3","v":"-2300","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":4040,"wires":[["4d18ea91.fe6d24"]]},{"id":"4d18ea91.fe6d24","type":"function","z":"5a245aa1.510164","name":"","func":"msg.topic = msg.payload.PAC3 < 0 ? \"Beladen\" : \"Entladen\";\nmsg.payload = msg.payload.PAC3;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":4000,"wires":[["dc737e2d.6fbe4","60a9ce87.71931"]]},{"id":"f2cd37f3.77f548","type":"inject","z":"5a245aa1.510164","name":"2300","props":[{"p":"payload.PAC3","v":"2300","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":3980,"wires":[["4d18ea91.fe6d24"]]},{"id":"dc737e2d.6fbe4","type":"debug","z":"5a245aa1.510164","name":"ternary","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":4080,"wires":[]},{"id":"60a9ce87.71931","type":"function","z":"5a245aa1.510164","name":"runden","func":"var temp = msg.payload\nmsg.payload = {[msg.topic]: parseFloat(temp.toFixed(0))};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":4000,"wires":[["dc737e2d.6fbe4"]]}]

grafik
only error comes out

Whats the flow and which function is causing the error, as you can see from my last post it works, so you must have change something or the input data

can we please focus on the problem: if the value PAC3 is switching between + and -, than the last output is beeing stored. I don't know how to get around that !

One of the new outputs ( "Entladen" or "Beladen" ) has to be zero.

PAC3 is from a buffer parser, the buffer parser is from Modbus:

{"topic":"PAC3","messageId":"606f5271f53dcc48e91efbd7","payload":507,"queueLengthByUnitId":{"unitId":3,"queueLength":0},"queueUnitId":3,"unitId":3,"modbusRequest":{"value":1617908337460,"unitid":"3","fc":4,"address":"30775","quantity":"2","messageId":"606f5271f53dcc48e91efbd7"},"responseBuffer":{"data":[0,507],"buffer":[0,0,1,251]},"_msgid":"bd027958.c0bdf8","originalPayload":[0,507],"specification":{"options":{"byteSwap":[],"resultType":"keyvalue","singleResult":true,"msgProperty":"payload","setTopic":true},"items":[{"type":"int32be","name":"PAC3","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"","id":0,"value":507}]},"values":[507],"objectResults":{"PAC3":{"type":"int32be","name":"PAC3","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"","id":0,"value":507}},"keyvalues":{"PAC3":507},"arrayResults":[{"type":"int32be","name":"PAC3","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"","id":0,"value":507}],"buffer":[0,0,1,251]}

There is no msg.payload.PAC3 in that object msg.payload is a integer.

P.s. when did we not focus on the problem?

So what should I do ?

If msg.payload in the object you posted is the value you require then change the function to use msg.payload.
in this example i have recreated the object you posted and fedd it into the altered function that targets msg.payload

[{"id":"79374974.a4c608","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":4140,"wires":[["cd76cbac.cb34"]]},{"id":"cd76cbac.cb34","type":"function","z":"5a245aa1.510164","name":"create object for testing","func":"msg = {\"topic\":\"PAC3\",\"messageId\":\"606f5271f53dcc48e91efbd7\",\"payload\":507,\"queueLengthByUnitId\":{\"unitId\":3,\"queueLength\":0},\"queueUnitId\":3,\"unitId\":3,\"modbusRequest\":{\"value\":1617908337460,\"unitid\":\"3\",\"fc\":4,\"address\":\"30775\",\"quantity\":\"2\",\"messageId\":\"606f5271f53dcc48e91efbd7\"},\"responseBuffer\":{\"data\":[0,507],\"buffer\":[0,0,1,251]},\"_msgid\":\"bd027958.c0bdf8\",\"originalPayload\":[0,507],\"specification\":{\"options\":{\"byteSwap\":[],\"resultType\":\"keyvalue\",\"singleResult\":true,\"msgProperty\":\"payload\",\"setTopic\":true},\"items\":[{\"type\":\"int32be\",\"name\":\"PAC3\",\"offset\":0,\"length\":1,\"offsetbit\":0,\"scale\":\"1\",\"mask\":\"\",\"id\":0,\"value\":507}]},\"values\":[507],\"objectResults\":{\"PAC3\":{\"type\":\"int32be\",\"name\":\"PAC3\",\"offset\":0,\"length\":1,\"offsetbit\":0,\"scale\":\"1\",\"mask\":\"\",\"id\":0,\"value\":507}},\"keyvalues\":{\"PAC3\":507},\"arrayResults\":[{\"type\":\"int32be\",\"name\":\"PAC3\",\"offset\":0,\"length\":1,\"offsetbit\":0,\"scale\":\"1\",\"mask\":\"\",\"id\":0,\"value\":507}],\"buffer\":[0,0,1,251]}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":4140,"wires":[["4d18ea91.fe6d24","dc737e2d.6fbe4"]]},{"id":"4d18ea91.fe6d24","type":"function","z":"5a245aa1.510164","name":"","func":"msg.topic = msg.payload < 0 ? \"Beladen\" : \"Entladen\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":4000,"wires":[["dc737e2d.6fbe4","60a9ce87.71931"]]},{"id":"dc737e2d.6fbe4","type":"debug","z":"5a245aa1.510164","name":"ternary","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":4080,"wires":[]},{"id":"60a9ce87.71931","type":"function","z":"5a245aa1.510164","name":"runden","func":"var temp = msg.payload\nmsg.payload = {[msg.topic]: parseFloat(temp.toFixed(0))};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":4000,"wires":[["dc737e2d.6fbe4"]]}]

Hi,
Please excuse me for not having tried it in so long.

your function works, it differentiates between loading and unloading.
but I need always (every 5s when modbus come in) two outputs:

value 0 or above = unloading with actual value & load = 0
value negative = loading with actual value *-1 & unloading = 0

I tried some, buth I don´t get it:

msg.topic = msg.payload.PAC3 < 0 ? "Entladen" : "Beladen";
var Be
var En
if (msg.topic == "Beladen") {
    Be = msg.payload.PAC3 * -1;
    En = null;
} else {
    Be = null;
    En = msg.payload.PAC3;
}
return msg;

or

var PAC3 = msg.payload;
var Be;
var En;
if(PAC3 < 0 ) {
    Be = (PAC3 * -1);
    En = null;
} else {
    En = PAC3;
    Be = null
}
return [Be,En];

I need every 5s (when modbus comes in) and object for example with:

Entladen = 0
Beladen = 1234

or

Entladen = 1111
Beladen = 0

or both 0

maybe @Steve-Mcl has an idea ?

Where do you need the outputs?

image

A better solution might be...

msg.topic = msg.payload.PAC3 < 0 ? "Entladen" : "Beladen";
msg.payload = msg.payload.PAC3; //store value in payload

//if payload is < 0 then * by -1
if(msg.payload < 0 ) {
    msg.payload = (msg.payload * -1);
}

return msg;

As Colin said, it all depends on what you want to come out of the function.

ASSUMPTION: the value you want to check is in msg.payload.PAC3 and you want the result to come out in msg.payload;

:triumph: I don´t get the basics of JS.
Every time I think I have understood, Node-Red shows me that I have not understood anything.

var msg1; 
var msg2;
var PAC3 = msg.payload;
var Bel;
var Ent;
if(PAC3 < 0 ) {
    Bel = (PAC3 * -1);
    Ent = null;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
} else {
    Ent = PAC3;
    Bel = null;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
}
return [msg1,msg2];

or

var msg1; 
var msg2;
var PAC3 = msg.payload.PAC3;
var Bel;
var Ent;
if(PAC3 < 0 ) {
    Bel = (PAC3 * -1);
    Ent = null;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
} else {
    Ent = PAC3;
    Bel = null;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
}
return [msg1,msg2];

both doesn´t work,
always get "TypeError: Cannot set property 'payload' of undefined" or just "undefined"

I just want to show these two values in my dashboard as:
Beladen = (+) ... W
Entladen = (+) ... W

one of both always has to be 0.

and furthermore I would like to write it as an object in Influx.

Now I connected the function directly to the buffer parser:

var PAC3 = msg.payload.PAC3;
var msg1 = {}; 
var msg2 = {};
var Bel;
var Ent;
if(PAC3 < 0 ) {
    Bel = (PAC3 * -1);
    Ent = 0;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
} else {
    Ent = PAC3;
    Bel = 0;
    msg1.payload = Bel;
    msg2.payload = Ent;
    msg1.topic = "Beladen";
    msg2.topic = "Entladen";
}
return [msg1,msg2];

Now I get:
grafik

better, but one is left

To set a property of an object you must first make the variable an object

E.g...

var msg1 = {};
msg1.payload = "now it works";

You can return 2 messages as you are or 1 message with multiple properties (usually in the payload)

E.g...

msg.payload ={};
msg.payload.beladen = 123;
msg.payload.entladen = 456;
return msg;