I asked ChatGPT to create a basic Portainer Integration

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

    - platform: portainer
      name:  nas15
      username: !secret port_user
      password: !secret port_pass
    - platform: portainer
      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

1 Like

The displayed link is correct, but the link behind that is broken (it points to https://portainer_listing).

Thanks. I fixed it.

1 Like

I will be honest with you, I don´t have a Portainer to test your integration, but the tests I’ve done so far with ChatGPT (both 3.5 and 4) for automations in Home Assistant are not very impressive and in most of the cases are not even usable or don’t gets to the expected results.
The tool is really powerful for generating texts, etc., but I still not convinced about using it for automating HA.
But based on what you are sharing, it looks powerful on developing a Python based solution, right?

I did have to go back and forth with it many, many times - it gave me code, I tested it and then pasted the errors back into ChatGPT, at which point, it said “Oh yeah - sorry, I did that wrong - try this…” and every once in a while, I’d ask for a minor change and it attempted to change everything - even switching from username/password authentication to expecting an API Key. When that happened, I just pasted in the last “working” set of code and told it that it had gotten confused. It apologized and then gave me the proper updates. Honestly, there were a few times when I thought we’d never get to something that worked - but a little persistence, and we did get there.

It’s certainly not the be all and end all at the moment - but I don’t think it will take very long to get there. The differences between 3.5 and 4.0 are night and day for code generation and correspondence - but there’s that limit of 25 requests every 3 hours on 4.0 to work around - so I still use 3.5 for more mundane things and try to save my 4.0 requests for more complicated asks. For example, I recently had a CT scan and I gave GPT4 the doctors report and it did a great job of summarizing it in easy to understand words and phrases. Even with its current imperfections and foibles, it’s the most amazing thing I’ve ever seen - and just imagine what it will be like in 5 years!


I have written very advanced automations in Node-RED using GPT-4, namely with the function node. Stuff I wouldn’t be able to do in six months with extensive research, I achieved in 7 hours. It’s all about the input you give AND proper feedback you return with testing.

GPT-3.5 is useless indeed for this kind of work.

1 Like

Trying to get this setup. It’s exactally what I’ve been looking for.

I did notice you also need const.py in addition to the three files. That got me past my first issue.

Now I’m getting:
Logger: custom_components.portainer.sensor
Source: custom_components/portainer/sensor.py:163

Failed to authenticate with Portainer.

I’ve tried making another user on Portainer and putting the user/pass directly into configuration.yaml, but that had no effect. I’m assuming the user/pass is the same you’d use to log into Portainer’s web interface?

I also get a “Failed to get JWT” error, but I’m guessing that because I didn’t authenticate.