Can I return a real null value to a multi out function from an else if statement?

Mistakes are allowed.

Everyone makes then and they are part of learning. I can vouch for that. :wink:

else {
    ""
};

Is also a problem.

I think it should be something like:

else {
   msg1.payload =  "";
};
return msg1;

But not sure.

Ok I cleaned things up but I still have a problem with using the or statement.

I labeled the conditions and included json that should pass and what rule they should be filtered by. Also one that should return null.

Right now it is as follows.

JSON for Rule 1 passes through rule 1,
JSON for Rule 2 passes through rule 2,
this is where the problem is:
JSON for Rule 3 passes through rule 2
JSON for null passes through rule 2


let msg1 = {};
// rule 1
if (evco === "CrossRegionDetection" && action === "Start" && ivsRule === "ivs")
    
msg1.payload = {"domain": haDom,"service": serv, "data": {"entity_id": cameraEntity},
    "driveFolder": "single",
    "title": cameraName,};
// rule 2
else if (evco === "CrossRegionDetection" && action === "Start" && ivsRule === "first" || "garden" || "alley" || "basement")

msg1.payload = {"domain": haDom, "service": serv, "data": {"entity_id": cameraEntity},
    "driveFolder": "multi",
    "title": ivsRule,};
// rule 3
else if (evco === "VideoMotion" && action === "Start")

msg1.payload = {"domain": haDom,"service": serv, "data": {"entity_id": cameraEntity},
    "driveFolder": "single",
    "title": cameraName,};

else {
msg1 = null
};






let msg2 = null;





return [ msg1, msg2 ]

I added a rule key to the output so you can see what passes through which rule in the debug.

