I installed the Spotify component on my HA in order to use the media controller on the front end UI. Everything works like a charm, and I’m able to play, pause and skip tracks; however, I am facing one issue. It seems to take HA 20-30 seconds after the song changes before HA updates the current song on the UI.
Does anyone know if it is possible to increase the HA to Spotify poll rate in order to decrease the time it takes for the UI element to update?
I made a custom integration that is the original but just changed the scan interval in the python code from 30 seconds to 1 second. I don’t know how stable it is long term. I also don’t know how Spotify would like that many requests that frequently and I assume if there’s a lot of requests it stops accepting them and could possibly not be compliant with their terms of service. I assume that’s why there’s lag in the regular integration.
I’ve been trying to fix this for the longest time (well, it wasn’t ever really a priority, but it was really annoying) and gave up trying to do it properly, so I ended up just making a node-red loop to update the entity every second if spotify is playing.
The loop is triggered by spotify going into “playing” state, and keeps running as long as spotify stays in that state, updating the spotify entity and checking the state once every second.
I can think of at least a dozen ways this can break, but it works for the time being, and most of my automations are hacked together and barely working so it doesn’t bother me.
This was the final piece of automation I needed to fully implement an ESPHome LCD displaying my current Spotify media and control it with an APDS-9960 gesture sensor, not only this solved that annoying delay but now data on the LCD updates instantaneously, making it so much user friendly. I cannot thank you enough for this idea.
Thanks for this, I was unaware of the update_entity service. I came here looking for a way to change the polling interval of the Spotify integration and found this solution. But I am trying to move away from polling wherever I can, and I ended up with a different solution: I have an Android TV box hooked up to a monitor that I use as a media player and dashboard. I have the HA companion app installed, and the media player widget for the Spotify integration on my home screen (the widget is the main reason I needed to reduce the lag). I am also a big fan of Tasker, and Tasker has a ‘Music Track Changed’ event. I use that event to trigger a webhook call to HA, which runs the update_entity service whenever that webhook is received. Works like a charm, without extra polling! The ‘Music Track Changed’ event comes with a lot of built-in variables with info about the current track, queued tracks and player state which you can use for automations on the local machine (Tasker), or send back to HA as the webhook payload. I also used the Tasker AutoInput UI Action event to detect clicks on the album art portion of the widget (it has a text element ‘Media Playing Preview Image’ which you can use to filter clicks) to open the Spotify app.
Thanks! It’s work perfectly (in 2024).
I actually made some changes so the scan will scan 24/7 and not just when playing.
That’s because when I start playing music, it still take 20-30 seconds to refresh for the first time.
Be careful you don’t get rate limited by the Spotify API. Running that automation constantly twice a second means that it’ll call the API 172,800 times a day!
That’s a surefire way to get yourself (and others, if Spotify decides HA is a security threat) in trouble.
Officially, no, so far Spotify’s own documentation doesn’t state that you’ll get banned. You’ll just get constant 429 errors (twice a second) if you’ve made too many requests in the past 30s.
Depending on how quickly you realise this happened, your logs would be swamped with errors eg. If it takes you just an hour to realise, that’s 3600 entries in your log eating up your disk space.
The wider implications for the rest of the community go beyond your errors though. If Spotify determines that HA is making too many requests, they have ways to restrict access, or worse, get lawyers involved to get the integration removed. It’s happened a couple of times with other integrations in the past year - look it up.
All this because you couldn’t wait 20-30s to see updates for your first song.
Here. this should work. It’ll update Spotify ONCE when starting, as well as every second while it’s playing.