Home Assistant Community

Examples for using the new Get Entities node

#21

Is it possible to use this node to get all entities currently in HA? If so, how? I tried things like removing all properties, as well as setting property entity_id is not "" (with string selected), and entity id is .* with RegEx selected, all without luck.

Of course, I’m brand new to Node Red, so I may just not be doing it right :slight_smile:

#22

regex with .* worked for me, test it out and let me know.

[{"id":"30d7a4ea.4b0d5c","type":"ha-get-entities","z":"5eb3594f.d294b8","server":"ef067c6f.620e6","name":"","rules":[{"property":"entity_id","logic":"is","value":".*","valueType":"re"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":352,"y":384,"wires":[["ec642993.2a7988"]]},{"id":"1538f157.28693f","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":188,"y":384,"wires":[["30d7a4ea.4b0d5c"]]},{"id":"ec642993.2a7988","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":534,"y":384,"wires":[]}]

edit:

All current states are also available in the global context.

[{"id":"1538f157.28693f","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":188,"y":384,"wires":[["61f648dd.48c528"]]},{"id":"ec642993.2a7988","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":502,"y":384,"wires":[]},{"id":"61f648dd.48c528","type":"function","z":"5eb3594f.d294b8","name":"","func":"msg.payload = global.get('homeassistant').homeAssistant.states;\nreturn msg;","outputs":1,"noerr":0,"x":338,"y":384,"wires":[["ec642993.2a7988"]]}]
#23

Aha!

This was a “I don’t really know NodeRed yet” failure. I failed to use an inject node, just assumed that the get entities node could be a starting place. I don’t see anywhere in the info panel for get entites say what the input should be, am I missing it, or is that assumed?

#24

It’s doesn’t take any input variables other than just being “triggered”, not really sure the term that should be used.

#25

Interesting. The other one I was poking at, the “Events: all” module, doesn’t have any inputs, it just runs, so I was kind of expecting the same. I’ll know for next time.

#26

Thanx, my problem was that one cannot use this string as is in a template. I had to convert it back to an object, so i could use it in a notify node.

1 Like
#27

Does anyone know how to get the entity_id from an array of entities with the highest timeSinceChangedMs?

#28

Here’s a quick and dirty solution but does have at least one problem. If two entities have the same timeSinceChangedMs it will use the first one in the array.

const index = msg.payload.reduce((oldestIndex, current, currentIndex, array) => current.timeSinceChangedMs > array[oldestIndex].timeSinceChangedMs ? currentIndex : oldestIndex, 0);
msg.payload = msg.payload[index].entity_id;

return msg;

edit: different version

msg.payload = msg.payload.reduce((oldest, current) => current.timeSinceChangedMs > oldest.timeSinceChangedMs ? current : oldest).entity_id;

return msg;
#29

That’s awesome, the function works!

Thanks!

#30

Is there some way to detect when there aren’t any results? It seems that with no entities the message is not sent to the output.

#31

If you’re using the Output Type Array use the checkbox Send Empty Results. The Count Output Type will send a 0 when there are no results. The other two outputs stop the flow when there is no output.

1 Like
#32

Thanks for the clarification. I’ve set up a workaround setting a default payload, then using stoptimer
and switch if no new message is received:
image

#33

Here’s a possible solution that may work that doesn’t rely on a timer.

[{"id":"ba35cbb2.442198","type":"ha-get-entities","z":"5eb3594f.d294b8","name":"","rules":[{"property":"","logic":"is","value":"","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":358,"y":992,"wires":[["ece746d9.05fe48"]]},{"id":"3aabbfe3.42285","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":992,"wires":[["ba35cbb2.442198"]]},{"id":"ece746d9.05fe48","type":"switch","z":"5eb3594f.d294b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"empty"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":504,"y":992,"wires":[["f03d0c54.d0fec"],["8509e2bc.09bdb"]]},{"id":"3429abc5.964a14","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1052,"y":992,"wires":[]},{"id":"8509e2bc.09bdb","type":"split","z":"5eb3594f.d294b8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":632,"y":1040,"wires":[["ab3e8434.91ed68"]]},{"id":"ab3e8434.91ed68","type":"template","z":"5eb3594f.d294b8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":770,"y":1040,"wires":[["716378bf.36ae88"]]},{"id":"716378bf.36ae88","type":"join","z":"5eb3594f.d294b8","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":904,"y":1040,"wires":[["3429abc5.964a14"]]},{"id":"f03d0c54.d0fec","type":"change","z":"5eb3594f.d294b8","name":"Default Payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"no results","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":662,"y":992,"wires":[["3429abc5.964a14"]]}]
2 Likes
#34

Thanks, I’ll give it a try!