How to use a number value from JSON message published from an MQTT broker in a switch node

Hi all,

I am publishing a JSON data from an MQTT broker which I can turn into a JavaScript Object using the JSON node, then I can find the number value in the object array using the debug node and clicking on the "copy path" button.

I need to use that number value in a switch node which will evaluate if it's less than "50" and then publish another message to another MQTT topic. How do I go about that?

My flow is like this now;

[{"id":"97527151.57977","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c8c0178d.388638","type":"mqtt in","z":"97527151.57977","name":"","topic":"iotgateway","qos":"1","datatype":"auto","broker":"42bbbb0d.1b4ff4","nl":false,"rap":true,"rh":0,"x":220,"y":560,"wires":[["db222afc.fc86f8"]]},{"id":"bcdd863a.a8cb18","type":"mqtt out","z":"97527151.57977","name":"","topic":"iotgateway/write","qos":"1","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"42bbbb0d.1b4ff4","x":740,"y":320,"wires":[]},{"id":"b7ac1935.42ee18","type":"inject","z":"97527151.57977","name":"Start_Value=False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Message","payload":"[{\"id\":\"Visu+ UA  Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","payloadType":"str","x":430,"y":320,"wires":[["bcdd863a.a8cb18"]]},{"id":"f8e957eb.eede38","type":"debug","z":"97527151.57977","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":560,"wires":[]},{"id":"db222afc.fc86f8","type":"json","z":"97527151.57977","name":"","property":"payload.values[0].v","action":"obj","pretty":false,"x":390,"y":560,"wires":[["6089bf6c.2faf1"]]},{"id":"6089bf6c.2faf1","type":"switch","z":"97527151.57977","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":560,"wires":[["f8e957eb.eede38"]]},{"id":"42bbbb0d.1b4ff4","type":"mqtt-broker","name":"","broker":"tcp://localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Your flow is corrupt due to not pasting correctly between tripple backticks on there own line, e.g.
Also your inject example holds no Property with a value like "50", If it did that would be a string not a number, so doing comparisons, e.g. > on a string will not produce the result you are looking for.

Please fix your post and add a clear example of your in put.

Thanks for the heads up on backticks. So how do I convert it to a number? I'm not familiar with coding so this is a little confusing for me. All I want to do is compare two numbers. The first number comes from the broker in JSON format, and I turned it into an object which looks like this in the output:

28.05.2021 09:31:14node: f8e957eb.eede38iotgateway : msg.payload : Object


timestamp: 1622183473979

values: array[1]

0: object

id: "Visu+ UA Server.Visu+ SCADA.Tags.Temperature"

v: 64

q: true

t: 1622183472904

I need to use the "v: 64" value to compare if it's less or bigger than 50 and then send a message to the MQTT broker

Convert what? your inject does not contain any number or string representation of a value like 40 or 60

But if you have the path, enter that into the property input of the switch node and compare it as you want. If the value in the path is a string(this is unknown as you have not given a clear example) then use a change node or function node to convert.
change node use JSONata expression $number(path to value)
function node msg.payload.path = Number(msg.payload.path);

Also the MQTT node will output an object if you choose so in the settings, so no need to the json node.

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