How to sort a subset of data within an XML structure

First, I'm new to Node-RED and trying to get into the right mindset. I am working with an older sensor device that I cannot modify. It sends Temp Readings in XML that look like this:

<?xml version="1.0"?>
<Message>
  <Timestamp>2019-11-21 21:40:23</Timestamp>
  <SensorReadings>
    <Reading>
      <ReadingTimestamp>2019-11-21 21:40:23</ReadingTimestamp>
      <Type>Temperature</Type>
      <Value>66.3</Value>
      <Units>F</Units>
    </Reading>
    <Reading>
      <ReadingTimestamp>2019-11-21 21:35:17</ReadingTimestamp>
      <Type>Temperature</Type>
      <Value>66.3</Value>
      <Units>F</Units>
    </Reading>
    <Reading>
      <ReadingTimestamp>2019-11-21 21:30:17</ReadingTimestamp>
      <Type>Temperature</Type>
      <Value>66.3</Value>
      <Units>F</Units>
    </Reading>
  </SensorReadings>
</Message>

I used the Parser XML Node to turn that into an Object. Then I want to sort the Readings. The sensor sends newest reading first. So as you walk through the array or readings you are walking back in time. I am going to save this to InfluxDB and it likes things in chronological order oldest to newest. So I thought the Sequence Sort node should fix my problem, but it doesn't seem to do anything.

I set sort to: msg.payload.Message.SensorReadings[0].Reading and key to: msg.payload.Message.SensorReadings.Reading.ReadingTimestamp

Since I am new I probably have something basic messed up, but after reading the doc a few times and trying search I can't see it. Sure I could just write a function node to do this for me, but I'm really trying to get into the idea of using Nodes and adopt the Flow programming style.

I'd appreciate any advice, help or, tips anyone can give me.

Thank You,
Kevin Runde

HI Kevin, I did a testing flow based on the your dataset.

First step is to generate the array of readings to be sorted. For this purpose I used a change node to assign msg.payload to msg.payload.Message.SensorReadings[0].Reading.

Next a split node will generate a separate message for each line of the array. This will give us a "true" sequence (one that contains msg.parts) to be sorted.

I used an auxiliary change node after the slip (but probably this can be simplified).

Finally the sort node configured as below:

a-01

Apparently it is working but I did not test extensively.

Testing flow:

[{"id":"ee02ea48.97c068","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"e7a660db.257af","type":"file in","z":"ee02ea48.97c068","name":"xml file","filename":"C:/Users/OCM/.node-red/static/nrfiles/forum-1.xml","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":360,"y":200,"wires":[["bf48ad6e.18fd7"]]},{"id":"bf48ad6e.18fd7","type":"xml","z":"ee02ea48.97c068","name":"","property":"payload","attr":"","chr":"","x":510,"y":200,"wires":[["9f94195f.b2a008","8f80f993.5c2978"]]},{"id":"6862fc61.afd814","type":"debug","z":"ee02ea48.97c068","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.ReadingTimestamp","targetType":"msg","x":1360,"y":200,"wires":[]},{"id":"6521fb64.b261c4","type":"inject","z":"ee02ea48.97c068","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":200,"wires":[["e7a660db.257af"]]},{"id":"e95c0dc8.54bcd","type":"sort","z":"ee02ea48.97c068","name":"","order":"ascending","as_num":true,"target":"","targetType":"seq","msgKey":"payload.value","msgKeyType":"jsonata","seqKey":"test","seqKeyType":"msg","x":1130,"y":200,"wires":[["6862fc61.afd814"]]},{"id":"d96116aa.5feae8","type":"split","z":"ee02ea48.97c068","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":830,"y":200,"wires":[["e92941f2.9228e","7e7aa8c0.a95558"]]},{"id":"9f94195f.b2a008","type":"change","z":"ee02ea48.97c068","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.Message.SensorReadings[0].Reading","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":200,"wires":[["d96116aa.5feae8","82652534.0f84d8"]]},{"id":"8f80f993.5c2978","type":"debug","z":"ee02ea48.97c068","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":120,"wires":[]},{"id":"e92941f2.9228e","type":"change","z":"ee02ea48.97c068","name":"","rules":[{"t":"set","p":"test","pt":"msg","to":"payload.ReadingTimestamp","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":200,"wires":[["e95c0dc8.54bcd"]]},{"id":"82652534.0f84d8","type":"debug","z":"ee02ea48.97c068","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":120,"wires":[]},{"id":"7e7aa8c0.a95558","type":"debug","z":"ee02ea48.97c068","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":120,"wires":[]}]

Hi Kevin, welcome to the forum.
It would be great if you could edit your thread title to something more descriptive (the pencil next to the title).
It would help other users in the future when searching the forum, who maybe have the same question.

Thanks

Hi Andrei,

Thank you for the quick reply. I had to change the Order to Descending and then everything worked like I wanted. Thank you for providing the Flow! I tried to providing one as an attachment, but it wouldn't let me as I was a new member. I should have done it as a code block like you did.

Thank you,
Kevin Runde

1 Like

Hi Paul,

Done. Since I am still new here if you have a better suggestion for a subject let me know and I'll update it.

Thank You,
Kevin Runde

1 Like