Node Red JSONata and filter?

Hello! I'm very new to Node Red and I am trying to create a flow that will check all motion sensors using the Get History for the last 20 minutes. Specifically if the sensor changed to On in the last 20 minutes.

Seems give me good msg.payload and I've used a Change node to remove unnecessary information.

Works pretty well, and I get:

{"entity_id":"binary_sensor.master_closet_motion_sensor_motion","state":"off"},{"entity_id":"binary_sensor.outdoor_motion_sensor_motion","state":"on"} etc

What I'm looking for is 1) a way to filter out and only get state=on, I've tried a Function Node, and I've tried to futher add another step in Change, but I can't seem to figure it out. I've tried JSONata Exerciser and seem to have $filter working, but get errors when I bring that back to Node Red. Is there something I'm missing?

Once I get that, then the next step would be to filter out sensors I don't care about. Sorry I'm really new and trying to learn this,would appreciate any nudge forward,

[{"id":"4c5bb995.6a0008","type":"tab","label":"Test","disabled":false,"info":""},{"id":"e9e06ac9.a0a128","type":"change","z":"4c5bb995.6a0008","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.(\t {\t "entity_id": $.'entity_id',\t "state": $.'state'\t\t }\t\t)\t","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$distinct(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":80,"wires":[]},{"id":"2b4ee9cd.3e63a6","type":"api-get-history","z":"4c5bb995.6a0008","name":"","server":"3a95bff3.8b691","version":0,"startdate":"","enddate":"","entityid":"binary_sensor._sensor_motion","entityidtype":"includes","useRelativeTime":true,"relativeTime":"20m","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":250,"y":80,"wires":[["e9e06ac9.a0a128"]]},{"id":"b81cb0f5.ac88a","type":"inject","z":"4c5bb995.6a0008","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":80,"wires":[["2b4ee9cd.3e63a6"]]},{"id":"932d2cdc.f281b","type":"comment","z":"4c5bb995.6a0008","name":"Detect Sleep Testing","info":"","x":130,"y":40,"wires":},{"id":"3a95bff3.8b691","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You should be able to filter your JSONata expression by adding [state="on"],can not say 100% as you provided no examples of input data or the code(with out importing whole flow and fixing corruption)

Also please post code between backticks
e.g.
```
code
```
as without them your posted flow will be corrupted by the forums styling.

2 Likes

Thanks! I appreciate that.

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