Msg.payload is changing two outputs

Hi guys,

I'm pretty new to both node red and coding so I apologize if this is a stupid question. I'm using a LOGO 8 and node red to mock up a vibration analysis system for bearings. As I don't have any sensors at the moment I'm just injecting values into flag (M) blocks and using the value of that to change an output message using an if statement by monitoring the msg.payload they output. The problem I've come up against is that when I have 2 "bearings" and I click the inject on one of them, the message changes for both. Obviously this is because the msg.payload is changing and my if statement doesn't care where it is from, just that it is changing. My question is, how can I split my msg depending on where the input is from or is there a better way to do this? JS code and a picture of flow are posted below.


if (msg.payload >= 101) {
    msg.payload = "V. High Hertz - Bearing failure imminent!"
} else if (msg.payload <= 100 && msg.payload >= 81) {
    msg.payload = "High Hz - Bearing  failure detected."
} else if (msg.payload <= 80 && msg.payload >= 41) {
    msg.payload = "Bearing Hz within parameters"
} else if (msg.payload <= 40 && msg.payload >= 20) {
    msg.payload = "Low Hz"
} else if (msg.payload == 0) {
    msg.payload = "Machine not running."
}

return msg;

Looking at the picture of your flow, I doubt that this is the case.
If you post the flow data here (export it from the editor via the menu & please format it as "preformatted text"), we'll be able to have a closer look.

I assume this is what you want?

