Spotify Media Player delay

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
27 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
1 Like

@ShadowFist @Osher @Jerry_D @leonkunst88 @BYOB @aquarist684

I have developed a new SpotifyPlus Home Assistant custom component integration that expands the built-in Spotify capabilities. It’s a simple install to the “custom_components” directory via a HACS UI custom repository installer.

I also developed the underlying SpotifyWebApiPython library that interfaces with the Spotify Web API.

With the release of v1.0.11, player status changes are detected in near real-time IF changes are made via Home Assistant (e.g. UI, service call, automation, etc).

This was accomplished by changing the integration SCAN_INTERVAL from 30 seconds to 1 second, but also adding logic to the update method to only call the underlying Spotify Web API at selected intervals:

  • call every 30 seconds if no user or track change activity.
  • call if a player command is issued: set_volume_level, media_play, media_pause, media_previous_track, media_next_track, media_seek, play_media, select_source, set_shuffle, and set_repeat.
  • call every second for 5 seconds after a player command is issued. This allows for extra time it may take for the Spotify Connect Player to update it’s status after the command is processed.
  • call if the nowplaying track position has less than 5 seconds remaining. This is accomplished by subtracting the nowplaying DurationMS value (e.g. length of the track) from the nowPlaying track ProgressMS (e.g. current position of the track) value. This logic adds a few more calls to the Spotify Web API, but not many. It’s not perfect, but the acceptable trade-off is near real-time updates of Spotify Connect Player status.

Note that Spotify Connect Player changes are NOT detected in near real-time if controlling the Spotify Connect Player from another non-HA service (e.g. Spotify Web App, Spotify Mobile App, etc). In this case, changes will be detected within 30 seconds by the HA state machine. For example, if you advance to the next track from the Spotify Web player, it will not show the change in HA for up to 30 seconds. This is a limitation of the Spotify Web API, in that Spotify does not offer a websockets API that informs clients of Spotify Connect Player status and relies on client polling.

Thought you would like to know, and hope it helps!

More Information Links

1 Like

Hi @ShadowFist

I get an error with your code for some reason it does not work.
Care to asssit?

Thank you

From the log:
Logger: custom_components.pyscript.file.spot2
Source: custom_components/pyscript/global_ctx.py:335
integration: Pyscript Python scripting (documentation, issues)
First occurred: 11:55:35 (1 occurrences)
Last logged: 11:55:35

Exception in </config/pyscript/spot2.py> line 3: trigger: ^ SyntaxError: invalid syntax (spot2.py, line 3)

From the error, it looks like you’re using the code as a python script instead of a regular automation.

If that’s the case, then my automation won’t work because scripts don’t have triggers.

1 Like

I have an issue when trying to modify, pause or stop music whilst spotify is casting on my Sonos speaker

Failed to call service media_player/media_pause. Restricted device

If playing on my computer speaker all works fine.

Please advise?