Syncing LED Lights to Playing Music

Last weekend I put a WiFi RGB LED strip on the back of my TV. I use my Chromecast on my TV to cast Spotify to pretty frequently for listening to music. One night my roommate said that it would be cool if the lights behind my TV changed color based on what album I was playing through Spotify. So, I started looking into ways to do this.

Thanks to AppDaemon and a Python library called Colorthief, I was able to come up with a solution. Below is the code for my AppDaemon app that changes the color of my LED strip based on my Chromecast’s current album art.

apps.yaml

music_lights:
  module: music_lights
  class: music_lights
  ha_url: !secret ha_url
  media_player: "media_player.adams_tv"
  photo_attribute: "entity_picture"
  light: "light.tv_leds"

music_lights.py

import appdaemon.appapi as appapi
import sys

if sys.version_info < (3, 0):
    from urllib2 import urlopen
else:
    from urllib.request import urlopen

import io

from colorthief import ColorThief
 
class music_lights(appapi.AppDaemon):
 
  def initialize(self): 
    self.listen_state(self.change_led_color, self.args["media_player"], attribute = self.args["photo_attribute"])

  def change_led_color(self, entity, attribute, old, new, kwargs):
    if new != old:
      rgb_color = self.get_color(self.args["ha_url"] + new)
      self.turn_on(self.args["light"], rgb_color=[rgb_color[0], rgb_color[1], rgb_color[2]])

  def get_color(self, url):
    fd = urlopen(url)
    f = io.BytesIO(fd.read())
    color_thief = ColorThief(f)
    return color_thief.get_color(quality=3)

Now whenever I’m casting music to my TV through Chromecast, my LEDs change with the album cover that is playing. The app is pretty generic so I could set this up with pretty much any other media player and light combo that I wanted (if the light is RGB and the media player has album art support).

If you’re looking into setting up something like this I suggest you check out the documentation for AppDaemon including @ReneTode’s AppDaemon For Beginner tutorial. Before looking into this I had no idea what AppDaemon was capable of with HA. I had only used it to setup HADashboard before.

I didn’t really have much experience with Python before this (although I am a full-time Javascript programmer). As far as AppDaemon goes, the documentation/guides are pretty helpful with installing it on your Pi and getting it configured with HA. It opens the door to do pretty much any automation that you can think of.

If anyone has suggestions for how to improve this or do it in a simpler way, please comment. My lights look super nice :sunglasses:

18 Likes

That’s really a smart and new way to do colour sync with a content (typically done with an HDMI grabber), thanks for sharing :+1:

1 Like

I just put the script for this up on GitHub in case anyone comes across this later.

2 Likes

Thank you for writing this up!
I was able to follow up getting AppDaemon setup, and use this to get my lights to sync to music. It’s EXACTLY what I needed.

A couple of notes I wanted to mention:
With the newer version of AppDaemon, the import line needs to change from

import appdaemon.appapi as appapi

to

import appdaemon.plugins.hass.hassapi as hass

as per part 1 of the AppDaemon Beginner’s Tutorial

Similarly, the argument appapi.AppDaemon needs to be changed to hass.Hass when defining class music_lights

1 Like

What lights did you get?

Glad you could get it set up. Thanks for the heads up, I’ll keep that in mind when I upgrade to the new version of AppDaemon!

I’m using a Yeelight Wifi LED Strip for this. I grabbed one while it was on sale just to try it out and I have to say, I was pleasantly surprised by the quality.

This is awesome, will definitely have to try it out. colorthief is a nice find

1 Like

This looks awesome!

How do i install the Python library Colorthief (am running hassio)
Would i need to connect to the AppDaemon plugin / docker and install the Python library from there?
Or do i put the Colorthief source files somewhere where music_lights.py can read them?

Thanks!

I’ve never run Hassio (I’m running Hassbian), but if there’s an AppDaemon plugin then I would think you would need to install ColorThief wherever AppDaemon runs. You shouldn’t need the source files for ColorThief, you can just install it with pip3.

1 Like

Has anyone had success getting ColorThief on Hassio?

2 Likes

Also, is it imperative that your TV be used as the media player in the apps.yaml? I don’t have my TV as an entity, but I do have my chromecast as one.

I cannot tell if the chromecast will work as the entity, becasue I’m pretty sure I’m stuck trying to get colorthief.

Would it be possible to share which packages you’re getting in your AppDaemon config?

Hey, I’m not sure about getting ColorThief to work on Hassio (one of the reasons I’m sticking to Hassbian for now).

As for the media player, what I’m using is actually a Chromecast as well. My TV has a “built-in” Chromecast, so my entity name for it is just adams_tv, when in reality, that’s just a Chromecast same as any other.

This should work with any entity that has a photo attribute of some kind. Hope that answers your question!

That helps. I used my chromecast entity so I should be ok there.

May I ask, how do you trigger this “on and off”? My chromecast stays on indefinitely so It would be scrolling screensaver art the whole time.

I know it’s a lot of questions but this sounds really amazing and I’d love to get it working. Do you spotify through HA, or just any regular method of casting it to the chromecast?

No problem.

In my experience, HA sort of “ignores” the Chromecast screensaver art (as in - the Chromecast screensaver art is not cached on the server or assigned to the photo attribute that I use, “entity_picture”), so, my lights only change color when I’m casting something. Unfortunately, that does include things like YouTube and other casted apps, but I rarely cast anything other than Spotify so I don’t mind it.

Usually I use Spotify Connect through the Spotify app to cast to my Chromecast.

I wonder if this would work using a tv that I mainly use as a 2nd source for sports or a digital photo frame that I have a Apple TV hooked up to that uses the cloud screen saver to display pictures of my family. It would be cool to get the Hue lights to react to the pictures being displayed.

I finally got around to setting this up and it’s working great for me, super cool!

2045946869069330403

I’m not sure if it’s my LED strip or the nature of the averaging of the album cover but everything is pretty primary red-green-blue. I will have to try it with other bulbs and since if the effect is more subtle.

Is it possible to create a new sensor in appdaemon? It would be great to have the color value returned as a sensor to be used in automations instead of sending command directly to one light. I could set up a dummy template light with MQTT and use that instead I guess.

It seems this could be easily adapted for a camera entity too, I will see if I can modify your code to do that. I have a webcam that gets a view of the sunset, would be interesting to sync that to lighting.

1 Like

You haven’t started playing AppDaemon yet. Do you want to use colorthief if you don’t use AppDaemon?

@astone Am a newbie to Home assistant can you help me how to set LED strips to sync with Music.

Any tutorial links /video would help.

I currently use Flux_LED component in Home assistant but as per my exploration that doesn’t have sync music option

Please help thanks in advance.

I just came across this thread.
@astone great idea and nice app!

I have made some changes on my side:

  • Faster color extraction (using Pillow directly). On my Raspberry Pi, it is about 8x faster!
  • Extract a color palette instead, that matches the number of lights added in the config
  • An optional entity state check condition

Also, @easwaran83, if you still need help for that, I’ve made a README wich explains what I did to set this up.

The code can be found here: https://github.com/ericmatte/HomeAutomation/tree/master/appdaemon

Cheers!

2 Likes