# In 1 node, read several information and make 1 message

**URL:** <https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924>\
**Category:** General\
**Created:** [23 December 2020 13:15 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924 "2020-12-23T13:15:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 13:15 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/1 "2020-12-23T13:15:06Z")

</div>

Hello, I turn to you again, because I have a difficulty:  
on the previous node I read the info from openweather, but I would like to summarize the info on a much shorter post. (and translate it into French that I am)  
I have syntax errors? or declaration of variables? (in fact I don't know node red)  
can you give me the right wording (i think you will understand what i want to do by reading my program:

```auto
let alerte0 = string;
let alerte1 = string;
let alerte2 = string;
let alerte3 = string;
let message1 = string;
let message2 = string;
let message3 = string;
let message4 = string;

 alerte0 = msg.payload.alerts[0];//.event||0;
 alerte1 = msg.payload.alerts[1];//.event||0;
 alerte2 = msg.payload.alerts[2];//.event||0;
 alerte3 = msg.payload.alerts[3];//.event||0;
 message1 = "";
 message2 = "";
 message3 = ""; 
 message4 = "";

if (alerte0=="Moderate rain-flood warning" || alerte1 =="Moderate rain-flood warning" || alerte2 =="Moderate rain-flood warning" || alerte3 =="Moderate rain-flood warning"){
    message1="Pluie"
}
else{ message1="";}
if (alerte0=="Moderate wind warning" || alerte1 =="Moderate wind warning" || alerte2 =="Moderate wind warning" || alerte3 =="Moderate wind warning"){
    message2="Vent"
}
else{ message2="";}
if (alerte0=="Moderate flooding warning" || alerte1 =="Moderate flooding warning" || alerte2 =="Moderate flooding warning" || alerte3 =="Moderate flooding warning"){
    message3="Inondation"
}
else{ message3="";}

if (message1==="" && message2!==""){
    message1=message2;
    message2={};
}
if (message2==="" && message3!==""){
    message2=message3;
    message3={};
}
if (message3==="" && message4!==""){
    message3=message4;
    message4={};
}
    
msg.payload="ALERTE : " + message1 + message2 + message3 + message4 ;    

return msg;

```

---

<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:** [23 December 2020 13:51 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/2 "2020-12-23T13:51:54Z")

</div>

> [@roro50](#):
>
> `let alerte0 = string;`

What do you believe that line to be doing? If you are just trying to create a variable called alerte0 then all you need is  
`let alerte0`  
The line you have tries to initialise it with the contents of a variable called `string` which does not exist.

> [@roro50](#):
>
> `message2={}`

That sets `message2` to be an object with no properties, if you want an empty string then you want  
`message4 = ""`

Finally I suggest that you don't use names like `message` for simple variables, keep names like that for message objects which you are going to pass on to other nodes.

Have you read the docs page Writing Functions? That will give you some help I think.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 December 2020 14:05 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/3 "2020-12-23T14:05:36Z")

</div>

To add, openweathermap also gives condition codes

> **[Weather Conditions - OpenWeatherMap](https://openweathermap.org/weather-conditions)**

using these may simplify your if statements as to weather types. Try creating a look up table for the condition, then use the code to retrive the condition.

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 14:07 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/4 "2020-12-23T14:07:25Z")

</div>

Thank you for your very quick response! I analyze your comments and I keep you informed

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 14:28 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/5 "2020-12-23T14:28:22Z")

</div>

sorry but I don't understand very well (language barrier and I don't know node red)

here are the changes I made

```auto
let alerte0 
let alerte1
let alerte2 
let alerte3
let alarme1
let alarme2
let alarme3
let alarme4

 alerte0 = msg.payload.alerts[0];
 alerte1 = msg.payload.alerts[1];
 alerte2 = msg.payload.alerts[2];
 alerte3 = msg.payload.alerts[3];
 alarme1 = "";
 alarme2 = "";
 alarme3 = ""; 
 alarme4 = "";

if (alerte0=="Moderate rain-flood warning" ||alerte1 =="Moderate rain-flood warning" || alerte2 =="Moderate rain-flood warning" || alerte3 =="Moderate rain-flood warning"){
    alarme1="Pluie"
}
else{ alarme1="";}
if (alerte0=="Moderate wind warning" ||alerte1 =="Moderate wind warning" || alerte2 =="Moderate wind warning" || alerte3 =="Moderate wind warning"){
    alarme2="Vent"
}
else{ alarme2="";}
if (alerte0=="Moderate flooding warning" ||alerte1 =="Moderate flooding warning" || alerte2 =="Moderate flooding warning" || alerte3 =="Moderate flooding warning"){
    alarme3="Inondation"
}
else{ alarme3="";}

if (alarme1==="" && alarme2!==""){
    alarme1=alarme2;
    alarme2={};
}
if (alarme2==="" && alarme3!==""){
    alarme2=alarme3;
    alarme3={};
}
if (alarme3==="" && alarme4!==""){
    alarme3=alarme4;
    alarme4={};
}
    
msg.payload="ALERTE : "+alarme1+alarme2+alarme3+alarme4 ;    

return msg;

```

there is no more compilation error, but in output display I have "ERROR:"  
and however I am with these 3 alerts activate! and I am sure of the way to get this information (msg.payload.alerts [0])  
do you have an idea ?

---

<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:** [23 December 2020 14:44 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/6 "2020-12-23T14:44:33Z")

</div>

Did you read the docs I suggested? It shows you how to do simple debugging of the function. Also add a debug node showing what is in the message going into the node and check that the payload contains an array called `alerts` with four elements. If not sure then show use what the debug node gives.

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 14:50 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/7 "2020-12-23T14:50:12Z")

</div>

I changed the last line to display the alerte0 message =\>  
msg.payload="ALERTE : "+alerte0+alarme2+alarme3+alarme4 ;

with that I have output =\>  
"ALERTE : [object Object]"

at the entry of my node I have:

23/12/2020 à 15:39:24[node: 69342ecf.48994](http://192.168.1.41:1880/#)

msg.payload : Object

object

lat: 49.02

lon: -0.94

timezone: "Europe/Paris"

timezone\_offset: 3600

current: object

minutely: array[61]

daily: array[8]

alerts: array[3]

0: object

sender\_name: "METEO-FRANCE"

event: "Moderate rain-flood warning"

start: 1608705180

end: 1608786000

description: "Although rather usual in this region, locally or potentially dangerous phenomena are expected. (such as local winds, summer thunderstorms, rising streams or high waves)"

1: object

sender\_name: "METEO-FRANCE"

event: "Moderate wind warning"

start: 1608714060

end: 1608786000

description: "Although rather usual in this region, locally or potentially dangerous phenomena are expected. (such as local winds, summer thunderstorms, rising streams or high waves)"

2: object

sender\_name: "METEO-FRANCE"

event: "Moderate flooding warning"

start: 1608714060

end: 1608786000

description: "Although rather usual in this region, locally or potentially dangerous phenomena are expected. (such as local winds, summer thunderstorms, rising streams or high waves)"

---

<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:** [23 December 2020 14:55 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/8 "2020-12-23T14:55:34Z")

</div>

The `alerts` array only has three elements, but you are using four, so the last one is off the end of the array and will be undefined whereas you are testing for an empty string. You can use  
`if (alert0)`  
to test if it exists before testing the contents. Also I see you still have lines like  
`alarme2={};`  
which should be  
`alarme2 = ""`  
That is probably why you are getting `Object` in the output.

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 15:10 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/9 "2020-12-23T15:10:18Z")

</div>

indeed I had forgotten some {}, I modified but it's the same!  
"ALERTE : [object Object]"

I declared 4 messages, and here I actually have 3, because the site sends a message if there is an alert (if no alert, it does not send empty content, but nothing (no path) )

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 15:15 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/10 "2020-12-23T15:15:39Z")

