# Convert script python to .json file

**URL:** <https://discourse.nodered.org/t/convert-script-python-to-json-file/49443>\
**Category:** General\
**Created:** [7 August 2021 01:03 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443 "2021-08-07T01:03:07Z")\
**Posts on this page:** 13\
**Page:** 3

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [9 August 2021 14:58 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/42 "2021-08-09T14:58:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [9 August 2021 15:45 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/43 "2021-08-09T15:45:50Z")

</div>

i've added object\_detected = classNames [classIds[i]] in the 83th line

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [9 August 2021 16:11 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/44 "2021-08-09T16:11:28Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [9 August 2021 18:58 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/45 "2021-08-09T18:58:32Z")

</div>

Thank u so much , It works ! 😄

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [10 August 2021 00:29 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/46 "2021-08-10T00:29:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 August 2021 05:00 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/47 "2021-08-10T05:00:59Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/113d204e37c0b1a0580917d704322c4224f481e2.png)

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

```

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [10 August 2021 18:36 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/48 "2021-08-10T18:36:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 August 2021 18:58 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/49 "2021-08-10T18:58:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [10 August 2021 22:24 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/50 "2021-08-10T22:24:07Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8ed2a62b1aa1a74d096b0ca78670046ca044efa8.png)  
when it reaches a value \< 0.5m the led truns ON and when the value changes \>0.5m it stills ON

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 August 2021 23:43 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/51 "2021-08-10T23:43:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [11 August 2021 03:50 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/52 "2021-08-11T03:50:10Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![wejdene.guedri](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wejdene.guedri](https://discourse.nodered.org/u/wejdene.guedri)\
**Post date:** [11 August 2021 14:17 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/53 "2021-08-11T14:17:43Z")

</div>

this is perfect , thank u so 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:** [25 August 2021 14:17 UTC](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443/54 "2021-08-25T14:17:49Z")

</div>

Closed on 25 August 2021 14:17 UTC

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

[Previous page](https://discourse.nodered.org/t/convert-script-python-to-json-file/49443.md?page=2)
