# Function node topic and payload fail to activate MQTT device

**URL:** <https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998>\
**Category:** General\
**Created:** [13 July 2020 12:30 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998 "2020-07-13T12:30:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [13 July 2020 12:30 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/1 "2020-07-13T12:30:34Z")

</div>

Hi, I am trying to activate a relay when temperature from OpenWeatherMap reaches a certain value.  
Using an inject node the relay is successfully activated but when when topic and payload are sent from functions it does not work.

 ![NodeRedPic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d625bbf041003545e453f115b379e8720535fc3.jpeg)

```auto
[{"id":"91f0be81.5623b","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a2bb2063.08297","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"e91fd94e.c36028","type":"aedes broker","z":"91f0be81.5623b","name":"","mqtt_port":1883,"mqtt_ws_port":"","cert":"","key":"","certname":"","keyname":"","dburl":"","usetls":false,"x":290,"y":60,"wires":[[]]},{"id":"6b9ea794.f10898","type":"debug","z":"91f0be81.5623b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":180,"wires":[]},{"id":"a28d3841.b18418","type":"mqtt in","z":"91f0be81.5623b","name":"","topic":"/Relay-001/status","qos":"2","datatype":"utf8","broker":"a2bb2063.08297","x":280,"y":200,"wires":[["6b9ea794.f10898"]]},{"id":"31e2f7ae.48c418","type":"mqtt out","z":"91f0be81.5623b","name":"","topic":"","qos":"","retain":"","broker":"a2bb2063.08297","x":730,"y":340,"wires":[]},{"id":"c08946df.27d148","type":"inject","z":"91f0be81.5623b","name":"Relay-001 CLOSE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/Relay-001/command","payload":"CLOSE","payloadType":"str","x":310,"y":280,"wires":[["31e2f7ae.48c418"]]},{"id":"8059b0f5.2a1ed","type":"inject","z":"91f0be81.5623b","name":"Relay-001 OPEN","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/Relay-001/command","payload":"OPEN","payloadType":"str","x":300,"y":340,"wires":[["31e2f7ae.48c418"]]},{"id":"7311fecb.3e02c","type":"inject","z":"91f0be81.5623b","name":"Relay-002 CLOSE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/Relay-002/command","payload":"CLOSE","payloadType":"str","x":310,"y":400,"wires":[["31e2f7ae.48c418"]]},{"id":"d9f0a21c.539ba","type":"inject","z":"91f0be81.5623b","name":"Relay-002 OPEN","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/Relay-002/command","payload":"OPEN","payloadType":"str","x":300,"y":460,"wires":[["31e2f7ae.48c418"]]},{"id":"c748b9e.7be8548","type":"mqtt in","z":"91f0be81.5623b","name":"","topic":"/Relay-002/status","qos":"2","datatype":"utf8","broker":"a2bb2063.08297","x":280,"y":140,"wires":[["6b9ea794.f10898"]]},{"id":"f72f398b.a52058","type":"openweathermap in","z":"91f0be81.5623b","name":"","wtype":"current","lon":"-42.821046","lat":"-22.894858","city":"","country":"","language":"en","x":300,"y":520,"wires":[["39a6954e.a4ec7a"]]},{"id":"39a6954e.a4ec7a","type":"function","z":"91f0be81.5623b","name":"","func":"\nvar msg1 = { payload:\"CLOSE\",topic:\"/Relay-001/command\"};\nvar msg2 = { payload:\"OPEN\", topic:\"/Relay-001/command\"};\n\nif(msg.payload.tempc>15){return msg1} else {return msg2}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":520,"wires":[["31e2f7ae.48c418","f7c82d07.8d1b7"]]},{"id":"f7c82d07.8d1b7","type":"debug","z":"91f0be81.5623b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":520,"wires":[]}]

```

When injected I have a status indicator that is activated by the remote relay that indicated relay ON or OFF. When injected the remote devices responds with ON or OFF but completely fail when it is the function who sends the command.

Assistance welcome.  
Thanks  
Paulo

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 July 2020 12:40 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/2 "2020-07-13T12:40:36Z")

</div>

Attach a debug after openweathermap node.

Verify the value is arriving in msg.payload.tempc is present and that is is a number as you expect.

Also, are you currently getting anything at all out of that bottom debug node? At a guess, you are always getting payload "OPEN" and never getting payload "CLOSE"?

Additional tip - dont start topics with a `/`

> According to the MQTT standard the slash is the delimiter of levels. By starting a topic with a slash you essentially tell the broker that the first level is named "null" (i.e. a non-existing value). This is not by any circumstances logical and should be avoided as well as double or triple slashes etc. should be avoided. So no, do not start with a slash.

---

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [13 July 2020 12:46 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/3 "2020-07-13T12:46:39Z")

</div>

Hi Steve, thanks for taking the time to help.  
Data is arriving from OpenWeather.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d617f85c43515a1643f4fcfc09daaff19cc36830.png)

As you can see the above is what is sent by the function and below is the status response when I inject the same command manually.

I will check removing the "/".

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 July 2020 12:54 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/4 "2020-07-13T12:54:47Z")

</div>

Strange - I dont see any issue.

Try sending a manual inject OPEN then wait for the map node to send an update to CLOSE the relay.

Also, add another SUBSCRIBE (to the command)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc467cf15754cee35d67ba335402e0921c5e6c93.png)  
and compare the values generated by the map+function node with value generated by the manual inject node

---

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [13 July 2020 13:06 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/5 "2020-07-13T13:06:26Z")

</div>

OK, see the modification below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7aea55d897da69be6a75a3819292d777c0ccbaea.png)

Just to see how the command looks when originating from manual inject and from the function:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/8132d95810d2272cf6c246b88617de7587f59b22.png)

Above from the function, no status response.  
Below from manual injection and status response.

Can you see anything that could cause this behavior?

Status response is sent even if the command is repeated.

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 July 2020 13:13 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/6 "2020-07-13T13:13:52Z")

</div>

Thats not what I said to do now is it 😆

you need to also subscribe to the command topic to ensure the message from the map+function is actually making its way to the broker (and for comparison)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc467cf15754cee35d67ba335402e0921c5e6c93.png)

---

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [13 July 2020 13:23 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/7 "2020-07-13T13:23:32Z")

</div>

Ok, missed that one.  
Here goes, no manual injection:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e21031ad8a7a11b0d4dbff06926b6cfbb8bc5e0.png)

---

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [14 July 2020 00:43 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/8 "2020-07-14T00:43:42Z")

</div>

Adding a 8s delay works.  
Smaller delays work sometimes.  
Why?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8ed545a49892d86450c455590353df7f0e86dd8d.png)

---

<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:** [14 July 2020 06:02 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/9 "2020-07-14T06:02:58Z")

</div>

Is that a straight delay or a rate limit?  
Are you getting multiple messages close together from the map which might confuse the device?

---

<div class="post-metadata">

**Author:** ![pcborges](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@pcborges](https://discourse.nodered.org/u/pcborges)\
**Post date:** [14 July 2020 10:14 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/10 "2020-07-14T10:14:59Z")

</div>

Hi.  
Straight.  
No, as I am monitoring temperature I see I get one at a time with large intervals in between.  
And I noticed that even with 8s, sometimes, it fails.  
Thanks

---

<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:** [12 September 2020 10:15 UTC](https://discourse.nodered.org/t/function-node-topic-and-payload-fail-to-activate-mqtt-device/29998/11 "2020-09-12T10:15:01Z")

</div>

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