# Reading a Node's message payload when the state changes and putting that into another Node

**URL:** <https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805>\
**Category:** General\
**Created:** [7 December 2021 03:34 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805 "2021-12-07T03:34:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 03:34 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/1 "2021-12-07T03:34:14Z")

</div>

Hi All,

I have a reed switch on a gate, when the switch triggers it updates on my device from false, to true.

Is there a way that when this occurs, that it populates a node? Intention is to get this information into OpenHAB as a Contact Item (so, OPEN/CLOSED)

Here is the debug from the door

```auto
07/12/2021, 8:45:22 amnode: get-status
get-status : msg.payload : Object
object
deviceId: 225024747
state: object
serialNumber: 225024747
event: object
doors: object
1: false
2: true
3: false
4: false

```

Under Doors: object, you can see TRUE. This is the data I want to populate in to OpenHAB

Any points would be fantastic, thank you!

Regards

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 December 2021 10:44 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/2 "2021-12-07T10:44:24Z")

</div>

> [@dastrix](#):
>
> Is there a way that when this occurs, that it populates a node?

What do you mean by 'populates a node'. The normal way to pass information to a node is to send it a message.

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 19:48 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/3 "2021-12-07T19:48:35Z")

</div>

What im confused about is what I need to use when the gate opens/triggers and the reed switch changes from closed to open.

How do I get that message and put it into a node?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 December 2021 19:58 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/4 "2021-12-07T19:58:24Z")

</div>

How did you get the message that you showed us above - did it get sent automatically when the switch was activated, or did you somehow request the current status?

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 19:59 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/5 "2021-12-07T19:59:15Z")

</div>

I used this template

> <https://github.com/uhppoted/node-red-contrib-uhppoted/blob/master/examples/basic/7-doors.json>

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 December 2021 20:21 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/6 "2021-12-07T20:21:40Z")

</div>

That flow has three Inject nodes, each labelled "test".  
The there are the three nodes "get door control", "set door control" and "open door". I don't know which of these gives you the data you posted above since I do not have a UHPPOTE TCP/IP Wiegand access controller board.

However, you can set the inject node to repeat. It could be once a minute or 10 times a second, whatever meets your requirement.  
Then the bit of data that you want seems to be in msg.payload.doors.2. You can use a change node to extract it and a Filter node to only pass the message on when it's value changes.

Maybe something like this:

 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09c13e770bc49645737b298f008f5caadee860ec.jpeg)

```auto
[{"id":"c021986865969964","type":"inject","z":"3140aa6bccad4d0d","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":110,"y":140,"wires":[["c8a9529354c8383b"]]},{"id":"656d89ab5b23e7ed","type":"rbe","z":"3140aa6bccad4d0d","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":710,"y":80,"wires":[["e9eb86e2885f32ec"]]},{"id":"ba760dea443b6668","type":"change","z":"3140aa6bccad4d0d","name":"Door 1","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.doors.1","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"door1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":80,"wires":[["656d89ab5b23e7ed"]]},{"id":"a93ae9247c9db0cc","type":"rbe","z":"3140aa6bccad4d0d","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":710,"y":120,"wires":[["9dfbe9fb125a2bcf"]]},{"id":"8f06051413e1ad57","type":"change","z":"3140aa6bccad4d0d","name":"Door 2","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.doors.2","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"door2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":120,"wires":[["a93ae9247c9db0cc"]]},{"id":"81792cdce7a35da7","type":"rbe","z":"3140aa6bccad4d0d","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":710,"y":160,"wires":[["8f92b3f367e2801d"]]},{"id":"cff78b9aca10ce0a","type":"change","z":"3140aa6bccad4d0d","name":"Door 3","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.doors.3","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"door3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":160,"wires":[["81792cdce7a35da7"]]},{"id":"2ad8e28ac9b4d7c6","type":"rbe","z":"3140aa6bccad4d0d","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":710,"y":200,"wires":[["be0f9557384ce526"]]},{"id":"b7c9e1c6563420c2","type":"change","z":"3140aa6bccad4d0d","name":"Door 4","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.doors.4","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"door4","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":200,"wires":[["2ad8e28ac9b4d7c6"]]},{"id":"e9eb86e2885f32ec","type":"debug","z":"3140aa6bccad4d0d","name":"Door 1 status changed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":80,"wires":[]},{"id":"9dfbe9fb125a2bcf","type":"debug","z":"3140aa6bccad4d0d","name":"Door 2 status changed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":120,"wires":[]},{"id":"8f92b3f367e2801d","type":"debug","z":"3140aa6bccad4d0d","name":"Door 3 status changed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":160,"wires":[]},{"id":"be0f9557384ce526","type":"debug","z":"3140aa6bccad4d0d","name":"Door 4 status changed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":200,"wires":[]},{"id":"c8a9529354c8383b","type":"get-door-control","z":"3140aa6bccad4d0d","name":"One of the UHPPOTE nodes","topic":"","config":"cbd92359.dbf19","x":360,"y":140,"wires":[["ba760dea443b6668","8f06051413e1ad57","cff78b9aca10ce0a","b7c9e1c6563420c2"]]},{"id":"378b04a7268fe0fd","type":"comment","z":"3140aa6bccad4d0d","name":"A possible way to get status changes of 4 doors","info":"","x":200,"y":100,"wires":[]},{"id":"cbd92359.dbf19","type":"config","name":"examples","timeout":"5000","bind":"192.168.1.100","broadcast":"192.168.1.255:60000","listen":"192.168.1.100:60001","controllers":"{\"303986753\":{\"address\":\"192.168.1.100:60000\",\"broadcast\":false},\"405419896\":{\"address\":\"192.168.1.100:60000\",\"broadcast\":true}}","debug":true}]

```

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 20:27 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/7 "2021-12-07T20:27:47Z")

