Page 1 of 1

How to put result of calculation into mqtt command

Posted: Sunday 09 March 2025 15:54
by sailmich
Hello,
I'm using node red to regulate fan speed I installed under my water heaters. It's running well. Till now I regulate the fan speed in steps e.g. 30/40/50%. I would like to do this with calculation in relation to the input temperature. E.g. 30°C => 40% fan speed and 35°C => 60% fan speed.
My problem is how can I get the calculated result into the function node to sent it to the fan.

Spoiler: show

Code: Select all

[
    {
        "id": "cf3ed0c4e266eb23",
        "type": "change",
        "z": "27bd8ea24b66cf31",
        "name": "calculate consumption",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload*4-80",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1600,
        "y": 840,
        "wires": [
            [
                "864087b683e6aecc",
                "f60883853089911c"
            ]
        ]
    },
    {
        "id": "dee39cb0daa74649",
        "type": "function",
        "z": "27bd8ea24b66cf31",
        "name": "SZ_40%",
        "func": "msg.topic = \"cmnd/tasmota_07235490/DIMMER\";\nmsg.payload = \"40\";\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1640,
        "y": 760,
        "wires": [
            [
                "29a2b83b8fe6679b"
            ]
        ]
    },
    {
        "id": "b685926d288f295d",
        "type": "inject",
        "z": "27bd8ea24b66cf31",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "SZ_Vorlauf",
        "payload": "35",
        "payloadType": "num",
        "x": 1390,
        "y": 840,
        "wires": [
            [
                "cf3ed0c4e266eb23",
                "dee39cb0daa74649",
                "08a697749e95080c"
            ]
        ]
    },
    {
        "id": "08a697749e95080c",
        "type": "debug",
        "z": "27bd8ea24b66cf31",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1580,
        "y": 900,
        "wires": []
    },
    {
        "id": "1ba62f886d0ad6f5",
        "type": "function",
        "z": "27bd8ea24b66cf31",
        "name": "SZ_Variable%",
        "func": "msg.topic = \"cmnd/tasmota_07235490/DIMMER\";\nmsg.payload = msg.payload.payload;\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1940,
        "y": 840,
        "wires": [
            [
                "7ccc2104d4956d9a"
            ]
        ]
    },
    {
        "id": "29a2b83b8fe6679b",
        "type": "debug",
        "z": "27bd8ea24b66cf31",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1800,
        "y": 760,
        "wires": []
    },
    {
        "id": "b66ecb5d3146f17a",
        "type": "comment",
        "z": "27bd8ea24b66cf31",
        "name": "working",
        "info": "",
        "x": 1630,
        "y": 720,
        "wires": []
    },
    {
        "id": "bb672a74f1f6887a",
        "type": "comment",
        "z": "27bd8ea24b66cf31",
        "name": "need help",
        "info": "",
        "x": 1920,
        "y": 800,
        "wires": []
    },
    {
        "id": "f60883853089911c",
        "type": "debug",
        "z": "27bd8ea24b66cf31",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1780,
        "y": 900,
        "wires": []
    },
    {
        "id": "864087b683e6aecc",
        "type": "json",
        "z": "27bd8ea24b66cf31",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1790,
        "y": 840,
        "wires": [
            [
                "1ba62f886d0ad6f5"
            ]
        ]
    },
    {
        "id": "7ccc2104d4956d9a",
        "type": "debug",
        "z": "27bd8ea24b66cf31",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 2120,
        "y": 840,
        "wires": []
    }
]

Re: How to put result of calculation into mqtt command

Posted: Sunday 09 March 2025 21:27
by FireWizard
Hi @sailmich,
My problem is how can I get the calculated result into the function node to sent it to the fan.
Question

Where do you do that calculation?
Is it a separate program, that gives you a result on the command line.
Is it on the same computer/Raspberry Pi, which runs Node-RED
Or is it done in Node-RED itself? If so, can you post the flow?

Regards

Re: How to put result of calculation into mqtt command

Posted: Monday 10 March 2025 6:03
by sailmich
Hello FireWizard,
a sample of the flow is in my first post. I got the temperature from a Shelly addon with node mqtt in. Than I'm doing the calculation within a change node. I would like to sent it with mqtt to a Shelly.
Cheers

Re: How to put result of calculation into mqtt command

Posted: Monday 10 March 2025 9:14
by FireWizard
Hi, @sailmich,

