Node Red - IF AND OR

Hi all, i’m a new in automation and maybe my question is already discussed by i not have find nothing to clearify me.

i try to understand how is possibile che “if al least” my binary_sensor are “on” and check “if all my binary_sensor” are “off”. To explain.
in my flow i have 1 flow variable "flow.alarm.alarm " and i want set this a “true” if al least one of my binary_sensor are in “on” state and set to “off” if all binary_sensor are “off”

Maybe i lose my self in water glass, but for now i’m disoriented. I a web developer with 20 years of experience in nodejs / javascript but i need understand the nodes concept in node-red better.

maybe work

in the node “get entities” i get all input_boolean.alarm_finestra* and send in msg.payload the array

in my node function i check

var entities = msg.payload;
var allTurnedOff = true;

for (var i = 0; i < entities.length; i++) {
    if (entities[i].state !== "off") {
        allTurnedOff = false;
        break;
    }
}

var result = {
    entities: entities,
    all_turn_off: allTurnedOff
};

msg.payload = result;
return msg;

debug recive the new payload with all_turn_off in true o false

With just nodes you do it like this:

The Current State nodes have the If field set to true to get two outputs.

[{"id":"889584e17e063dd0","type":"trigger-state","z":"120358abd7c22d30","name":"","server":"","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":"input_boolean\\.alarm_finestra.*","entityIdType":"regex","debugEnabled":false,"constraints":[{"targetType":"entity_id","targetValue":"input_boolean.alarm_finestra_01","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"input_boolean.alarm_finestra_02","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"input_boolean.alarm_finestra_03","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"x":270,"y":5712,"wires":[["9939d8bcee213f99"],["4fc512cfbfe37a1d"]]},{"id":"5124ab1b6d00fe31","type":"comment","z":"120358abd7c22d30","name":"if you had a static number of booleans you could use the conditions ","info":"","x":336,"y":5664,"wires":[]},{"id":"9939d8bcee213f99","type":"change","z":"120358abd7c22d30","name":"all off","rules":[{"t":"set","p":"alarm.alarm","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":5712,"wires":[[]]},{"id":"4fc512cfbfe37a1d","type":"change","z":"120358abd7c22d30","name":"at least one on","rules":[{"t":"set","p":"alarm.alarm","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":5760,"wires":[[]]},{"id":"2070a49c499861ee","type":"server-state-changed","z":"120358abd7c22d30","name":"","server":"","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"input_boolean\\.alarm_finestra.*","entityIdType":"regex","outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":280,"y":5856,"wires":[["f00151f2cb8e9112"],["a9c2a0a9d4aec53c"]]},{"id":"ef36099b27836651","type":"comment","z":"120358abd7c22d30","name":"example 2","info":"","x":156,"y":5808,"wires":[]},{"id":"f00151f2cb8e9112","type":"change","z":"120358abd7c22d30","name":"at least one on","rules":[{"t":"set","p":"alarm.alarm","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":576,"y":5856,"wires":[[]]},{"id":"a9c2a0a9d4aec53c","type":"ha-get-entities","z":"120358abd7c22d30","name":"get total","server":"","version":0,"rules":[{"property":"entity_id","logic":"is","value":"input_boolean\\.alarm_finestra.*","valueType":"re"}],"output_type":"count","output_empty_results":false,"output_location_type":"msg","output_location":"total","output_results_count":1,"x":560,"y":5904,"wires":[["45bb05c4c5781964"]]},{"id":"45bb05c4c5781964","type":"ha-get-entities","z":"120358abd7c22d30","name":"get off count","server":"","version":0,"rules":[{"property":"entity_id","logic":"is","value":"input_boolean\\.alarm_finestra.*","valueType":"re"},{"property":"state","logic":"is","value":"off","valueType":"str"}],"output_type":"count","output_empty_results":false,"output_location_type":"msg","output_location":"count","output_results_count":1,"x":718,"y":5904,"wires":[["645688667179a3fd"]]},{"id":"645688667179a3fd","type":"switch","z":"120358abd7c22d30","name":"all off?","property":"total","propertyType":"msg","rules":[{"t":"eq","v":"count","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":866,"y":5904,"wires":[["986b6d6b0c6cc534"]]},{"id":"986b6d6b0c6cc534","type":"change","z":"120358abd7c22d30","name":"all off","rules":[{"t":"set","p":"alarm.alarm","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":5904,"wires":[[]]}]

If I am understanding your question, I am doing something similar with two independent bedroom lamps.

This flow sets a flow variable for the room status. If either light is on, the room status is on.

Here is the code for the last function node where the room status is set:

var kimStat = flow.get('kimStatus') || 'OFF';       //Default is off
var steveStat = flow.get('steveStatus') || 'OFF';   //Default is off
msg.payload = 'OFF';                                //Default is off

// If either steve's or kim's light is on, the room is on.
if(kimStat == 'ON' || steveStat == 'ON'){
    msg.payload = 'ON';
}

flow.set('roomStatus',msg.payload);
return msg;