</div>

do I have to add these?

if (alerte0===undefined ) {  
alerte0=""}  
}

---

<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:** [23 December 2020 15:41 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/11 "2020-12-23T15:41:42Z")

</div>

Yes, or the suggestion I gave earlier which is just to use  
`if (alert0)`  
which works because undefined is interpreted as false in a test.  
Also expand the alerts in the debug output and check they consist of just the string.

Edit: Just looked back at the debug and the alert is not just a string, you need to extract the `event` property of the alert.

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 15:46 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/12 "2020-12-23T15:46:07Z")

</div>

Here is what I did, but I have the same message in debugging output  
(23/12/2020 at 16: 44: 01node: 511f3f71.1a6ec8  
msg.payload: string [24]  
"ALERT: [object Object]")

```auto
let alerte0 
let alerte1
let alerte2 
let alerte3
let alarme1
let alarme2
let alarme3
let alarme4

 alerte0 = msg.payload.alerts[0];
 alerte1 = msg.payload.alerts[1];
 alerte2 = msg.payload.alerts[2];
 alerte3 = msg.payload.alerts[3];
 
 if (alerte0===undefined ) {
    alerte0=""
}
if (alerte1===undefined ) {
    alerte1=""
}
if (alerte2===undefined ) {
    alerte2=""
}
if (alerte3===undefined ) {
    alerte3=""
}
 
 alarme1 = "";
 alarme2 = "";
 alarme3 = ""; 
 alarme4 = "";
 

if (alerte0=="Moderate rain-flood warning" ||alerte1 =="Moderate rain-flood warning" || alerte2 =="Moderate rain-flood warning" || alerte3 =="Moderate rain-flood warning"){
    alarme1="Pluie"
}
else{ alarme1="";}
if (alerte0=="Moderate wind warning" ||alerte1 =="Moderate wind warning" || alerte2 =="Moderate wind warning" || alerte3 =="Moderate wind warning"){
    alarme2="Vent"
}
else{ alarme2="";}
if (alerte0=="Moderate flooding warning" ||alerte1 =="Moderate flooding warning" || alerte2 =="Moderate flooding warning" || alerte3 =="Moderate flooding warning"){
    alarme3="Inondation"
}
else{ alarme3="";}

if (alarme1==="" && alarme2!==""){
    alarme1=alarme2;
    alarme2="";
}
if (alarme2==="" && alarme3!==""){
    alarme2=alarme3;
    alarme3="";
}
if (alarme3==="" && alarme4!==""){
    alarme3=alarme4;
    alarme4="";
}
    
msg.payload="ALERTE : "+alerte0+alarme2+alarme3+alarme4 ;    

return msg;

```

