# Falling edge detection with JSON payload

**URL:** <https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920>\
**Category:** General\
**Created:** [10 July 2022 22:46 UTC](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920 "2022-07-10T22:46:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [10 July 2022 22:46 UTC](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920/1 "2022-07-10T22:46:25Z")

</div>

I have some sensors that are set up to send their data via MQTT to NR in JSON format. When they detect, they appear similar to:

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

I want to send an email notification when the battery drops below the warn threshold. Basically, when a battery is new, it reports as battery\_ok=1. When it gets below whatever Acurite determines warning level, it switches to battery\_ok=0. I can detect this with a falling edge node easily by using a change node to break the battery\_ok value out, but then I don't have the rest of the message intact. The other nodes in the triggers (e.g., deadband) seem to allow selection of the property desired (e.g., msg.payload.battery\_ok).

The reason I would like to keep it, is so I could do smarter things with the notification. For the weather station, I only have 1. However, I have an array of Acurite tower sensors and it would be much cleaner and more maintainable to be notified for each sensor with a single rather than 1 flow hard coded for each sensor.

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

```auto
[{"id":"a2a32d63.97ea78","type":"mqtt in","z":"6f1f8b48.e80634","name":"Acurite-5n1","topic":"rtl_433/Acurite-5n1/2825/#","qos":"2","datatype":"json","broker":"b2400571.717f88","x":110,"y":60,"wires":[["504cb7c9.699908"]]},{"id":"504cb7c9.699908","type":"switch","z":"6f1f8b48.e80634","name":"filter by seq","property":"payload.sequence_num","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"gte","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":60,"wires":[["5d50bf19.7e3568"],[]]},{"id":"5d50bf19.7e3568","type":"switch","z":"6f1f8b48.e80634","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"payload.battery_ok","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":450,"y":60,"wires":[["f0953c6c.be9a4"]]},{"id":"6a3e9be9.3568dc","type":"falling-edge","z":"6f1f8b48.e80634","name":"<0.5","threshold":"0.5","x":610,"y":60,"wires":[["87989dc2.6dc8e8"]]},{"id":"87989dc2.6dc8e8","type":"deduplicate","z":"6f1f8b48.e80634","name":"","keyproperty":"","registryclass":"","expiry":"86400","x":790,"y":60,"wires":[["8099434.fedd5c"],[]]},{"id":"8099434.fedd5c","type":"change","z":"6f1f8b48.e80634","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Weather station battery low","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Acurite weather station battery has dropped below 100%. Replace at soonest convenience.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":60,"wires":[[]]},{"id":"f0953c6c.be9a4","type":"change","z":"6f1f8b48.e80634","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"msg.payload.battery_ok","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":120,"wires":[["6a3e9be9.3568dc"]]},{"id":"b2400571.717f88","type":"mqtt-broker","name":"RPi3","broker":"192.168.0.200","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

Perhaps I can set msg.payload to the battery\_ok value and retain the remainder of the JSON packet? I don't know how to do that and my quick attempt did not succeed. It replaced the entire msg.payload with the single value of battery\_ok.

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

```auto
[{"id":"204b24ae.b9f4d4","type":"inject","z":"6f1f8b48.e80634","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test","payload":"{\"time\":\"2022-07-10 22:30:51\",\"model\":\"Acurite-5n1\",\"message_type\":49,\"id\":2825,\"channel\":\"A\",\"sequence_num\":0,\"battery_ok\":0,\"wind_avg_km_h\":0,\"wind_dir_deg\":157.5,\"rain_in\":110.47,\"mic\":\"CHECKSUM\"}","payloadType":"json","x":110,"y":180,"wires":[["c4cb371b.bea128","bc595a98.21cd28"]]},{"id":"c4cb371b.bea128","type":"debug","z":"6f1f8b48.e80634","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":300,"y":160,"wires":[]},{"id":"bc595a98.21cd28","type":"switch","z":"6f1f8b48.e80634","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"battery_ok","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":200,"wires":[["6ab9ea8e.4ef3fc","ca7c3e7b.5b7098"]]},{"id":"6ab9ea8e.4ef3fc","type":"change","z":"6f1f8b48.e80634","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.battery_ok","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["726e8d54.ddcf4c","8cfb1949.5cf4e8"]]},{"id":"726e8d54.ddcf4c","type":"falling-edge","z":"6f1f8b48.e80634","name":"<0.5","threshold":"0.5","x":530,"y":280,"wires":[[]]},{"id":"8cfb1949.5cf4e8","type":"debug","z":"6f1f8b48.e80634","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":240,"wires":[]},{"id":"ca7c3e7b.5b7098","type":"debug","z":"6f1f8b48.e80634","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [11 July 2022 03:01 UTC](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920/2 "2022-07-11T03:01:42Z")

</div>

Just use a switch node and switch on the property as it is a JS Object you can reference this directly through the actual incoming message

It will be something along the lines of msg.payload.battery\_ok - switch one way if it s 0 or the other way (into your normal flow stream if it is 1)

Have all the sensors report in on a wildcard MQTT topic and then the first step is to switch them based on battery state - if battery is low then they go off to the warning stream which can break out the identifiers for you

Craig

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [12 July 2022 20:37 UTC](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920/3 "2022-07-12T20:37:13Z")

</div>

My RPi that feeds the sensor data crashed and my backup was faulty. I'm going to have to rebuild that before I can fix this issue. When that's resolved, I'll test your recommendation.

---

<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:** [10 September 2022 20:38 UTC](https://discourse.nodered.org/t/falling-edge-detection-with-json-payload/64920/4 "2022-09-10T20:38:09Z")

</div>

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