[{"id":"492a144664cddd58","type":"debug","z":"60f2d2277843c698","name":"debug 21","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":500,"wires":[]},{"id":"bd06552fad2076d9","type":"debug","z":"60f2d2277843c698","name":"debug 22","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":600,"wires":[]},{"id":"2889b1460434c0d1","type":"inject","z":"60f2d2277843c698","name":"second (rule 1)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event_type\":\"dahua_event_received\",\"event\":{\"Code\":\"CrossRegionDetection\",\"action\":\"Start\",\"index\":\"3\",\"data\":{\"Action\":\"Appear\",\"Class\":\"Normal\",\"CountInGroup\":1,\"DetectRegion\":[[4168,3111],[4405,7778],[5916,7510],[5388,3160]],\"EventSeq\":26,\"FrameSequence\":2419837,\"GroupID\":0,\"IndexInGroup\":0,\"Mark\":0,\"Name\":\"ivs\",\"Object\":{\"Action\":\"Appear\",\"BoundingBox\":[3752,2840,5112,4664],\"Center\":[4432,3752],\"Confidence\":0,\"FrameSequence\":0,\"ObjectID\":176,\"ObjectType\":\"Unknown\",\"RelativeID\":0,\"Source\":0,\"Speed\":0,\"SpeedTypeInternal\":0},\"PTS\":58592,\"RuleId\":1,\"Sequence\":0,\"Source\":0,\"Track\":null,\"UTC\":1705948123,\"UTCMS\":0},\"name\":\"XVR second floor\",\"DeviceName\":\"XVR second floor\"},\"origin\":\"LOCAL\",\"time_fired\":\"2024-01-22T23:28:45.514691+00:00\",\"context\":{\"id\":\"01HMSR2GEAFBBXNWHDVCK556PJ\",\"parent_id\":null,\"user_id\":null}}","payloadType":"json","x":180,"y":420,"wires":[["e9b06bb2bc44145a"]]},{"id":"bcfc9f4e928bf816","type":"inject","z":"60f2d2277843c698","name":"basement (rule 2)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event_type\":\"dahua_event_received\",\"event\":{\"Code\":\"CrossRegionDetection\",\"action\":\"Start\",\"index\":\"2\",\"data\":{\"Action\":\"Appear\",\"Class\":\"Normal\",\"CountInGroup\":1,\"DetectRegion\":[[5697,7792],[7827,6965],[7954,6187],[5952,7184]],\"EventSeq\":5,\"FrameSequence\":2418608,\"GroupID\":0,\"IndexInGroup\":0,\"Mark\":0,\"Name\":\"basement\",\"Object\":{\"Action\":\"Appear\",\"BoundingBox\":[6312,6024,6984,7272],\"Center\":[6648,6648],\"Confidence\":0,\"FrameSequence\":0,\"ObjectID\":33,\"ObjectType\":\"Unknown\",\"RelativeID\":0,\"Source\":0,\"Speed\":0,\"SpeedTypeInternal\":0},\"PTS\":37761,\"RuleId\":1,\"Sequence\":0,\"Source\":0,\"Track\":null,\"UTC\":1705948103,\"UTCMS\":0},\"name\":\"XVR first floor\",\"DeviceName\":\"XVR first floor\"},\"origin\":\"LOCAL\",\"time_fired\":\"2024-01-22T23:28:24.743001+00:00\",\"context\":{\"id\":\"01HMSR1W57T75MKGFXS2J1A8GM\",\"parent_id\":null,\"user_id\":null}}","payloadType":"json","x":180,"y":480,"wires":[["e9b06bb2bc44145a"]]},{"id":"6f88ea7e9ba32377","type":"inject","z":"60f2d2277843c698","name":"first (rule 2)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event_type\":\"dahua_event_received\",\"event\":{\"Code\":\"CrossRegionDetection\",\"action\":\"Start\",\"index\":\"2\",\"data\":{\"Action\":\"Cross\",\"Class\":\"Normal\",\"CountInGroup\":1,\"DetectRegion\":[[2293,6198],[3986,6222],[3568,7972],[2658,8069],[1893,6660],[2293,6344]],\"Direction\":\"Leave\",\"EventSeq\":10,\"FrameSequence\":2419240,\"GroupID\":0,\"IndexInGroup\":0,\"Mark\":0,\"Name\":\"first\",\"Object\":{\"Action\":\"Appear\",\"BoundingBox\":[2944,3864,4992,7960],\"Center\":[3968,5912],\"Confidence\":0,\"FrameSequence\":0,\"ObjectID\":35,\"ObjectType\":\"Unknown\",\"RelativeID\":0,\"Source\":0,\"Speed\":0,\"SpeedTypeInternal\":0},\"PTS\":48461,\"RuleId\":2,\"Sequence\":0,\"Source\":0,\"Track\":null,\"UTC\":1705948113,\"UTCMS\":0},\"name\":\"XVR first floor\",\"DeviceName\":\"XVR first floor\"},\"origin\":\"LOCAL\",\"time_fired\":\"2024-01-22T23:28:35.553034+00:00\",\"context\":{\"id\":\"01HMSR26Q1RR5FPACJ8A30RQES\",\"parent_id\":null,\"user_id\":null}}","payloadType":"json","x":190,"y":540,"wires":[["e9b06bb2bc44145a"]]},{"id":"8bcaf65812a52597","type":"inject","z":"60f2d2277843c698","name":"should return null","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event_type\":\"dahua_event_received\",\"event\":{\"Code\":\"VideoMotion\",\"action\":\"Stop\",\"index\":\"3\",\"data\":{\"SmartMotionEnable\":false},\"name\":\"XVR second floor\",\"DeviceName\":\"XVR second floor\"},\"origin\":\"LOCAL\",\"time_fired\":\"2024-01-22T23:24:13.969622+00:00\",\"context\":{\"id\":\"01HMSQT78HSZHSW6Y2HY3PXDW6\",\"parent_id\":null,\"user_id\":null}}","payloadType":"json","x":180,"y":660,"wires":[["e9b06bb2bc44145a"]]},{"id":"e9b06bb2bc44145a","type":"function","z":"60f2d2277843c698","name":"camera","func":"// isolate relevant incoming JSON\nlet cameraName = msg.payload.event.DeviceName;\nlet ivsRule = msg.payload.event.data.Name;\nlet evco = msg.payload.event.Code;\nlet action = msg.payload.event.action;\n\n// phone state\nlet phoneState = global.get('homeassistant.homeAssistant.states[\"sensor.pixel_7_phone_state_2\"].state');\n\n\n\n// create entity type for service call\nconst haDom = \"camera\";  // domain\nconst serv = \"snapshot\"; // service\nconst entityType = \"camera.\"; // same as domain with period\n// replace spaces with _   add entity type at begining   add _main to end\nlet cameraEntity = entityType + (cameraName.toLowerCase()).replace(/\\s/g, \"_\") + \"_main\";\n\n\nlet msg1 = {};\n// rule 1\nif (evco === \"CrossRegionDetection\" && action === \"Start\" && ivsRule === \"ivs\")\n    \nmsg1.payload = {\"domain\": haDom,\"service\": serv, \"data\": {\"entity_id\": cameraEntity},\n    \"driveFolder\": \"single\",\n    \"title\": cameraName,\n    \"rule\": \"one\",};\n// rule 2\nelse if (evco === \"CrossRegionDetection\" && action === \"Start\" && ivsRule === \"first\" || \"garden\" || \"alley\" || \"basement\")\n\nmsg1.payload = {\"domain\": haDom, \"service\": serv, \"data\": {\"entity_id\": cameraEntity},\n    \"driveFolder\": \"multi\",\n    \"title\": ivsRule,\n    \"rule\": \"two\",};\n// rule 3\nelse if (evco === \"VideoMotion\" && action === \"Start\")\n\nmsg1.payload = {\"domain\": haDom,\"service\": serv, \"data\": {\"entity_id\": cameraEntity},\n    \"driveFolder\": \"single\",\n    \"title\": cameraName,\n    \"rule\": \"three\",};\n\nelse {\nmsg1 = null\n};\n\n\n\n\n\n\nlet msg2 = null;\n\n\n\n\n\nreturn [ msg1, msg2 ]","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":560,"wires":[["492a144664cddd58"],["bd06552fad2076d9"]]},{"id":"3796393bc7bed77a","type":"inject","z":"60f2d2277843c698","name":"motion start (rule 3)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event_type\":\"dahua_event_received\",\"event\":{\"Code\":\"VideoMotion\",\"action\":\"Start\",\"index\":\"15\",\"data\":{\"SmartMotionEnable\":false},\"name\":\"XVR front step\",\"DeviceName\":\"XVR front step\"},\"origin\":\"LOCAL\",\"time_fired\":\"2024-01-22T23:24:13.506340+00:00\",\"context\":{\"id\":\"01HMSQT6T2NSYRJGYGYFVE2K5Q\",\"parent_id\":null,\"user_id\":null}}","payloadType":"json","x":170,"y":600,"wires":[["e9b06bb2bc44145a"]]}]