[
    {
        "id": "c565ec84b57dc443",
        "type": "tab",
        "label": "Vibration Analysis Monitoring System",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "dd6c88d00ca9774d",
        "type": "function",
        "z": "c565ec84b57dc443",
        "name": "Hz/If monitor",
        "func": "if (msg.payload >= 101) {\n    msg.payload = \"V. High Hertz - Bearing failure imminent!\"\n} else if (msg.payload <= 100 && msg.payload >= 81) {\n    msg.payload = \"High Hz - Bearing  failure detected.\"\n} else if (msg.payload <= 80 && msg.payload >= 41) {\n    msg.payload = \"Bearing Hz within parameters\"\n} else if (msg.payload <= 40 && msg.payload >= 20) {\n    msg.payload = \"Low Hz\"\n} else if (msg.payload == 0) {\n    msg.payload = \"Machine not running.\"\n}\n\nreturn msg;\n\nvar hzSpike = 0;\n\nif (msg.payload > 100) {\n    hzSpike = hzSpike + 1\n}\n\nif (hzSpike >= 10) {\n    msg.payload = \"Hz spiking detected!\"\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 270,
        "y": 20,
        "wires": [
            [
                "8a8adb8828852a4a",
                "bba0907895a46563"
            ]
        ]
    },
    {
        "id": "d749b6750dfcc567",
        "type": "s7 in",
        "z": "c565ec84b57dc443",
        "endpoint": "30d25704f03d7b38",
        "mode": "single",
        "variable": "Bearing 1",
        "diff": true,
        "name": "",
        "x": 60,
        "y": 20,
        "wires": [
            [
                "dd6c88d00ca9774d",
                "224192b56bea68b8"
            ]
        ]
    },
    {
        "id": "06b438c17f8bc55a",
        "type": "s7 out",
        "z": "c565ec84b57dc443",
        "endpoint": "30d25704f03d7b38",
        "variable": "Bearing 1",
        "name": "",
        "x": 380,
        "y": 280,
        "wires": []
    },
    {
        "id": "9f409172000326fa",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "971e59507afd7d4d",
        "order": 12,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "101 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "101",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 220,
        "wires": [
            [
                "06b438c17f8bc55a"
            ]
        ]
    },
    {
        "id": "a3b3f675562eaa8f",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "971e59507afd7d4d",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "0 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "0",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 460,
        "wires": [
            [
                "06b438c17f8bc55a"
            ]
        ]
    },
    {
        "id": "8a8adb8828852a4a",
        "type": "ui_text",
        "z": "c565ec84b57dc443",
        "group": "971e59507afd7d4d",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Bearing 1",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 440,
        "y": 20,
        "wires": []
    },
    {
        "id": "bba0907895a46563",
        "type": "debug",
        "z": "c565ec84b57dc443",
        "name": "debug 7",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 80,
        "wires": []
    },
    {
        "id": "656e8e331da91c7e",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "971e59507afd7d4d",
        "order": 10,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "81 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "81",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 280,
        "wires": [
            [
                "06b438c17f8bc55a"
            ]
        ]
    },
    {
        "id": "ed467d1807ef14ee",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "971e59507afd7d4d",
        "order": 8,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "41 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "41",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 340,
        "wires": [
            [
                "06b438c17f8bc55a"
            ]
        ]
    },
    {
        "id": "186660b9ee34d2a8",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "971e59507afd7d4d",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "20 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "20",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 400,
        "wires": [
            [
                "06b438c17f8bc55a"
            ]
        ]
    },
    {
        "id": "114499ce4ec5dea1",
        "type": "function",
        "z": "c565ec84b57dc443",
        "name": "Hz/If monitor",
        "func": "if (msg.payload >= 101) {\n    msg.payload = \"V. High Hertz - Bearing failure imminent!\"\n} else if (msg.payload <= 100 && msg.payload >= 81) {\n    msg.payload = \"High Hz - Bearing  failure detected.\"\n} else if (msg.payload <= 80 && msg.payload >= 41) {\n    msg.payload = \"Bearing Hz within parameters\"\n} else if (msg.payload <= 40 && msg.payload >= 20) {\n    msg.payload = \"Low Hz\"\n} else if (msg.payload == 0) {\n    msg.payload = \"Machine not running.\"\n}\n\nreturn msg;\n\nvar hzSpike = 0;\n\nif (msg.payload > 100) {\n    hzSpike = hzSpike + 1\n}\n\nif (hzSpike >= 10) {\n    msg.payload = \"Hz spiking detected!\"\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 270,
        "y": 120,
        "wires": [
            [
                "d3937371f8ec1b69",
                "a7dbc91be690bfb6"
            ]
        ]
    },
    {
        "id": "05e35c983b0b2532",
        "type": "s7 in",
        "z": "c565ec84b57dc443",
        "endpoint": "30d25704f03d7b38",
        "mode": "single",
        "variable": "Bearing 2",
        "diff": true,
        "name": "",
        "x": 60,
        "y": 120,
        "wires": [
            [
                "114499ce4ec5dea1",
                "13cf828a78d5300c"
            ]
        ]
    },
    {
        "id": "d3937371f8ec1b69",
        "type": "ui_text",
        "z": "c565ec84b57dc443",
        "group": "3f7cc53c64571341",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Bearing 2",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 440,
        "y": 120,
        "wires": []
    },
    {
        "id": "a7dbc91be690bfb6",
        "type": "debug",
        "z": "c565ec84b57dc443",
        "name": "debug 8",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 180,
        "wires": []
    },
    {
        "id": "224192b56bea68b8",
        "type": "debug",
        "z": "c565ec84b57dc443",
        "name": "debug 9",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 200,
        "y": 80,
        "wires": []
    },
    {
        "id": "13cf828a78d5300c",
        "type": "debug",
        "z": "c565ec84b57dc443",
        "name": "debug 10",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 200,
        "y": 180,
        "wires": []
    },
    {
        "id": "f5313920b4045f5a",
        "type": "s7 out",
        "z": "c565ec84b57dc443",
        "endpoint": "30d25704f03d7b38",
        "variable": "Bearing 2",
        "name": "",
        "x": 760,
        "y": 400,
        "wires": []
    },
    {
        "id": "159f9fbbea0c0bda",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "3f7cc53c64571341",
        "order": 12,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "101 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "101",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 450,
        "y": 340,
        "wires": [
            [
                "f5313920b4045f5a"
            ]
        ]
    },
    {
        "id": "1b148bca307ec808",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "3f7cc53c64571341",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "0 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "0",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 450,
        "y": 580,
        "wires": [
            [
                "f5313920b4045f5a"
            ]
        ]
    },
    {
        "id": "7d20e59f99f14514",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "3f7cc53c64571341",
        "order": 10,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "81 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "81",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 450,
        "y": 400,
        "wires": [
            [
                "f5313920b4045f5a"
            ]
        ]
    },
    {
        "id": "316718458959d3bf",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "3f7cc53c64571341",
        "order": 8,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "41 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "41",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 450,
        "y": 460,
        "wires": [
            [
                "f5313920b4045f5a"
            ]
        ]
    },
    {
        "id": "5dafba5e5fc206a4",
        "type": "ui_button",
        "z": "c565ec84b57dc443",
        "name": "",
        "group": "3f7cc53c64571341",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "20 Hz Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "20",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 450,
        "y": 520,
        "wires": [
            [
                "f5313920b4045f5a"
            ]
        ]
    },
    {
        "id": "30d25704f03d7b38",
        "type": "s7 endpoint",
        "transport": "iso-on-tcp",
        "address": "192.168.0.3",
        "port": "102",
        "rack": "0",
        "slot": "2",
        "localtsaphi": "02",
        "localtsaplo": "00",
        "remotetsaphi": "03",
        "remotetsaplo": "00",
        "connmode": "tsap",
        "adapter": "",
        "busaddr": "2",
        "cycletime": "1000",
        "timeout": "2000",
        "name": "VAMS",
        "vartable": [
            {
                "addr": "DB1,WORD1104",
                "name": "Bearing 1"
            },
            {
                "addr": "DB1,WORD1104.1",
                "name": "Bearing 2"
            }
        ]
    },
    {
        "id": "971e59507afd7d4d",
        "type": "ui_group",
        "name": "Bearing 1",
        "tab": "104e99694bfd0fcc",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "3f7cc53c64571341",
        "type": "ui_group",
        "name": "Bearing 2",
        "tab": "104e99694bfd0fcc",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "104e99694bfd0fcc",
        "type": "ui_tab",
        "name": "Vibration Analysis Monitoring System",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

@whelpleyjf

Are you sure your variables are configured correctly?
I guess (!!) they are not ... as the In node for Bearing 2 shows a different reading than the Out node ... but the same as Bearing 1. This might yet be a coincidence...

Um....

if (msg.payload >= 101) {
    msg.payload = "V. High Hertz - Bearing failure imminent!"
} else if (msg.payload <= 100 && msg.payload >= 81) {
    msg.payload = "High Hz - Bearing  failure detected."
} else if (msg.payload <= 80 && msg.payload >= 41) {
    msg.payload = "Bearing Hz within parameters"
} else if (msg.payload <= 40 && msg.payload >= 20) {
    msg.payload = "Low Hz"
} else if (msg.payload == 0) {
    msg.payload = "Machine not running."
}

return msg;

Won't you need to add break in there?

Something like:

if (msg.payload >= 101) {
    msg.payload = "V. High Hertz - Bearing failure imminent!"
    break;
} else if (msg.payload <= 100 && msg.payload >= 81) {
    msg.payload = "High Hz - Bearing  failure detected."
    break;
} else if (msg.payload <= 80 && msg.payload >= 41) {
    msg.payload = "Bearing Hz within parameters"
    break;
} else if (msg.payload <= 40 && msg.payload >= 20) {
    msg.payload = "Low Hz"
    break;
} else if (msg.payload == 0) {
    msg.payload = "Machine not running."
}

return msg;

Because it would - otherwise - test ALL of them. Which is not what you want - is it?

Sorry.

The else fixes that - yes?

Break is only for switch blocks. You don't need it for if-else

1 Like

Yeah, I sort of realised that after posting.
That's why I mentioned it.

Maybe I should have deleted the post.
Thanks though for the clarification.

Hi, I'm trying to fiddle around with this to see what the problem is. I've tried resetting everything and checking that it is configured correctly. I cannot see where I could have misconfigured it unless I have somehow misunderstood how the variables are addressed. Here are some photos of my S7 In and S7 out blocks. The variables are monitoring the values of 2 flags (or merker).


Separately, can someone clarify how the msg variable works? Is it a global variable or does each "flow" (by which I mean connected line of nodes) have it's own msg variable?


msg itself is non-global.
If you put the same object into several msgs, this object will be passed (as always) by reference and act in some way "global".

What happens, if you change the definition of Bearing 1 into something "weird".
Does this alter value read by the In node of Bearing 2 as well?
If so, you know there's something wrong with the way you defined those variables...

Excuse my ignorance, but when you say definition, what exactly do you mean? The name I choose in the variables tab after putting in the address?

Here's how the variables are assigned. Again, I'm new to this but after reading some documentation this is my understanding of how I address flags that I've set up in the LOGO!softcomfort software.
Screenshot 2023-03-25 131824

Sorry. Most probably my proposal wasn't clear enough: I was referring "definition" to the DB1,INT1104.

Understood. So I'm using this list I found online to define my addresses.

Logo-VM-Adressen — IP-Symcon :: Automatisierungssoftware

I've addressed them both as DB1,INTxxxx as I'm trying to pick up integer values.

I've already tried changing Bearing 1 to DB1,INT1104.0 but this throws an error.

I'm rather confused as to what I am doing wrong here but I think you are on the right track.

I've the impression, Bearing 1 works / worked properly.
What happens when you change the definition of Bearing 2 ?

I've managed to make some progress. I changed the flags in LOGO!softcomfort from M1 and M2 to M1 and M9. I also played around with the variable type in node-red and found that BYTE almost works. My only problem is now that when I press the button to inject a value, the value recorded is 1 less than what I injected. So if inject 81 I it shows 80. Strangely though the 0 and 20 buttons inject the correct value. Any idea what is going on here?

Not at all; just guessing: Could it be that the Hz figure is rounded down to the next 10th value? What happens, when you inject e.g. 83 ? Is it 82 or 80 as well?

Injecting 79 shows 78, injecting 80 shows 80, 81 shows 80, 82 shows 82, 83 shows 82. Really have no idea what is going on here. I'm glad it's not just me who is confused.

Is there a pattern in this data?
Looks like it shows only even numbers?