How to make a flow, a topic is missing

Hi guys, Can someone help me?

My question is, I want to do an automation. When outputting multiple topics, if a topic is missing, then turn on the switch.

For example, the usual output are A, B and C 3 topics, but when C topic is missing then turn on the switch

I think you need to explain more about the timing of things. Also, a better explanation of what you are trying to do would help us help you.

And what turns the switch off?

ok! I try to use ai detect to create automation!

The ai sensor outputs multiple topics: bed, person, chair...

I know that using switch can monitor when the topic person appears, create automation

I want to know how to build automation if the topic of person is missing from the ai sensor output.

do not worry about this , I just want to know how to let node red know when a specific topic is missing in the output!

A message can only have one topic property, so it is not clear what you mean by a missing topic?

Do you mean that you have multiple messages with different topics, and if you do not see a message from a particular topic within a given time then you want to know that? If so then you can use a Trigger node set to Send Nothing, then wait a given time, and then send the error message. Set Extend Delay and then provided messages keep coming in then it keeps resetting the delay so it never sends the error message. When no messages come in for the configured time then it sends it.

By setting Handling to Each it will treat each topic seperately.

You probably need to show us examples of your data. One example where "the topic person" is present and one where it is missing.

2021/12/28 下午1:19:11node: 94b53f53b6f1b065
msg.payload : array[4]
array[4]
0: object
confidence: 0.4633109
label: "couch"
y_min: 461
x_min: 779
y_max: 679
x_max: 1144
1: object
confidence: 0.46816668
label: "person"
y_min: 418
x_min: 485
y_max: 581
x_max: 673
2: object
confidence: 0.4898363
label: "chair"
y_min: 492
x_min: 1189
y_max: 638
x_max: 1280
3: object
confidence: 0.7694483
label: "chair"
y_min: 485
x_min: 1102
y_max: 628
x_max: 1205

Hi this debug is an example, AI detected 2 chairs, 1 person and 1 couch.

I want to create an automated flow When "person" is missing in the output

The produced output is always an array ?

If yes, you could try in a function node:

let person = msg.payload.filter(x => x.label == "person").length

if(person == 0){
    // no person...
}

This flow uses Jsonata in the switch node to route the complete message depending on whether the array contains label: "person" "person" in $lookup(payload, 'label')
It's my first attempt to use Jsonata, no doubt someone with greater knowledge could give a better solution.

[{"id":"a10a8e4bce1a49a2","type":"switch","z":"a531e868a7ed264e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"\"person\" in $lookup(payload, 'label')","vt":"jsonata"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":550,"y":460,"wires":[["d9e6a10898b10f99"],["992ad6f1be7a088f"]]},{"id":"d9e6a10898b10f99","type":"debug","z":"a531e868a7ed264e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":440,"wires":[]},{"id":"992ad6f1be7a088f","type":"debug","z":"a531e868a7ed264e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":480,"wires":[]},{"id":"ecb9b37cb628507d","type":"inject","z":"a531e868a7ed264e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":440,"wires":[["a494ade5aa4420a3"]]},{"id":"a494ade5aa4420a3","type":"function","z":"a531e868a7ed264e","name":"Does include person","func":"msg.payload = [];\nmsg.payload[0] ={'confidence': 0.4633109,\n'label': \"couch\",\n'y_min': 461,\n'x_min': 779,\n'y_max': 679,\n'x_max': 1144};\nmsg.payload[1] ={'confidence': 0.46816668,\n'label': \"person\",\n'y_min': 418,\n'x_min': 485,\n'y_max': 581,\n'x_max': 673};\nmsg.payload[2] ={'confidence': 0.4898363,\n'label': \"chair\",\n'y_min': 492,\n'x_min': 1189,\n'y_max': 638,\n'x_max': 1280};\nmsg.payload[3] ={'confidence': 0.7694483,\n'label': \"chair\",\n'y_min': 485,\n'x_min': 1102,\n'y_max': 628,\n'x_max': 1205};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":440,"wires":[["a10a8e4bce1a49a2"]]},{"id":"d57286a8aa76d6d7","type":"function","z":"a531e868a7ed264e","name":"Does not include person","func":"msg.payload = [];\nmsg.payload[0] ={'confidence': 0.4633109,\n'label': \"couch\",\n'y_min': 461,\n'x_min': 779,\n'y_max': 679,\n'x_max': 1144};\nmsg.payload[1] ={'confidence': 0.46816668,\n'label': \"table\",\n'y_min': 418,\n'x_min': 485,\n'y_max': 581,\n'x_max': 673};\nmsg.payload[2] ={'confidence': 0.4898363,\n'label': \"chair\",\n'y_min': 492,\n'x_min': 1189,\n'y_max': 638,\n'x_max': 1280};\nmsg.payload[3] ={'confidence': 0.7694483,\n'label': \"chair\",\n'y_min': 485,\n'x_min': 1102,\n'y_max': 628,\n'x_max': 1205};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":480,"wires":[["a10a8e4bce1a49a2"]]},{"id":"1bc44afb4286b294","type":"inject","z":"a531e868a7ed264e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":480,"wires":[["d57286a8aa76d6d7"]]}]

The expression payload[label='person']~>$exists() returns a tue/false, which is nice to use in a switch statement that only has 2 output ports.

Alternatively, if you want your flow to branch based upon the number of chairs (e.g. 0, 1, 2 or more), you can use [payload[label='chair']]~>$count() to return a number (the outer square brackets ensure the output is an array, even if none match) ...

... and then add as many outputs as you need:

some time like that

2021/12/30 上午12:23:26node: 94b53f53b6f1b065
msg.payload : array[3]
array[3]
0: object
confidence: 0.40182823
label: "chair"
y_min: 487
x_min: 1186
y_max: 644
x_max: 1280
1: object
confidence: 0.4348055
label: "chair"
y_min: 482
x_min: 1129
y_max: 626
x_max: 1203
2: object
confidence: 0.46988472
label: "bed"
y_min: 438
x_min: 146
y_max: 686
x_max: 1143

Much easier is in my opinion to split the array with a split node and investigate the single messages.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.