Creating nodes (sensors) dynamically?

Hi all,

I am trying to build a basic monitoring for my Proxmox Host with Node-Red.
I am able to access the API from Proxmox and get a list of all available containers and their information...

These information will be returned as an Array (for each container) - and each Array does contain an Object with the different information related to the container.

[
  {
  "template":"",
  "netin":513218353,
  "maxmem":16000221184,
  "maxswap":16000221184,
  "disk":"2422423552",
  "maxdisk":"12615557120",
  "cpu":0.000143815233868653,
  "uptime":152019,
  "vmid":"103",
  "status":"running",
  "netout":405024201,
  "type":"lxc",
  "cpus":2,
  "name":"ngnix-server",
  "diskread":821694464,
  "swap":0,
  "pid":"3137",
  "diskwrite":173953024,
  "mem":51335168
},
{
  "vmid":"100",
   "status":"running",
   "maxdisk":"8388009984",
   "uptime":137495,
  "cpu":0.0208788031474906,
  "disk":"2650222592",
  "maxswap":16000221184,
  "maxmem":16000221184,
  "template":"",
  "netin":790458843,
  "mem":187113472,
  "pid":"950",
  "swap":0,
  "diskwrite":569401344,
  "diskread":687075328,
  "name":"samba-server",
  "cpus":2,
  "type":"lxc",
  "netout":638719271
}
# and more...
]

Since the API would allow to create or delete containers on proxmox, I can't be sure how many machines will be returned back - so, I don't know, how long the array would be.

Since I want to forward the information to another client (homeAssistant), I need to create sensors for each entity that I want to monitor.
In my case, I need an "sensor" at least for each container included in the message.

Now, I can do this "manually":

With this method, I could simply collect the information and will have each device available as a Sensor in HomeAssistant.

But: Is there a way to create the sensors dynamically?
something like that?


in this case, the sensor would work as a template - which will be created for each container that is within the return message...

Unless the HA team provided a node that permits this dynamic nature you might have to change the means of communication. Can you use MQTT to transmit information to HA?

Note: I have zero working knowledge of HA (same as most folk here) so you may have more luck asking on the HA forum.

Hi,
I've already tried asking in the HA forum... unfortunately, it seems that there isn't a way to generate this dynamically in HA...

But yes, I could use MQTT, too... or - I just create the sensors manually (for now)... in theory there shouldn't be a change in the amount of containers in a high frequency...

just struggling with splitting the return message and switch them to different outputs... isn't working as expected atm...

You can do this dynamically with Node-RED dashboard, but without HA.

But with HA, I am not sure if there is an easy way.
If you know the upper limit, then you can pre-define all the sensors and connect them to HA, and only send message to the sensor when you detect that the sensor exists from the array. If the sensor does not exist, you may send null message, or use a gate node to block the message.
This is not an elegant solution, but works if the upper limit number of sensors is not a big number.

thanks a lot!
I have now managed to get these things done... with creating the sensors manually...

But... one short further question:
Can someone pls. help me to convert the "uptime" attribute into (days, hours....)

{"state":"running","attributes":{"maxmem":16000221184,"netin":299918419,"template":"","maxswap":16000221184,"cpu":0.000211577817977571,"uptime":189062,"maxdisk":"52777418752","disk":"4223741952","status":"running","vmid":"101","netout":428287283,"type":"lxc","diskread":1995493376,"name":"emby-server","cpus":4,"mem":360140800,"diskwrite":28351791104,"swap":0,"pid":"2821"}}

"uptime":189062 ==> the output should be something like: 3d, 13h, 45m

so, the msg should be then:

{"state":"running","attributes":{"maxmem":16000221184,"netin":299918419,"template":"","maxswap":16000221184,"cpu":0.000211577817977571,"uptime":"3d 13h, 45m","maxdisk":"52777418752","disk":"4223741952","status":"running","vmid":"101","netout":428287283,"type":"lxc","diskread":1995493376,"name":"emby-server","cpus":4,"mem":360140800,"diskwrite":28351791104,"swap":0,"pid":"2821"}}

Thanks a lot and with best regards,
Christoph

Quick google search shows this;

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.