Multiple device trackers send command if statement

New to node-red. I have for items being tracked. based on whether they are home or away I want to change it up.

prefer if statement so If 1 and 2 and 3 = home and 4 <> home then

this possible?

Anything is possible, but I’m not quite sure what you’re attempting to accomplish.

Depending who is home it will set blue iris to what profile.

If adults home and kids not then do this.

if adults not but 1 of the kids are then do that.

Never wrote anything like this before besides 100 nodes.

You could use a function node to assign binary values to variables based upon states.

If (nobody is home) {
  this payload;
} else if (adult 1 is home or adult 2 is home) {
  this payload;
} else if (adult 1 is home or adult 2 is home and child 1 is home) {
  this payload;
} else if (no adult is home and child 1 or 2 or 3 is home) {
  this payload;
}
return msg;

Then use a switch node to route the message to blue iris.

I’m not a javascript expert, but this seems like the basic logic, or something along these lines. There’s probably 100 ways to do this and many far more elegant. I think the same could be accomplished with a bunch of node spaghetti.

1 Like

Using a trigger-state node.

[{"id":"a1814098.452c2","type":"trigger-state","z":"5eb3594f.d294b8","name":"","entityid":"device_tracker.person1,device_tracker.person2,device_tracker.person3,device_tracker.person4","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"id":"6k9qro0qc4q","targetType":"entity_id","targetValue":"device_tracker.person1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"8pwpg1aui9b","targetType":"entity_id","targetValue":"device_tracker.person2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"vgjr1g8xv2","targetType":"entity_id","targetValue":"device_tracker.person3","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"x2rz00zs32p","targetType":"entity_id","targetValue":"device_tracker.person4","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"not_home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":588,"y":2208,"wires":[[],[]]}]

Thats what I did … Node spaghetti

So your saying just make 4 of these. change values as needed?

No you only need one. It will trigger for each of the four device_trackers and the message will be sent to the first output if the constraints are true and to the second if they are not.

The constraints are ‘ands’ so the logic so be as you described above. If person1 is home and person2 is home and person3 is home and person4 is not home send to output 1 otherwise send to the second output.

So if person 1 not home and others are home it still does same output just changes payload?

No, if person 1 is not home it will send the message to the second output.

[{"id":"ae62c3f0.f9131","type":"server-state-changed","z":"5eb3594f.d294b8","name":"","version":1,"entityidfilter":"device_tracker.adult1,device_tracker.adult2,device_tracker.child1,device_tracker.child2","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":398,"y":2576,"wires":[["4065c8d4.818d38"]]},{"id":"4065c8d4.818d38","type":"function","z":"5eb3594f.d294b8","name":"","func":"const states = global.get('homeassistant').homeAssistant.states;\n\nconst adult1 = states[\"device_tracker.adult1\"].state;\nconst adult2 = states[\"device_tracker.adult2\"].state;\nconst child1 = states[\"device_tracker.child1\"].state;\nconst child2 = states[\"device_tracker.child2\"].state;\n\nif(adult1 === \"home\" && adult2 === \"home\" && child1 !== \"home\" && child2 !== \"home\") {\n    msg.payload = \"both adults home but no kids\";\n} else if(adult1 === \"not_home\" && adult2 === \"not_home\" && (child1 === \"home\" || child2 === \"home\")) {\n    msg.payload = \"no adults home but at least one kid is\";\n} else {\n    // this stops flow if one of the above condition aren't met\n    return;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":818,"y":2576,"wires":[[]]}]

So Kermit.
I copied what you said to do. Only thing that does not work is the Else statement. Have any idea why that statement is not running?

hmm? It stops the flow if the above two conditions are not met. You weren’t really specific on what you were trying to accomplish so I was just giving you an example to work from.

It was a side note. I was just curious because I am testing this setup right now.

Right now I have a few times when it runs and shouldnt be but trying to figure out who the problem child is first