Frigate Data - Convert to Object then limit by score

Not sure if anyone is using Node-Red for Frigate/MQTT automation, but I’m having an issue with parsing out messages based on the top score of Frigate Data. See this section of Frigate’s Documentation.

What I want to do is similar to the above documentation, but in Node-Red. Right now Frigate triggers the flow from MQTT events, the JSON node parses and converts the data from string to objects, and then the change node separates all the messages. Without the following function node, it works perfectly. I get notifications and all is well.

But now I’d like to limit notifications based on the score of the data. I’ve set the change node to set msg.score to the value of payload.after.top_score. I’ve tried out a few function nodes to basically only return the message if the score is above a certain threshold, but nothing has worked so far. This is what the function node currently has:

if (msg.score > 0.8) {
    return msg;

And here is my node-red flow if you want to test it out:

[{"id":"01af693ee023282e","type":"tab","label":"Test","disabled":false,"info":"","env":[]},{"id":"612da2b7db5b290f","type":"change","z":"01af693ee023282e","name":"Extract All Data + Snapshot","rules":[{"t":"set","p":"eventid","pt":"msg","to":"payload.after.id","tot":"jsonata"},{"t":"set","p":"camera","pt":"msg","to":"payload.after.camera","tot":"jsonata"},{"t":"set","p":"area","pt":"msg","to":"payload.after.entered_zones","tot":"jsonata"},{"t":"set","p":"object","pt":"msg","to":"payload.after.label","tot":"jsonata"},{"t":"set","p":"image","pt":"msg","to":"\"http://ip-address:port/api/events/\" & msg.eventid & \"/snapshot.jpg\"\t","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"\"String to be sent to phones\"","tot":"jsonata"},{"t":"set","p":"score","pt":"msg","to":"payload.after.top_score","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":495,"y":100,"wires":[["3ab601c6ed7f1234"]]},{"id":"c42d11c52d0ab405","type":"json","z":"01af693ee023282e","name":"","property":"payload","action":"","pretty":false,"x":250,"y":100,"wires":[["612da2b7db5b290f"]]},{"id":"00d8fd31b41c181d","type":"pushover api","z":"01af693ee023282e","keys":"","title":"Motion","name":"Notification","x":495,"y":180,"wires":[]},{"id":"3cd236b1cc275232","type":"mqtt in","z":"01af693ee023282e","name":"Frigate/Events","topic":"frigate/events","qos":"0","datatype":"auto","broker":"","nl":false,"rap":true,"rh":0,"inputs":0,"x":75,"y":100,"wires":[["c42d11c52d0ab405"]]},{"id":"bb051b2b6687aa4a","type":"delay","z":"01af693ee023282e","name":"1 msg/minute","pauseType":"timed","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":300,"y":180,"wires":[["00d8fd31b41c181d"]]},{"id":"3ab601c6ed7f1234","type":"function","z":"01af693ee023282e","name":"","func":"if (msg.score > 0.8) {\n    return msg;\n} else {\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":115,"y":180,"wires":[["bb051b2b6687aa4a"]]}]

Any suggestions are greatly appreciated!

Your code in the function node seems incomplete.
Try to use a switch node instead, since made exactly for what you want.

Thanks for the tip - you’re right, switch seems like a much better node.

But I’ve figured out the issue and not sure how to solve it. I set a debug node after the first switch node to show me msg.score. It doesn’t actually retrieve the score. So Set msg.score to the value payload.after.top_score returns the following:

if the value type is number, NaN
if the value type is string, undefined
if the value type is expression, undefined <— this is what all the other values are set to
if the value type is JSON, the node throws an error.

Would the parse node be causing this issue? If I set a msg.payload debug node after the parser, I can read all the data (including top_score) without any issues. Seems strange that any only strings get read by the switch node…

try to force it to a number, put this before and use move rather than set for payload.after.top_score and any message that you move into a new msg position.

Sounds like you might have written the variable name wrong, since one of them should give a result.
When you see the output in the debug list then the value you want should have some icons to the right of it when holding the cursor over it.
One of the icons should be “copy path”. Use that to get the right typing.

@WallyR I didn’t see any of that unfortunately in the debug. It looks like the value just wasn’t passed at all. It looks like @Mikefila offered a solution that worked. Thanks for your suggestion tho!

@Mikefila Thank you!!! This solved it, with some slight tweaking. I’ve attached a screenshot of my flow, but using your suggestions, I added a switch node after the JSON Parse node and set the msg.score to $number(payload.after.top_score).

Then in the following Switch/Extract node, I didn’t need to add any other set or move variable, msg.score was simply part of the payload. Now the switch node only allowing >= 0.8 works without issue!

Thanks again for your help.

Full flow: