Mqtt in - I can't write a function

Good morning.
I have a switch.

You have recommended me to mqtt out that work.
I can't solve mqtt in.
How to solve the function?
How to write

if(msg.payload state_left == "ON")
{
}

He uses here json node component.

I think the word 'please' is missing here somewhere, perhaps a language gap.

The switch only provides a button press and you are able to receive the payload.
Now, what do want to do with it ?

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

When you understand how to do that then, instead of using Function node, look up the path for the state and use a Switch node, with that path, to test for ON.

Also, I recommend 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.

I want to do this.
I wrote that Zigbee2mqttt is bad
I am sorry. I can't speak English. Translator.

[{"id":"9578ebc7de6ea8bc","type":"group","z":"6ad77323c3e5fde5","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["3669d3ad26e1d2ef","70a3e28999f700eb","3a447b756fe39103","074fed3a5cd94703","3f99ebe486270192","349449d30c998319","a92611fd702edc85"],"x":134,"y":139,"w":752,"h":242},{"id":"3669d3ad26e1d2ef","type":"inject","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":180,"wires":[["70a3e28999f700eb"]]},{"id":"70a3e28999f700eb","type":"zigbee2mqtt-out","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"","server":"018707f7cacbabb0","friendly_name":"zasuvka (TS011F_plug_3)","device_id":"0x70b3d52b600f5054","command":"state","commandType":"z2m_cmd","payload":"toggle","payloadType":"z2m_payload","optionsValue":"","optionsType":"nothing","x":490,"y":180,"wires":[]},{"id":"3a447b756fe39103","type":"zigbee2mqtt-in","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"","server":"018707f7cacbabb0","friendly_name":"zasuvka (TS011F_plug_3)","device_id":"0x70b3d52b600f5054","state":"state","outputAtStartup":true,"filterChanges":false,"enableMultiple":false,"x":290,"y":280,"wires":[["3f99ebe486270192"]]},{"id":"074fed3a5cd94703","type":"debug","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":340,"wires":[]},{"id":"3f99ebe486270192","type":"function","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"function 3","func":"if(msg.payload =='ON')\n{\n  msg.background = \"red\";\n  msg.label = \"zapni\";\n  msg.payload = 'Rele je ON';\n}\nelse\n{\n  msg.background = \"green\";\n  msg.label = \"vypni\";\n  msg.payload = 'Rele je OFF';\n}\nreturn msg;\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":280,"wires":[["074fed3a5cd94703","a92611fd702edc85"]]},{"id":"349449d30c998319","type":"ui_button","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"","group":"d28fa5aec3e24953","order":0,"width":"4","height":"1","passthru":false,"label":"{{topic}}","tooltip":"","color":"","bgcolor":"{{background}}","className":"","icon":"{{myicon}}","payload":"open","payloadType":"str","topic":"control","topicType":"str","x":230,"y":220,"wires":[["70a3e28999f700eb"]]},{"id":"a92611fd702edc85","type":"change","z":"6ad77323c3e5fde5","g":"9578ebc7de6ea8bc","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"label","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":280,"wires":[["349449d30c998319"]]},{"id":"018707f7cacbabb0","type":"zigbee2mqtt-server","name":"raspi zigbee2mqtt","host":"192.168.1.11","mqtt_port":"1883","mqtt_username":"","mqtt_password":"","mqtt_qos":"0","tls":"","usetls":false,"base_topic":"zigbee2mqtt"},{"id":"d28fa5aec3e24953","type":"ui_group","name":"Default","tab":"b059073edf59e633","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"b059073edf59e633","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Should be

if(msg.payload.state_left == "ON")
{
}


1 Like

Which would often be better done in a Switch node, as I suggested earlier.

Hi.
In the Function node it's ok.
Switche not work.


Enable debug 14 ok
action: "1_single", action: "2_single"

debug 13,15 not write

[
    {
        "id": "1b2688f242462eed",
        "type": "group",
        "z": "1ace8f272b838e8e",
        "name": "DalkaÄŤ obyvak + pokoj",
        "style": {
            "label": true
        },
        "nodes": [
            "011962acb87762ad",
            "96e363fa5bf99242",
            "219fd7caaca58b43",
            "6afae3a4a745341e",
            "4a53804bea52318d"
        ],
        "x": 134,
        "y": 319,
        "w": 672,
        "h": 162
    },
    {
        "id": "011962acb87762ad",
        "type": "debug",
        "z": "1ace8f272b838e8e",
        "g": "1b2688f242462eed",
        "name": "debug 13",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 400,
        "wires": []
    },
    {
        "id": "96e363fa5bf99242",
        "type": "mqtt in",
        "z": "1ace8f272b838e8e",
        "g": "1b2688f242462eed",
        "name": "",
        "topic": "zigbee2mqtt/dalkac obyvak",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "c6a17104c99a5cc7",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 270,
        "y": 380,
        "wires": [
            [
                "219fd7caaca58b43",
                "6afae3a4a745341e"
            ]
        ]
    },
    {
        "id": "219fd7caaca58b43",
        "type": "switch",
        "z": "1ace8f272b838e8e",
        "g": "1b2688f242462eed",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "1_single",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "2_single",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 490,
        "y": 380,
        "wires": [
            [
                "4a53804bea52318d"
            ],
            [
                "011962acb87762ad"
            ]
        ]
    },
    {
        "id": "6afae3a4a745341e",
        "type": "debug",
        "z": "1ace8f272b838e8e",
        "g": "1b2688f242462eed",
        "name": "debug 14",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 440,
        "wires": []
    },
    {
        "id": "4a53804bea52318d",
        "type": "debug",
        "z": "1ace8f272b838e8e",
        "g": "1b2688f242462eed",
        "name": "debug 15",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 360,
        "wires": []
    },
    {
        "id": "c6a17104c99a5cc7",
        "type": "mqtt-broker",
        "name": "Raspberry MQTT",
        "broker": "192.168.1.5",
        "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",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

What am I doing wrong?
I put in.

``
code goes here

This works.
Switch Ctrl + C and Ctrl + V.

It seems you are now trying to do something different. Show us what debug 14 gives you and explain what you want the switch to do.

In the MQTT-In node you have a space in the topic-name.
Did you really mean to do that?

zigbee2mqtt/dalkac obyvak

dynamicdave - debug 17 works without switch.


debug 17 disable and debug 16,18 enable, not write

As I think I mentioned earlier

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Did you look at that? If you had you could have seen that you need to switch using the value of msg.payload.action. So in the switch you want

Also have you watched the getting started videos that I suggested?

It works.Thank you very much.