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:
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.
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.
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.
sorry but I don't understand very well (language barrier and I don't know node red)
here are the changes I made
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 ?
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.
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)"
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.
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) )
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.
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]")
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;
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 : ""