Node-Red function to loop on all battery levels. Is it possible?

I currently have a function to chack all of my battery levels and send an email if any get below 25%.
image

I’m doing this entity-by-entity.

This works fine but as I add devices, I will also need to add them to this function.
Is there a way to loop on all the home assistant objects and check for the battery level without having to specify each entity? Also, I would like to do the same with the “update.” entity.

Thanks

Not that I have anything against NR (i run it myself), but isn’t this a lot easier to do this HA blueprint

There is even an option to exclude some sensors :wink:
Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

In Node Red use a trigger on the state and then set it to substring in the entity with battery as the substring.
This will then trigger whenever one of your batteries change state and you can pull the device from the msg from the trigger node.

@Wally, he want to incude all battery entities at once, so no need to add any new ones

He will get them all at once and also any future ones.
The substring in Node Red will make it so that there is only one flow that handle them all and as long as future battery entities will contain the same substring, then they will be included.

1 Like

@WallyR Ah…cool, never noticed that was an option. you see, never too old to learn something new :grin:

[{"id":"5b73be02e2966698","type":"trigger-state","z":"80c0b5702a819a5e","name":"","server":"37d68302.2aabcc","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":"battery","entityIdType":"substring","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"<","comparatorValueDatatype":"num","comparatorValue":"25"}],"customOutputs":[],"outputInitially":false,"stateType":"num","enableInput":false,"x":210,"y":120,"wires":[[],[]]},{"id":"37d68302.2aabcc","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

I still like the automation blueprint though :wink:

image

[{"id":"4fb0add3dc2db3b3","type":"inject","z":"120358abd7c22d30","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":788,"y":1440,"wires":[["9f6aa96b5b747b63"]]},{"id":"9f6aa96b5b747b63","type":"ha-get-entities","z":"120358abd7c22d30","name":"get batteries","server":"","version":1,"rules":[{"property":"attributes.device_class","logic":"is","value":"battery","valueType":"str"},{"property":"entity_id","logic":"starts_with","value":"sensor.","valueType":"str"},{"property":"state","logic":"lte","value":"$entity(\"number.battery_low_threshold\").state","valueType":"jsonata"}],"outputType":"array","outputEmptyResults":false,"outputLocationType":"msg","outputLocation":"payload","outputResultsCount":1,"x":970,"y":1440,"wires":[["c522080b5b1e6f99"]]},{"id":"c522080b5b1e6f99","type":"debug","z":"120358abd7c22d30","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1158,"y":1440,"wires":[]},{"id":"ee654dd8aeeeadd3","type":"ha-number","z":"120358abd7c22d30","name":"battery low threshold","version":1,"debugenabled":false,"inputs":0,"outputs":1,"entityConfig":"64f549bed83382a2","mode":"listen","value":"payload","valueType":"msg","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"value"},{"property":"previousValue","propertyType":"msg","value":"","valueType":"previousValue"}],"x":820,"y":1392,"wires":[[]]},{"id":"64f549bed83382a2","type":"ha-entity-config","server":"2dad33ee.42bf5c","deviceConfig":"","name":"battery low threshold","version":"6","entityType":"number","haConfig":[{"property":"name","value":"battery low threshold"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"%"},{"property":"min_value","value":0},{"property":"max_value","value":100},{"property":"step_value","value":1},{"property":"mode","value":"auto"}],"resend":false,"debugEnabled":false}]

Interesting. I’ve never used blueprint and I’m so entrenched with node-red that it’s too late for me to switch to something else.

Personally I’d do this in a combo of HA and NR - create a template sensor in HA which has the count of low batteries as the state, and the list of them as an attribute. Then an “event state” node in NR which fires when the count is over zero. It joins all the friendly names and sends you an email. Or trigger it daily like you’re doing if you don’t want to know the number has gone from 1 → 2 (for example).

Making it a sensor is much more visual, and then NR just acts on the sensor value.

Thanks to you and aceindy for the help. This worked for me with some modifications.