MQTT-controlled priority-based SmartLight control with AppDaemon

Hey everyone,

Do you a smart bulb that you want to use to change colors for several different situations, but find it difficult to handle multiple events sharing the same bulb? Do you want your bulb to turn on when motion is detected, turn red when you are in a Zoom call, and go all disco when your garage door opens, but the bulb only shows the latest event? This app may help.

I started writing this SmartBulb controller app quite a while ago, but never finished it. Maybe some day I’ll finish it, but for now since it basically works I figured I might as well share it in case someone else may find it useful.

Consider this a ‘beta.’ The code is really messy and incomplete. I may never get around to finishing it. But maybe you’ll still find a use for it.

This app allows you to control a HomeAssistant light entity via MQTT. In your apps.yaml you specify a light bulb and an MQTT topic to watch, something like:

  module: notification_light_beta
  class: NotificationLight
  bulb_entity: light.test_light
  topic: myhome/lights/test_light/notifications/#

Once the app is watching a topic, you can post your events to this topic and give each event a priority.
For example, if you posted these topics

  topic: myhome/lights/test_light/notifications/TestRedLight
  payload: { "rgb": [255, 0, 0], "priority": 100 }

  topic: myhome/lights/test_light/notifications/TestLightOn
  payload: {"temperature": 2700, "brightness": 50, "priority": 101 }

  topic: myhome/lights/test_light/notifications/TestSequence
  payload: {
           "sequence": [
               { "rgb": [255,0,0], "duration": 1, "fade_in": 0.5 },
               { "rgb": [0,255,0], "duration": 2, "fade_in": 1 },
               { "rgb": [0,0,255], "duration": 1, "fade_in": 0 }
            "loops": -1,
            "priority": 102

then the highest priority topic, “TestSequence,” will run on the bulb. The light will loop fading between red, green, and blue forever, until either a higher-priority topic is published, or “TestSequence” is removed from MQTT by posting an empty payload with ‘retain’ set.

Once “TestSequence” is removed, the next highest priority, “TestLightOn”, will take over the light, and so on. If you have multiple events that you want to display on the bulb, you simply publish them to MQTT with a priority, and the app will handle deciding what is shown.

For a real-world example, I have a SmartBulb that sets to full brightness 2700k when a light switch is ‘on,’ is a blue ‘night light’ when motion is detected after sunset, and is red when I am in a Zoom call. Without this app it would be a nightmare to figure out the logic for this. With this app, I just post “InMeeting”, “LightOn”, and “NightLight” topics to MQTT (with priorities descending in that order). Now, if I’m in a meeting it is Red, no matter what. NightLight will only show through if I am not in a meeting and the light switch is off. Easy!

Anyway, just sharing what I have. Don’t expect it to be perfect, but the basic use cases seem to work pretty well.