Using dominant color of Plex poster (currently playing item) to change color of RGB bulb


Since I’ve been asked how to do this in another topic, here’s a short explanation with necessary Python script attached (it is attached as .txt file, since .py files can’t be attached).

DominantImageColor.txt (1.1 KB)

General idea:

Change the color of RGB light bulb to a dominant color of a Plex poster for currently playing movie/TV show.

Required software and bindings:

  • Python 3 (with ColorThief library installed)
  • SSH enabled on both, OpenHAB server and Plex server
  • Exec Binding
  • Plex Binding
  • JSONPath Transformation

Python script explanation

Python script has 1 parameter (-i) which takes URL of currently playing Plex item poster as input. After running script, it returns JSON output, containing Hue, Saturation and Brightness of dominant poster color.

OpenHAB part of implementation

After item containing state of Plex player changes to “Playing”, URL of the Plex poster is used as Python script parameter, and the script result is used to change the color bulb item to a dominant poster color. I’m running Python script on the Plex server (Windows machine), since running it on the OpenHAB server (Raspberry Pi 3) was way too slow.

OpenHAB rule

rule "Plex Playing"
    Item PlexPlayerState changed to "Playing"
    var PlexPosterURL = ""						
    var PlexPosterURLEdited = ""
    var DominantImageColor = ""

    PlexPosterURL = PlexPlayerPosterURL.state.toString
    PlexPosterURLEdited = PlexPosterURL.replace(https, http)

    if (PlexPosterURLEdited != "") {
        DominantImageColor = executeCommandLine(Duration.ofSeconds(90), "ssh", "User@", "C:\\DominantImageColor\\", "-i", PlexPosterURLEdited)
        if ((transform("JSONPATH", "$.hue", DominantImageColor) !== NULL) && (transform("JSONPATH", "$.saturation", DominantImageColor) !== NULL) && (transform("JSONPATH", "$.brightness", DominantImageColor) !== NULL)) {								
            var DecimalType RGBWColorValueHue = new DecimalType(Float::parseFloat(transform("JSONPATH", "$.hue", DominantImageColor)).intValue)
            var PercentType RGBWColorValueSaturation = new PercentType(Float::parseFloat(transform("JSONPATH", "$.saturation", DominantImageColor)).intValue)
            var PercentType RGBWColorValueBrightness = new PercentType(Float::parseFloat(transform("JSONPATH", "$.brightness", DominantImageColor)).intValue)
            var HSBType RGBWBulbColor = new HSBType(RGBWColorValueHue, RGBWColorValueSaturation, RGBWColorValueBrightness)


I’m using this rule with Shelly Duo RGBW, so, using it with another color bulb might require some changes in this rule and/or Python script.

Best regards,

thx, very nice!