Actually this has no relationship with Domoticz.

You receive the temperature from a Shelly addon in Node-Red by MQTT input node. You do the calculation in a Change node.
The result you want to send to a MQTT output node to a Shelly device.

Which Shelly device? A Gen 1 or Gen 2/3 device.
Type number?

Is de input addon connected to the same device as you want to control?

Regards

Re: How to put result of calculation into mqtt command

Posted: Monday 10 March 2025 19:09
by FireWizard
Hi, @sailmich,

I loaded your flow in my test system with Node RED.

Honestly said, I do not understand what you really want to achieve.

Let me explain.

You "Inject" a numerical value (35). I assume that this shall represent the temperature, so 35°C.
This has a topic, called "SZ_Vorlauf", which I think means "Forward".

You send this to a "Function" node as follows:

Code: Select all

msg.topic = "cmnd/tasmota_07235490/DIMMER";
msg.payload = "40";
return msg;
The result is "40". I understand this, as probably most readers will do.
It will always be "40". But why the topic with DIMMER?

You added a comment, "Working". Okay, I understand.

Now the lower stream.

You "Inject" 35. and send that to a "Change" node and with a JSONata expressions ((35 x 4) -80) = (140 -80).
This results in a numerical value 60. It does not surprise me.
You put this value of 60 in a JSON node and the output is ...."60". I understand this.

Then you put this in a "Function" node with the following contents:

Code: Select all

msg.topic = "cmnd/tasmota_07235490/DIMMER";
msg.payload = msg.payload.payload;
return msg;
Again the Dimmer topic

Your incoming payload = "60", but you send to the output msg.payload.payload, which does not exist,
So your "Debug" node shows "undefined".

Where, do you need help for?

I really do not understand what kind of help you expect to receive, because I do not see any relationship between tasmota, Dimmer, temperature and controlling a Shelly.

Explain please, what you want, what your input is and what you expect as output.

Regards

Re: How to put result of calculation into mqtt command

Posted: Monday 10 March 2025 20:22
by sailmich
@FireWizard,
sorry for confusing you!
I shouldn't put the code inside the post as I did.
[Again the Dimmer topic

Your incoming payload = "60", but you send to the output msg.payload.payload, which does not exist,
So your "Debug" node shows "undefined".

Where, do you need help for?
You gave me the right kick instead of msg.payload.payload I should use msg.payload. With that the flow is working as I want!
Thank you very much!
Spoiler: show

Code: Select all

[
    {
        "id": "cf3ed0c4e266eb23",
        "type": "change",
        "z": "27bd8ea24b66cf31",
        "name": "calculate consumption",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload*4-80",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1600,
        "y": 840,
        "wires": [
            [
                "864087b683e6aecc"
            ]
        ]
    },
    {
        "id": "b685926d288f295d",
        "type": "inject",
        "z": "27bd8ea24b66cf31",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "SZ_Vorlauf",
        "payload": "35",
        "payloadType": "num",
        "x": 1390,
        "y": 840,
        "wires": [
            [
                "cf3ed0c4e266eb23"
            ]
        ]
    },
    {
        "id": "1ba62f886d0ad6f5",
        "type": "function",
        "z": "27bd8ea24b66cf31",
        "name": "SZ_Variable%",
        "func": "msg.topic = \"cmnd/tasmota_07235490/DIMMER\";\nmsg.payload = msg.payload;\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1940,
        "y": 840,
        "wires": [
            [
                "957df9569f0dc0df"
            ]
        ]
    },
    {
        "id": "864087b683e6aecc",
        "type": "json",
        "z": "27bd8ea24b66cf31",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1790,
        "y": 840,
        "wires": [
            [
                "1ba62f886d0ad6f5"
            ]
        ]
    },
    {
        "id": "957df9569f0dc0df",
        "type": "mqtt out",
        "z": "27bd8ea24b66cf31",
        "name": "To WLAN Thermostat",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "f9f13036.e28b58",
        "x": 2180,
        "y": 840,
        "wires": []
    },
    {
        "id": "f9f13036.e28b58",
        "type": "mqtt-broker",
        "name": "Homeserver",
        "broker": "192.168.178.91",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

Re: How to put result of calculation into mqtt command

Posted: Tuesday 11 March 2025 0:06
by FireWizard
Hello @sailmich,
With that the flow is working as I want!
If you are happy, I have to be as well :D .

Regards