if (evco === "CrossRegionDetection" && action === "Start" && ivsRule === "first" || "garden" || "alley" || "basement")

Since the string "garden" is always considered to be true then the if will always be considered true. Possible you meant this or something similar.
if (evco === "CrossRegionDetection" && action === "Start" && (ivsRule === "first" || ivsRule === "garden" || ivsRule === "alley" || ivsRule === "basement"))

1 Like

That was it. Looking at the mistake I made and the solution, it makes sense to me now. Thank you.

Guessing you did not see my reply hours ago.

1 Like

I did miss that.

In an else if statement, once there is a match, does it skip the rest of the block? Would a switch statement be more appropriate if I wanted it to stop the rest of the block only?

If I understand correctly, adding break at the end of a case it will jump all remaining blocks of code?

Your question is not clear, please show a simple code example of what you are trying to do.

Yes. And super easy to test: if...else - JavaScript | MDN

Yes, however this information is readily available & simple to try out: switch - JavaScript | MDN


One thing I often point out is: this is a visual flow based development tool & all the things you speak of in this thread can be done using the visual flow paradigm - before learning to code js, perhaps you should learn node-red?

ouch I have been using it for 3 years now. I'm often the on HA's nodered forum explaining how to use jsonata to pass variables and have quite complex flows.

This is my first real venture into the function node. Everything works. I've been trying to minimize as much as I can. Here's the link to the entire function code.

And the project

Any comments would be much appreciated.I apologize for the snippets and not the whole code. I was trying to learn my way through it.

My apologies. I really do try not to assume but sometimes I slip and I slipped up here. Please forgive me :smiling_face:

I hope you appreciate my intent though? That much of what you are doing is possible in a more node-red way. And not for the sake of doing it in a flow manner but because a lot of the nodes were designed to help users struggling with code to do things in a no code/low code manner.

I don't have screen time today but will try to take a look and provide constructive comments.

Yeah no definitely, I laughed when I saw that. I'm always reading here, this forum has been instrumental in my progress. I don't post here much because I usually find what I am looking for.

I'd appreciate any advice. I know the message creation is convoluted and some of it's logic statements unnecessary. I was considering changing this block to a 'switch'.

image

I just want to make sure it was not continuing if one of the two conditions are met.

So what exactly do you mean by
In an else if statement, once there is a match, does it skip the rest of the block
If you mean, with a sequence of if, else if, else if, else statements, once one of the if's is met does it skip the others, then the answer is yes, that is what 'else' means.

The reason your question is ambiguous is that it is not clear what you mean by 'block'. You might have meant whether you can so something like

if (a) {
  // some code
  break;
  // some more code
} else if ...

and were asking if break can be used to break you out of that block.

My understanding is that this complete else if statement was a code block.

Screenshot 2024-01-28 055242

If I try to insert a break, it gives me this error

A 'break' statement can only be used within an enclosing iteration or switch statement.(1105)

Screenshot 2024-01-28 055201

If you mean the complete if, else if, else is a code block then possibly. But so is anything contained in { .. }

Why would you need the break? The else already implies that it will only run that if the if fails. What else could it mean?

To explain further if (aTT < cameraTest) returns true, it jumps the rest of the block and then reads the next block that starts with if (att < cameraTest)

In this scenario if papayas is true it jumps to the end and sends the message

Or does every thing need to be in the switch?

Yes

No, the break sends it to the end of the Switch statement.

1 Like

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