Convert script python to .json file

It's this line you have to modify
client.publish("distance/result", str(Distance*0.0254))

What is the name of the variable that holds the object name?

i've added object_detected = classNames [classIds[i]] in the 83th line

So you have to return it also like

return object_width, bbox, object_center_x, object_center_y, object_detected

And in your call to function findObjects

object_width_in_frame, bbox, OB_X ,OB_Y, OB_detected = findObjects(outputs,img)

then change this line to also publish info about found object

client.publish("distance/result", str(Distance*0.0254) + ' Object detected: ' + OB_detected)

You need also to change this line (for the ref image)

ref_image_object_width, _, _, _, _ = findObjects(outputs,ref_image)

1 Like

Thank u so much , It works ! :grinning_face_with_smiling_eyes:

1 Like

Last question please , how can i set an alarm in node red (for exemple when the distance is <0.5 m ==> launches alarm )

When you say alarm in Node-RED there are so many possibilities - from simple to very advanced

A simple starter is in the flow below. I have extended the flow we have already with some nodes. It shows a notification in the Node-RED dashboard when the distance is below 0.5

It works like this:

  • the incoming message from the script is broken down in the change node using a JSONata expression, to get the actual distance as a number
  • we add a topic to the message
  • we set the duration of the notification
    If the distance is below 0.5 the message will be passed on to the notification node and it will be shown on the dashboard

To make more advanced alarms you can think of introducing some audio notificaation, send the alarm to your phone using Telegram etc etc. Many possibilities.

[{"id":"f097b105.f85e5","type":"mqtt in","z":"fcecd26b.a4283","name":"","topic":"distance/result","qos":"2","datatype":"auto","broker":"56c5f0f2.9f483","nl":false,"rap":true,"rh":0,"x":220,"y":740,"wires":[["3767954f.23f1ba","5f43e95e.1947f8"]]},{"id":"c76a1e70.41db4","type":"debug","z":"fcecd26b.a4283","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":680,"wires":[]},{"id":"340384a4.30f80c","type":"ui_toast","z":"fcecd26b.a4283","position":"top right","displayTime":"","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"","raw":false,"topic":"","name":"","x":830,"y":740,"wires":[]},{"id":"3767954f.23f1ba","type":"change","z":"fcecd26b.a4283","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($split(payload, ' ')[0])\t","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"Distance is to short","tot":"str"},{"t":"set","p":"timeout","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":740,"wires":[["be555a39.858278"]]},{"id":"5e391542.72ed5c","type":"inject","z":"fcecd26b.a4283","name":"> 0.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.6 Object detected: person","payloadType":"str","x":190,"y":800,"wires":[["3767954f.23f1ba"]]},{"id":"4330c723.0f5798","type":"inject","z":"fcecd26b.a4283","name":"< 0.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.3 Object detected: person","payloadType":"str","x":190,"y":840,"wires":[["3767954f.23f1ba"]]},{"id":"be555a39.858278","type":"switch","z":"fcecd26b.a4283","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"0.5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":740,"wires":[["340384a4.30f80c","c76a1e70.41db4"]]},{"id":"5f43e95e.1947f8","type":"debug","z":"fcecd26b.a4283","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":680,"wires":[]},{"id":"56c5f0f2.9f483","type":"mqtt-broker","name":"","broker":"127.0.0.1","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":""}]

Want to use " node-red-contrib-ui-led " ( a simple led status indicator on dashboard)
if the distance is > 0.5 m : the color of led will be green
then if distance < 0.5m gives a red color

Well, that little thing you should be able to figure out. What have you tried so far?

i've tried to modify the flow that you sent me and convert the number received (distance value) from the node switch to a boolean that i can connect to led


when it reaches a value < 0.5m the led truns ON and when the value changes >0.5m it stills ON

It looks like you have only one output in your switch node. What is it doing?

You are on the right track, very good!! I thought the same idea would work and I needed to use some more nodes. I added a new path from the original 3 rules switch (just to keep the notification we already had). In there, the switch checks if the distance is < 0.5 or higher. Depending on, it sends the message to one of the change nodes where the payload is set to boolean either true or false. The message is then carried over to the ui_led node that shows green or red

[{"id":"f097b105.f85e5","type":"mqtt in","z":"fcecd26b.a4283","name":"","topic":"distance/result","qos":"2","datatype":"auto","broker":"56c5f0f2.9f483","nl":false,"rap":true,"rh":0,"x":220,"y":740,"wires":[["3767954f.23f1ba","5f43e95e.1947f8"]]},{"id":"c76a1e70.41db4","type":"debug","z":"fcecd26b.a4283","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":680,"wires":[]},{"id":"340384a4.30f80c","type":"ui_toast","z":"fcecd26b.a4283","position":"top right","displayTime":"","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"","raw":false,"topic":"","name":"","x":910,"y":740,"wires":[]},{"id":"3767954f.23f1ba","type":"change","z":"fcecd26b.a4283","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($split(payload, ' ')[0])\t","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"Distance is to short","tot":"str"},{"t":"set","p":"timeout","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":740,"wires":[["be555a39.858278","f7aeecd2.c019a"]]},{"id":"5e391542.72ed5c","type":"inject","z":"fcecd26b.a4283","name":"> 0.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.6 Object detected: person","payloadType":"str","x":190,"y":800,"wires":[["3767954f.23f1ba"]]},{"id":"4330c723.0f5798","type":"inject","z":"fcecd26b.a4283","name":"< 0.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.3 Object detected: person","payloadType":"str","x":190,"y":840,"wires":[["3767954f.23f1ba"]]},{"id":"be555a39.858278","type":"switch","z":"fcecd26b.a4283","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"0.5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":740,"wires":[["340384a4.30f80c","c76a1e70.41db4"]]},{"id":"5f43e95e.1947f8","type":"debug","z":"fcecd26b.a4283","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":680,"wires":[]},{"id":"78b8c5b1.275bdc","type":"ui_led","z":"fcecd26b.a4283","order":1,"group":"16030ced.45add3","width":0,"height":0,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"false","valueType":"bool"},{"color":"#008000","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":950,"y":840,"wires":[]},{"id":"f7aeecd2.c019a","type":"switch","z":"fcecd26b.a4283","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"0.5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":840,"wires":[["254bf5f4.a127fa"],["649219f4.80a1c8"]]},{"id":"254bf5f4.a127fa","type":"change","z":"fcecd26b.a4283","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":820,"wires":[["78b8c5b1.275bdc"]]},{"id":"649219f4.80a1c8","type":"change","z":"fcecd26b.a4283","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":860,"wires":[["78b8c5b1.275bdc"]]},{"id":"56c5f0f2.9f483","type":"mqtt-broker","name":"","broker":"127.0.0.1","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":""},{"id":"16030ced.45add3","type":"ui_group","name":"Test","tab":"aed64a14.9adfe8","order":1,"disp":true,"width":"14","collapse":false},{"id":"aed64a14.9adfe8","type":"ui_tab","name":"Test","icon":"dashboard","disabled":false,"hidden":false}]

this is perfect , thank u so much

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