i'm trying to create a flow that will output the sensors in alarm to Alexa before launch a script on Home-Assistant that will close all shutter and the arm the alarm.
Main problem is when i need to send the alert to Alexa (and don't execute the service call) or just execute service call where there is no alarm.
i see "ok", but the problem is that i shouldn't see anything if the first node (that check "allarme" word) should stop and avoid sending to the second output.
If you mean that the payload contains "ok" then it does not contain "allarme" so it should be sent to output 2 (because the allarme test has failed).
[Edit] I wonder whether you meant to only send "ok" if none of the alarms is active. As you have it then you will get either an alarm message or an ok for every element of the array. If so then add a line before the loop let ok = true
then in the else condition, instead of sending a message do ok = false
the result will be that at the end of the loop the variable ok will tell you whether there were any alarms. Then you can add
After the main loop, so it will run right through the array, sending alarm messages if necessary, then after that is finished it will send ok if it did not find any alarms.
// START FUNCTION
let ok = true
for (var i = 1; i < zone_status.length; i++) {
if ((zone_status[i]) == "AALARM") {
msg.payload = "Sensore" + (zone_name[i]) + " è in allarme,"
node.send(msg)
}
else {
ok = false
}
}
if (ok) {
node.send(msg)
}
// START FUNCTION
let ok = true
for (var i = 1; i < zone_status.length; i++) {
if ((zone_status[i]) == "ALARM") {
msg.payload = "Sensore" + (zone_name[i]) + " è in allarme,"
node.send(msg)
}
else {
ok = false
}
}
if (ok) {
msg.payload = "ok"
node.send(msg)
}
ok, i've fixed the code, but when no "alert", the second output of the switch is not triggered
Sorry, the changes I suggested are all wrong. So wrong that I am surprised you did not see it when working out what it does :). You don't need the else at all, it should set ok to false whenever it sends a alarm message, so that it won't send ok if it sends any alarms.
let ok = true
for (var i = 1; i < zone_status.length; i++) {
if ((zone_status[i]) == "ALARM") {
msg.payload = "Sensore" + (zone_name[i]) + " è in allarme,"
node.send(msg)
ok = false
}
}
if (ok) {
msg.payload = "ok"
node.send(msg)
}
@Colin latest posted code will work fine but you might his a subtle bug due to msg being an object so settings msg.payload for each alarm actually changes the value of .payload for already sent messages & could impact your flows in odd ways.
To avoid this possible anomaly, you might want to consider sending new messages...
let ok = true;
for (let i = 1; i < zone_status.length; i++) {
if ((zone_status[i]) == "ALARM") {
//alarm found - create and send a new message object...
let m = { payload: "Sensore" + (zone_name[i]) + " è in allarme," }
node.send(m);
ok = false;
}
}
if (ok) {
msg.payload = "ok"
node.send(msg)
}