[Help] Tricky Node-Red automation that test if an old state was true for 5 mins

I can’t wrap my head around how to achieve this new automation in Node Red.

I wan’t to run a script when my phone disconnects from a paired Bluetooth speaker. I have the native HA Android app installed. I have the Bluetooth sensor and Bluetooth connection sensor exposed. The Connection Sensor exposes an attribute that show’s the currently paired device.

What I’m trying to achieve

  • trigger: when Bluetooth speaker disconnects and stays disconnected for 10s
  • conditions: speaker was connected to my phone for 5 mins or more (this is the bit I can’t work out how to achieve)
  • action: run a script.

This is where I’m currently at:

[{"id":"e8cab719.8638b8","type":"trigger","z":"f506f890.db8b98","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"10","extend":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":430,"y":1200,"wires":[["1a932c6a.59f024"]]},{"id":"19247665.85807a","type":"server-state-changed","z":"f506f890.db8b98","name":"Bluetooth Connected","server":"79d7078a.622af8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.sm_g960f_ha_androidnative_bluetooth_connection","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"0","halt_if_type":"num","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":140,"y":1200,"wires":[["e8cab719.8638b8"],[]]},{"id":"1a932c6a.59f024","type":"api-call-service","z":"f506f890.db8b98","name":"","server":"79d7078a.622af8","version":1,"debugenabled":false,"service_domain":"script","service":"turn_on","entityId":"script.morning_bedroom_getting_dressed","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":620,"y":1200,"wires":[[]]},{"id":"79d7078a.622af8","type":"server","z":"","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I am currently running version beta-463-3a3577d-full on my phone for reference.

The bluetooth sensor for connected devices is currently a string and not an array so you have to do a string comparison, I think this is an oversight and it might change in the future.

[{"id":"f204c52d.d9a2b8","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.pixel_2_bluetooth_connection","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"$contains($entity().attributes.connected_paired_devices, \"F4:F5:D8:A3:56:DB\")","halt_if_type":"jsonata","halt_if_compare":"jsonata","outputs":2,"output_only_on_state_change":false,"for":"5","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":286,"y":1552,"wires":[["4dd8e203.9b6a3c"],["1e6aeb82.791e64"]]},{"id":"4dd8e203.9b6a3c","type":"ha-wait-until","z":"ffbd7f06.4a014","name":"","outputs":1,"entityId":"sensor.pixel_2_bluetooth_connection","entityIdFilterType":"exact","property":"afsadf","comparator":"jsonata","value":"$not($contains($entity().attributes.connected_paired_devices, \"F4:F5:D8:A3:56:DB\"))","valueType":"jsonata","timeout":0,"timeoutType":"num","timeoutUnits":"seconds","entityLocation":"payload","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":588,"y":1552,"wires":[["54e099b4.924d28"]]},{"id":"9379a259.b83b7","type":"debug","z":"ffbd7f06.4a014","name":"do stuff","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":908,"y":1552,"wires":[]},{"id":"1e6aeb82.791e64","type":"change","z":"ffbd7f06.4a014","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":608,"y":1600,"wires":[["4dd8e203.9b6a3c"]]},{"id":"54e099b4.924d28","type":"ha-wait-until","z":"ffbd7f06.4a014","name":"","outputs":2,"entityId":"sensor.pixel_2_bluetooth_connection","entityIdFilterType":"exact","property":"afsadf","comparator":"jsonata","value":"$contains($entity().attributes.connected_paired_devices, \"F4:F5:D8:A3:56:DB\")","valueType":"jsonata","timeout":"10","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"payload","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":748,"y":1552,"wires":[[],["9379a259.b83b7"]]}]

You can follow this issue to see if they get changed to arrays.

Thank you for that. I wasn’t aware of the JSONata element.

@Kermit does it matter that the Wait Until attribute is set to afsada?

No, the attribute field doesn’t matter when JSONata is selected but I found I a bug that required the field not to be empty even when using JSONata so that’s why there is random text in there.