# Mqtt in - I can't write a function

**URL:** <https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667>\
**Category:** General\
**Created:** [3 December 2024 16:20 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667 "2024-12-03T16:20:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [3 December 2024 16:20 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/1 "2024-12-03T16:20:37Z")

</div>

Good morning.  
I have a switch.

> **[Moes ZS-EUB\_2gang control via MQTT | Zigbee2MQTT](https://www.zigbee2mqtt.io/devices/ZS-EUB_2gang.html)**
>
> Integrate your Moes ZS-EUB\_2gang via Zigbee2MQTT with whatever smart home infrastructure you are using without the vendor's bridge or gateway.

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.

 ![funkce](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d1baae49ac3337f5de364d5c4e75b79236c8ce35.png)

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcfd630bf21587c8dd20986f14bb7bcb3ea10e7b.jpeg "Learn MQTT in Node-RED | Node Red Tutorial Part 4") ](https://www.youtube.com/watch?v=161AKGM4xjk)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [3 December 2024 16:32 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/2 "2024-12-03T16:32:55Z")

</div>

> [@Le-Ze](#):
>
> You have recommended me to mqtt out that work.

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 ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 16:35 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/3 "2024-12-03T16:35:17Z")

</div>

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 16:39 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/4 "2024-12-03T16:39:09Z")

</div>

Also, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [3 December 2024 17:04 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/5 "2024-12-03T17:04:08Z")

</div>

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

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [3 December 2024 21:45 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/6 "2024-12-03T21:45:49Z")

</div>

> [@Le-Ze](#):
>
> ```auto
> if(msg.payload state_left == "ON")
> {
> }
> 
> ```

Should be

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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 22:03 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/7 "2024-12-03T22:03:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [4 December 2024 15:02 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/8 "2024-12-04T15:02:06Z")

</div>

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

 ![switche](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e9cfa676ec68a01fd61fbad029c80bc82db105a.png)  
Enable debug 14 ok  
action: "1\_single", action: "2\_single"

debug 13,15 not write

```auto
[
    {
        "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": ""
    }
]

```

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [4 December 2024 15:08 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/9 "2024-12-04T15:08:42Z")

</div>

What am I doing wrong?  
I put in.

``  
code goes here

```auto

```

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [4 December 2024 15:19 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/10 "2024-12-04T15:19:02Z")

</div>

![switche1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57550b015a63cb2074912032a2c4a2b0c5f3a866.png)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2024 16:04 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/11 "2024-12-04T16:04:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 December 2024 16:33 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/12 "2024-12-04T16:33:13Z")

</div>

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

zigbee2mqtt/dalkac obyvak

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [4 December 2024 16:43 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/13 "2024-12-04T16:43:42Z")

</div>

dynamicdave - debug 17 works without switch.

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [4 December 2024 16:44 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/14 "2024-12-04T16:44:53Z")

</div>

> **[Tuya TS0044\_1 control via MQTT | Zigbee2MQTT](https://www.zigbee2mqtt.io/devices/TS0044_1.html#tuya-ts0044_1)**
>
> Integrate your Tuya TS0044\_1 via Zigbee2MQTT with whatever smart home infrastructure you are using without the vendor's bridge or gateway.

 ![switche3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7ae6b7aa499b0e2426d23c9e6e708cb76f76bec.png)  
debug 17 disable and debug 16,18 enable, not write

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2024 16:59 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/15 "2024-12-04T16:59:45Z")

</div>

As I think I mentioned earlier

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6fe0626c6680c3ef4ee508aed917478f9882a8f1.png)

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

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [5 December 2024 15:14 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/16 "2024-12-05T15:14:07Z")

</div>

It works.Thank you very much.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 December 2024 15:14 UTC](https://discourse.nodered.org/t/mqtt-in-i-cant-write-a-function/93667/17 "2024-12-19T15:14:56Z")

</div>

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