Function node topic and payload fail to activate MQTT device

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.

[{"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

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.

Hi Steve, thanks for taking the time to help.
Data is arriving from OpenWeather.
image

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

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
and compare the values generated by the map+function node with value generated by the manual inject node

OK, see the modification below:

Just to see how the command looks when originating from manual inject and from the function:
image

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

Thats not what I said to do now is it :laughing:

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

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

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

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

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

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