</div>

Thanks, but can't NR react to the code changing (Closed to Open) or must it use some kind of timer like you said ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 December 2021 20:33 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/8 "2021-12-07T20:33:07Z")

</div>

All I have to go on is what you've told us.  
I don't have the slightest idea what you mean by "the code changing".

There are nodes which watch for an event and send a message when a change occurs. An example is the Raspberry PI GPIO In node. Perhaps one of the UHPPOTE nodes does that? I don't know.

The alternative is to poll the device periodically, as I suggested above.

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 20:36 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/9 "2021-12-07T20:36:02Z")

</div>

Changing, by this i mean the door ie from Closed to Open. But I guess this is all NR polling the device, rather than the device sending a message to NR upon it changing (which it wouldnt do)

I dont think NR is the right way to do it.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [7 December 2021 20:52 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/10 "2021-12-07T20:52:45Z")

</div>

From what I can tell, you may have better luck taking a look at [this example](https://github.com/uhppoted/node-red-contrib-uhppoted/blob/master/examples/basic/4-listen.json) for a closer to real-time data feed.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 December 2021 21:53 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/11 "2021-12-07T21:53:06Z")

</div>

> [@dastrix](#):
>
> rather than the device sending a message to NR upon it changing (which it wouldnt do)

Most devices do send data when they change state. What device are you using and how is it connected to the system?

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [7 December 2021 21:59 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/12 "2021-12-07T21:59:11Z")

</div>

Seems listen is what I need.

Need to extract that open/closed data then to send to openHab

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [8 December 2021 01:05 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/13 "2021-12-08T01:05:55Z")

</div>

Should be pretty easy past that point. Hit us up if you run into trouble.

With lots of other APIs, the best you get is the equivalent of check status (philips hue, this shade is cast at you.) In that situation, the typical approach is to just check the status every X amount of time (referred to as "polling.")

---

<div class="post-metadata">

**Author:** ![dastrix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dastrix/32/51740_2.png) [@dastrix](https://discourse.nodered.org/u/dastrix)\
**Post date:** [8 December 2021 03:10 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/14 "2021-12-08T03:10:17Z")

</div>

Thanks Jay I will if thats OK by you.

I need to go and open/close the gate a few times to see if the debug on the code I imported registers correctly. Then I'm not sure where to start but basically i want to extract that gate 'status' so to speak and send that to an OpenHAB Contact Item. The link between NR and OH is working , at least in the sense that if I send a ON command to the Electric Strike switch, it opens the gate via NR, but sending the gate status/state, in the other direction is new..

cheers!

---

<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:** [6 February 2022 03:11 UTC](https://discourse.nodered.org/t/reading-a-nodes-message-payload-when-the-state-changes-and-putting-that-into-another-node/54805/15 "2022-02-06T03:11:05Z")

</div>

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