Spotify Media Player delay

Hello, everyone.

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?

3 Likes

Hi guys,

I was wondering the same thing as @Tophat17.
Did anyone found an issue ?
Thank you.

Regards,
Ripper2909

Unfortunately not. I’m still looking for an answer.

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.

Here’s the Integration

1 Like

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.

2 Likes

OMG thank you sooo much for this idea !
I just did the same in an automation, if it could help anyone :slight_smile:

alias: '[OK] Spotify Refresher'
description: ''
trigger:
  - platform: state
    entity_id: media_player.spotify_ripper2909
condition: []
action:
  - repeat:
      while:
        - condition: state
          entity_id: media_player.spotify_ripper2909
          state: playing
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
        - service: homeassistant.update_entity
          target:
            entity_id: media_player.spotify_ripper2909
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
mode: single
25 Likes

Huh, didn’t think about using an automation. That’s cool though! I thought it was hard-coded in the integration!

Thanks, Ripper2909! Your code works flawlessly for keeping Spotify data current!

1 Like

Thanks, Rip!

On the money my friend. Was scratching my head and came across your solutions. Took less than a minute!

~TJ

Still working like a charm in 2022, thanks!

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.

In case you may want to do this in node-red: Refreshing the Spotify Integration Attributes - #2 by tux1984

still works like a charm in late 2023. You are legend.

You, sir, just made my day.

Wow, impressive, thank you so much!!!

Did the same for paused (got a Aqara cube. When i turn it from side 0 to 2, it pauses. And again, it continues. God i love HA)

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.

Here is my code for 24/7:

alias: Spotify Refresher
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - repeat:
      while: []
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
        - service: homeassistant.update_entity
          target:
            entity_id: media_player.spotify_name
          data: {}
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
mode: single

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.

Can I get ban from using it like this?

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.

alias: "Spotify Refresher"
description: ""
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: media_player
      service: media_play
      service_data:
        entity_id: media_player.spotify_yourspotifyentity
    id: starting
  - platform: state
    entity_id:
      - media_player.spotify_yourspotifyentity
    id: playing
    to: null
    from: null
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - starting
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 500
          - service: homeassistant.update_entity
            target:
              entity_id: media_player.spotify_yourspotifyentity
            data: {}
      - conditions:
          - condition: trigger
            id:
              - playing
        sequence:
          - repeat:
              sequence:
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
                - service: homeassistant.update_entity
                  metadata: {}
                  data: {}
                  target:
                    entity_id: media_player.spotify_yourspotifyentity
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
              while:
                - condition: state
                  entity_id: media_player.spotify_yourspotifyentity
                  state: playing
mode: queued