Refresh Button for Statistics For Strava Using Home Assistant + Node-RED + Portainer API #1328
lennon101
started this conversation in
Show and tell
Replies: 2 comments
-
|
Nice! I didn't know Portainer had an API that could do that! Will definitely copy your idea ! |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thanks for sharing. I´ll do that when i find time. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I wanted a simple way to manually trigger my
refresh.shscript inside the Statistics For Strava (SFS) Docker container. While I have setup a daily sync of the strava data, I've wanted a way to manually sync (or even automatically sync) so that I can get my new workouts into SFS as soon as I've finished. I'm already running a docker instance of Home Assistant (HA) and I wondered if I could create a HA button that I when pressed, would trigger a SFS sync.Here’s how I wired it all together using Node-RED and the Portainer API.
🧩 Setup Overview
All three services — Home Assistant, Node-RED, and Statistics For Strava — are running as containers managed by the same Portainer instance on the same machine.
This makes it easy to execute Docker commands via Portainer’s API without worrying about cross-host networking.
🟢 Step 1 – Home Assistant Button
In Node-RED, I installed the Home Assistant Pallet and then added a Button node to register a new button entity in Home Assistant via the Node-RED Integration.
This button appears in my dashboard as
button.refresh_statistics.When pressed, it simply sends an event into Node-RED. Simple!
🔄 Step 2 – Node-RED Flow
When the button event fires, Node-RED performs two quick API calls to Portainer:
Create a Docker Exec session in the
statistics-for-stravacontainer:You'll need to use a
functionnode in Node Red and you'll need to create an API Key in Portainer for Node Red. Then copy and paste the below into thefunction node(ensuring you update the api key details and the portainer ip addr details)Notes:
Start the Exec session:
Create another
function nodeand paste the below into it (ensuring you update the api key details and the portainer ip addr details as before)The Authorization header uses a Portainer Access Token created under My Account → Access Tokens.
⚙️ Step 3 – Integration Details
msg.payload.Idfrom the first response to start the exec session."Detach": trueensures Node-RED doesn’t hang while waiting for Portainer’s stream response.Wire each of these function nodes to http request nodes (as shown in the screnshot of the node flow above). Leave the fields in the http request nodes blank (they have been set using the function nodes). Here's a screenshot of what the http request node should look like:
🎉 Result
Now, pressing the “Refresh Strava Stats” button in Home Assistant immediately runs the
refresh.shscript inside the container — all through Portainer’s API, fully automated.The only annoying part here is the
http request nodefor thestartappears to timeout after 2mins. But fear not, if you use the below payload (as it is written above) then the timeout never occurs because it is running in detach mode.🧠 Lessons Learned
Detach: truewhen starting execs via Portainer — otherwise Node-RED times out.Beta Was this translation helpful? Give feedback.
All reactions