Create "dynamic" Sensors for HomeAssistant in NodeRed?

Hi all,

I am using the NodeRed addon for HomeAssistant for different monitoring reasons - which is working well so far.

right now, I am trying to create a new Monitoring flow in NodeRed, which should monitor some information from my Proxmox Server.

right now, I am at a point, where I can call the API and get some informations back…
for example, all available Containers, with their VMID, Name, Uptime, etc.

The Information will be provided in an array with X objects (depending on how many containers are running).
Each Array does have the corresponding information for the LXC.

[
  {
    "maxmem":16000221184,
    "template":"",
    "netin":747361494,
    "maxswap":16000221184,
    "maxdisk":"8388009984",
    "uptime":129398,
    "cpu":0.0210235609050815,
    "disk":"2650095616",
    "vmid":"100",
    "status":"running",
    "netout":608623129,
    "type":"lxc",
    "diskread":687075328,
    "name":"samba-server",
    "cpus":2,
    "mem":187125760,
    "swap":0,
    "pid":"950",
    "diskwrite":545361920
  },
  {
    "netout":363588777,
    "type":"lxc",
    "cpus":4,
    "name":"emby-server",
    "diskread":1995395072,
    "swap":0,
    "pid":"2821",
    "diskwrite":14869811200,
    "mem":354201600,
# and so on...
  }
]

I have also managed to split each array into a single message - which does return X messages (according to the number of elements in the array…)

But is it possible to create a “dynamic” sensor for each array?

for example, I would create sensors “Name”, “VM-ID”, “Uptime” etc.
And NodeRed will create one of these Sensors for each container available?

Since I could create new Containers - or delete one through the API, I don’t want to add a new Node every time when something might has changed on the server.

So… long story short:
Do I need to create sensors for each Container manually, or is there a way to create a “template” which will be applied for each container available?

I haven’t been able to set the name of an entity dynamically and I don’t think it’s possible.
When you deploy the entity appears, so it’s probably based on the deployment.

But you can have dynamic state and attributes.

hm… but with dynamic states and attributes, it would not allow me to create sensors in HA for each container, or?

What WOULD work is a sensor which does have all information available as attribute.
example:

Sensor: LXC-101
Attributes:
Status: Running
Uptime: 123548sec
ID: 101
and so on…

But still, I would need a method that would create a sensor for each available lcx container.
Or is there a way to do this within HA?

I don’t know what a container is or what lcx is.

But this:

is possible.
I don’t think there is a way to dynamically name entities in HA either.

Here is how to set dynamic attributes:
Custom Component for node-red-contrib-home-assistant-websocket - Third party integrations / Node-RED - Home Assistant Community

Thanks :slight_smile:

First, LXC / Container:
Proxmox is a virtualization system like VM-Ware or similar.
It does allow to setup different virtualized Computers in a host system.

For example, on my Host there are different containers (lxc) running - one for my emby Media Server,
one for nginX Reverse Proxy, and so on.

While such a container has some improvements against a complete virtualized system, Proxmox does also support VMs.
my HomeAssistant is running in such a VM… for example.

Right now, it seems that I need to add each container manually… then, the monitoring would work.
But then, I always need to change something, if I change something in the HostSystem on Proxmox (creating a new machine, deleting one… etc.)
That’s something I wanted to avoid … :slight_smile:

Well…
You could just create 10 entity nodes and have a switch node separate the messages to each of the nodes.
As long as you have less than 10 containers then it will work, and you will have empty entites.
But yes. I believe you need to do this manually.
I think you need to go down to python level if you want to make something dynamic

1 Like

yes, thanks so far :slight_smile:

I’ll try it - and maybe - the guys in the NodeRed Forum do have some ideas :slight_smile:

I’m quite sure this is a HA question

Hi,

can you show me this as an example in your node red flow?

I think, I am now a bit lost - and confused … :smiley:

I am still quite new to the NodeRed stuff and haven’t figured out how to split my return message in a way that would work as I do expect…

Here is one way of doing it:

