I have Portainer running on my NAS, 2 Raspberry Pi’s and 2 Debian VM’s and I was finding it hard to remember what containers were running on which servers - so I searched for a Portainer integration. I found one that seemed pretty robust but looked like it was only intended to interact with a single Portainer server co-existing with Home Assistant - and I think it was also marked as deprecated?
In any case, I needed something simpler, but that could work with multiple servers. All I really wanted was to see what containers were on each server and if they were running or stopped, but I didn’t have the first clue as to how to build a custom integration - so I reached out to GPT4 and explained what I wanted - and after 2.5 days of testing auto-generated code and going back and forth with GPT-4, it finally provided an init.py, a manifest.json and a sensor.py file that do what I need!
This may not be useful to anyone other than me - and I don’t have time to make it more robust or document it - but on the off chance that someone else wants to take it further, I’ll attach my files here.
This post isn’t so much a “look at the great component I created”, but more of a “Holy Crap! I used GPT-4 to build a working custom component without having to write a single line of code!!!”
I don’t need any credit or attribution for this. If it’s useful to you - take it. If not, ignore it.
But just know that it IS possible to get GPT-4 to create a working custom component if you have the patience to go back and forth with it for a day or two. I’ve also used ChatGPT3.5 to configure some Lovelace multi-entity cards for me since I was too lazy to look up the documentation for it.
When you’re trying to understand some of the Home Assistant documentation - that doesn’t have any examples - just paste it in to ChatGPT and ask it to generate some examples for you! I’ve only been doing it for a couple of days so far - but I already find it a huge time saver.
It’s configured by adding
sensor:
- platform: portainer
url: http://192.168.1.15:9000
name: nas15
username: !secret port_user
password: !secret port_pass
- platform: portainer
url: http://192.168.1.10:9000
name: pi10
username: !secret port_user
password: !secret port_pass
And it creates a sensor for the server itself, named sensor.portainer_server_[servername] and then a sensor for each container on the server, named sensor.portainer_[servername]_[containername]. Each sensor has several extra attributes such as how long it’s been running, what image it’s running, and the url to quickly get to that server.
I don’t see how to attach files to this post - and I don’t have a GitHub account - so I guess I’ll just paste the code in here. There are only 3 files required. Copy them into a folder named ‘portainer’ under your custom_components folder, restart Home Assistant and then add the settings into configuration.yaml - I’ve only tested it with HTTP. It may need some tweaking to work with HTTP portainer servers.
The code is available at https://github.com/tenly2000/portainer_listing