Struggling with JavaScript syntax

Ok so I’m a newbie
Struggling with syntax

I have the following multicast being passed to a function in node-red

"<electricity id='xxxxxxxx' ver='2.0'><timestamp>1672667665</timestamp><signal rssi='-56' lqi='1'/><battery level='100%'/>

<channels><chan id='0'><curr units='w'>3928.00</curr><day units='wh'>21726.03</day></chan>

<chan id='1'><curr units='w'>0.00</curr><day units='wh'>0.00</day></chan>

<chan id='2'><curr units='w'>0.00</curr><day units='wh'>0.00</day></chan><chan id='3'><curr units='w'>0.00</curr><day units='wh'>0.00</day></chan><chan id='4'><curr units='w'>0.00</curr><day units='wh'>0.00</day></chan><chan id='5'><curr units='w'>0.00</curr><day units='wh'>0.00</day></chan></channels><property><current><watts>3928.00</watts><cost>139.44</cost></current><day><wh>21726.03</wh><cost>733.83</cost></day><tariff time='1672667665'><start>1672642800</start><curr_price>0.35</curr_price><block_limit>4294967295</block_limit><block_usage>12409</block_usage></tariff></property></electricity>"

I can add the RSSI to the MQTT payload ok using

msgmqtt.payload = "{\"Electricity\":{\"RSSI\":"
msgmqtt.payload += msg.payload.electricity.signal[0].$.rssi

But I can't work out the syntax for adding the <property><day><wh> which is 21726.03.

Be kind people the last code I wrote was in Assembler in the 80's

Hi and welcome to the forum. I've changed the title since Node-RED does not use Java but rather JavaScript which is, of course, very different. I've also changed the category to general since you aren't trying to create a custom node.

So the source data looks like XML? If so, try passing your msg through the xml node before your function node and you should be able to access it a lot easier.

Also, you don't need to create a text string in order to pass JSON to MQTT, just create a JavaScript object as your msg.payload and the MQTT node will take care of it for you.

Thanks for the rapid reply

Now I have to work out what that means
As I said newbee :slight_smile: but I’ll get there

As a self professed node-red newbie please accept the below advice from someone who can see the pitfalls you are making...

  1. XML (and JSON and CSV) are all strings. Splitting & joining strings is messy and problematic & just not fun at all. Where ever possible, convert your data strings to Objects (this will become more obvious as you get used to Node-RED and JavaScript.

    1. That is why Julian suggested feeding the XML (string) through an XML node since it will convert the messy string into a simple Object.
  2. There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item IN A MSG OBJECT << there is that object thing again. Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.
    BX00Cy7yHi

  3. I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

2 Likes

Thank all. I already had the XML node, just didn't realize the full capability. But the debug node helped me find the exact path to the data so my syntax needed was

msgmqtt.payload += msg.payload.electricity.property[0].day[0].wh[0]

Now have a perfect MQTT string to publish to home-assistant.