[{"id":"966a894f3fee5069","type":"function","z":"4eb25acf17389316","name":"","func":"var arr = [];\nmsg.payload.forEach(function (value,index) {\n    msg.index = index;\n    msg.payload ={\"state\": value.type,\n        \"attributes\":value\n    };\n    node.send(msg);\n\n});\n\n//return [msg.payload = {arr[0]}, msg.payload = arr[1],msg.payload = arr[2],msg[3],msg[4],msg[5],msg[6],msg[7],msg[8],msg[9]];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":383.00000762939453,"y":421.0000057220459,"wires":[["13d247e48c2c399e"]]},{"id":"c247e79131f6c45e","type":"function","z":"4eb25acf17389316","name":"","func":"msg.payload = [\n  {\n    \"maxmem\":16000221184,\n    \"template\":\"\",\n    \"netin\":747361494,\n    \"maxswap\":16000221184,\n    \"maxdisk\":\"8388009984\",\n    \"uptime\":129398,\n    \"cpu\":0.0210235609050815,\n    \"disk\":\"2650095616\",\n    \"vmid\":\"100\",\n    \"status\":\"running\",\n    \"netout\":608623129,\n    \"type\":\"lxc\",\n    \"diskread\":687075328,\n    \"name\":\"samba-server\",\n    \"cpus\":2,\n    \"mem\":187125760,\n    \"swap\":0,\n    \"pid\":\"950\",\n    \"diskwrite\":545361920\n  },\n  {\n    \"netout\":363588777,\n    \"type\":\"lxc\",\n    \"cpus\":4,\n    \"name\":\"emby-server\",\n    \"diskread\":1995395072,\n    \"swap\":0,\n    \"pid\":\"2821\",\n    \"diskwrite\":14869811200,\n    \"mem\":354201600,\n\n  }\n];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":223.00000762939453,"y":421.0000057220459,"wires":[["966a894f3fee5069"]]},{"id":"13d247e48c2c399e","type":"switch","z":"4eb25acf17389316","name":"","property":"index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"},{"t":"eq","v":"7","vt":"num"},{"t":"eq","v":"8","vt":"num"},{"t":"eq","v":"9","vt":"num"}],"checkall":"true","repair":false,"outputs":10,"x":513.0000076293945,"y":421.0000057220459,"wires":[["e67afa668f18dcca","191ba11331181de7"],["64291d8f9c2f9438","0528f3c263331f37"],[],[],[],[],[],[],[],[]]},{"id":"81535561a35ec217","type":"inject","z":"4eb25acf17389316","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":93.00000762939453,"y":421.0000057220459,"wires":[["c247e79131f6c45e"]]},{"id":"e67afa668f18dcca","type":"debug","z":"4eb25acf17389316","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":753.0000076293945,"y":301.0000057220459,"wires":[]},{"id":"64291d8f9c2f9438","type":"debug","z":"4eb25acf17389316","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":753.0000076293945,"y":401.0000057220459,"wires":[]},{"id":"0528f3c263331f37","type":"ha-entity","z":"4eb25acf17389316","name":"two","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"two"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"","stateType":"str","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":753.0000076293945,"y":441.0000057220459,"wires":[[]]},{"id":"191ba11331181de7","type":"ha-entity","z":"4eb25acf17389316","name":"one","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"one"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"","stateType":"str","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":753.0000076293945,"y":341.0000057220459,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The first function node is just to get your json in the message.
Copy the second function node and the switch and connect those to your sequence and entity nodes.

1 Like

Thanks :slight_smile:
That’s pretty much what I wanted - except, a small detail, which I still need to figure out - but probably, that requires a custom card for this type of “sensor”…

Something like this, where I can use an Attribute of the Sensor for the Name, and the other Attributes as information…

Do you know, if there’s already a card similar to this available?

grafik

There is probably better alternatives, but the one that I think of is picture elements card.
You can configure it almost as you want.

hm…
it seems that the HomeAssistant Contribution for NodeRed does have one major issue…

Finally, I was able to replace the attribute “name” in my messages with “friendly_name” - which I thought would be used by HA as attribute, like it is for all other sensors, like this for example.

grafik

So, this is my RodeNed Flow…

I am using your method to provide the values to each sensor, but before this, I am converting the content to a string, do a replacement of the “name” - and convert it back into a JSON object.
Finally, you can see, the “fiendly_name” attribute in the output.

Unfortunately,
The Integration does have one BIG issue:


Here, you can set the “name” of the sensor, which will be used as Friendly_Name in HA.
This is optional - and can be blank.

Unfortunately, if I try this method, the Sensor will be registered in HA but does not use the attribute friendly_name - instead, this attribute will be overwritten… :frowning:

The next thing I’ve tried was to assign the “friendly_name” additionally as attribute to the sensor like this:
grafik

So - the main issue seems to be, that the friendly name will be set to a random value when it is not set - and the attribute will not be taken.
Another issue is, that you can’t select msg.payload… to define the name of the sensor in HA…

Maybe, I can fix this within the homeassistant-node-red-contribution? :thinking:

How about setting the attribute friendly name in the function node?
Have you tried that?
I just tried in a debug node and it’s set at least.

I just added one line to add the friendly_name.
But you need some way to make that dynamic I guess or all will have the same name.

var arr = [];
msg.payload.forEach(function (value,index) {
    msg.index = index;
    msg.payload ={"state": value.type,
        "attributes":value
    };
    msg.payload.attributes.friendly_name = "test";
    node.send(msg);

});

The way you set your friendly name in the image is probably incorrect.
You try to set friendly name to something that does not exist. You point it to the attributes of the message, but I can’t see a friendly name key there.

Hi, thanks for the help.
As far as I got from the Node-Red Integration Developers, the Attribute friendly_name cannot be set dynamically.
The problem is, that the sensor needs to be registered when the flow is deployed.
In this case, the friendly_name will also be set.

Therefore, it cannot be set during the runtime when the flow will be executed.

It seems, that there is some more work required to update the attribute, but I think - I’ve found another way to workaround.

– finally, I will create multiple sensors out of the information I receive… With this, I can setup the UI in HomeAssistant in a more flexible way.
I think, that will do the work for me.

Mqtt maybe a better way to send the information to HA rather than using entity nodes.

1 Like