# Struggling with JavaScript syntax

**URL:** https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073
**Category:** General
**Created:** [2 January 2023 17:26 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073 "2023-01-02T17:26:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nivek1612](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@Nivek1612](https://discourse.nodered.org/u/Nivek1612)
#### Post date: [2 January 2023 17:26 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/1 "2023-01-02T17:26:41Z")

</div>

Ok so I’m a newbie  
Struggling with syntax

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

```auto
"<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

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [2 January 2023 17:33 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/2 "2023-01-02T17:33:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Nivek1612](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@Nivek1612](https://discourse.nodered.org/u/Nivek1612)
#### Post date: [2 January 2023 17:37 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/3 "2023-01-02T17:37:34Z")

</div>

Thanks for the rapid reply

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 January 2023 17:44 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/4 "2023-01-02T17:44:08Z")

</div>

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.

2. There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

3. I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

### Author: ![Nivek1612](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@Nivek1612](https://discourse.nodered.org/u/Nivek1612)
#### Post date: [2 January 2023 18:38 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/5 "2023-01-02T18:38:07Z")

</div>

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.

---

<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: [3 March 2023 18:38 UTC](https://discourse.nodered.org/t/struggling-with-javascript-syntax/73073/6 "2023-03-03T18:38:57Z")

</div>

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