---

<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:** [23 December 2020 15:47 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/13 "2020-12-23T15:47:08Z")

</div>

You probably missed the edit I made to my previous post.

> [@Colin](#):
>
> Edit: Just looked back at the debug and the alert is not just a string, you need to extract the `event` property of the alert.

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 15:52 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/14 "2020-12-23T15:52:53Z")

</div>

if i do these

```auto
let alerte0 
 alerte0 = msg.payload.alerts[0];
 if (alerte0===undefined ) {
    alerte0=""
}
msg.payload=alerte0;    
return msg;

```

I have this output:  
23/12/2020 à 16:49:53[node: 7b2406e5.684f88](http://192.168.1.41:1880/#)

msg.payload : Object

{ sender\_name: "METEO-FRANCE", event: "Moderate rain-flood warning", start: 1608705180, end: 1608786000, description: "Although rather usual in this …" }

---

<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:** [23 December 2020 15:54 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/15 "2020-12-23T15:54:18Z")

</div>

That is exactly what is expected, from the debug you posted eariler:

> [@roro50](#):
>
> alerts: array[3]
> 
> 0: object
> 
> sender\_name: "METEO-FRANCE"
> 
> event: "Moderate rain-flood warning"
> 
> start: 1608705180
> 
> end: 1608786000
> 
> description: "Although rather usual in this region, locally or potentially dangerous phenomena are expected. (such as local winds, summer thunderstorms, rising streams or high waves)"

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 15:56 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/16 "2020-12-23T15:56:11Z")

</div>

yes, I forgot part of the way!

---

<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:** [23 December 2020 15:59 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/17 "2020-12-23T15:59:52Z")

</div>

```auto
let alerte0 = msg.payload.alerts[0] ? mst.payload.alerts[0].event : ""

```

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 16:01 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/18 "2020-12-23T16:01:28Z")

</div>

he does not want (: "") there is an error? ?

---

<div class="post-metadata">

**Author:** ![roro50](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@roro50](https://discourse.nodered.org/u/roro50)\
**Post date:** [23 December 2020 16:27 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/19 "2020-12-23T16:27:39Z")

</div>

"TypeError: Cannot read property 'event' of undefined"

---

<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:** [23 December 2020 16:34 UTC](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924/20 "2020-12-23T16:34:03Z")

</div>

I am sure you can work out what the typo I put in the line I posted is, I had mst instead of msg, it should be  
`let alerte0 = msg.payload.alerts[0] ? msg.payload.alerts[0].event : ""`

> **[Conditional (ternary) operator - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)**

[Next page](https://discourse.nodered.org/t/in-1-node-read-several-information-and-make-1-message/37924.md?page=2)
