Examples for using the new Get Entities node

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:

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"]]}]

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?

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

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.

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

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

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;

That’s awesome, the function works!

Thanks!

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.

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

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

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"]]}]
4 Likes

Thanks, I’ll give it a try!

On example two, I’m trying to use all of the lights in a specific group (group.kitchen), and it seems to identify those 4 entities, but the next node turns on all lights. I was only able to make the service call work with the split output.

hi. i am having the same problem.
i am trying to send a message to my telegram notification but am not able to do so. can you please guide me for the same. i am trying to use the Telegram_bot node in node-red for the same. attaching the snapshots.
@Kermit can you plz guide on this?
i am trying to retrieve the switches which are on currently and pass the message to my Telegram notification.
request your help.!

Hi,

I am trying to do something which I thought should be simple enough but I am running into issues I cannot explain…
I am trying to pull a list of entities whose state hasn’t been updated in say 1h… (ie timeSinceChangedMs > 3600000)

For some reason, this does not return any entities… Am I missing something?

Note, I was able to remove the 2nd condition then split my array using a split node, feed all the messages into a “Switch” node to filter out and then join them again but this feels convoluted and I would like to understand how this node is supposed to work…

Any help would be appreciated…

Guillaume

timeSinceChangedMs doesn’t exist in this context only the text timestamps from Home Assistant.

You can use a JSONata expression to convert them to a timestamp and check that way.

$millis() - $toMillis($entity().last_changed) > 3600000

image

[{"id":"da45ecf3.23b07","type":"ha-get-entities","z":"ffbd7f06.4a014","name":"","rules":[{"property":"entity_id","logic":"starts_with","value":"sensor.room","valueType":"str"},{"property":"ANY TEXT NEEDS TO BE HERE","logic":"jsonata","value":"$millis() - $toMillis($entity().last_changed) > 3600000","valueType":"jsonata"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":320,"y":1248,"wires":[["4a966b2b.ce1dd4"]]},{"id":"225bd07a.1d69b","type":"inject","z":"ffbd7f06.4a014","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1248,"wires":[["da45ecf3.23b07"]]},{"id":"4a966b2b.ce1dd4","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":1248,"wires":[]}]

Found a bug, while making this example, that requires the property field not to be empty but the example should still work.

edit: I’ll see about adding in timeSinceChangedMs as a filterable property

1 Like