Single Topic MQTT RGB Light (not JSON)

I have a few rgb lights that respond to a single MQTT topic each & I can’t get them to work correctly with HA. This is the most simple one, the others support more than just the “rgb:” prefix and can flash, fade, etc, etc with the right command (but that will come later - small steps here).

Using this config I can get the light to turn on & off, but can’t get it to set color for more than a fraction of a second. It seems that each time I select the color on the color wheel, HA pushes the “on” command a fraction of a second later.

I can monitor the MQTT server directly and can publish directly to the MQTT server and that all works fine, it’s just that I’m having issues getting HA to send the correct commands.

i.e. Full White = publish “rgb:255,255,255”, Full Red = publish “rgb:255,0,0”, Off = publish “rgb:0,0,0”

mqtt:
  light:
    - name: "Bedroom Glow"
      command_topic: "bedroom/glow"
      state_topic: "bedroom/glow"
      payload_on: "rgb:255,255,255"
      payload_off: "rgb:0,0,0"
      rgb_state_topic: "bedroom/glow"
      rgb_value_template: "{{ value[4:] }}"
      rgb_command_topic: "bedroom/glow"
      rgb_command_template: "rgb:{{red}},{{green}},{{blue}}"
      unique_id: "bedroom_glow"

Probably the quickest way to get it working would be to use a light template:

That way you can configure a dedicated action for each UI action i.e. send a custom MQTT message in each case, depending on the complexity of the protocol you might need to track state (using input helpers). If the state of the light can be changed outside HA, you might also need additional automations to catch MQTT messages and update the state in HA,

It’s possible that there is a simpler solution, however specifics of what hardware and MQTT server you are using would be needed to provide useful advice.

This seems to (mostly) work… I can turn on and off the light, get the on/off status - only issue is RGB.. When I select a color on the color wheel, it runs the script, but the script doesn’t get the variables it should with the color) and the wheel doesn’t show the color selected.

EDIT: I’m pretty sure I’m missing something in the code below to send the variables to the script, I just don’t know what.

configuration.yaml

mqtt:
  sensor:
    # Bedroom Glow
    - name: "Bedroom Glow"
      state_topic: "bedroom/glow"
      value_template: "{{ value[4:] }}"
      unique_id: "bedroom_glow"

template:
  - light:
      - name: "Bedroom Glow"
        state: "{{ states('sensor.bedroom_glow') != '0,0,0' }}"
        turn_on:
          action: script.bedroom_glow_on
        turn_off:
          action: script.bedroom_glow_off
        rgb: sensor.bedroom_glow
        set_rgb:
          action: script.bedroom_glow_rgb
        unique_id: "bedroom_glow_helper"

Take a look at the “Light Considerations” section of the previously linked page:

TL;DR - There are multiple ways of setting color, both in Home Assistant and on hardware.
Its useful to know that as it can explain unusual behavior.

However most likely you need to use the variable rgb to extract the color you need to pass to your hardware.

More progress… The color wheel now shows the correct color on it (i.e. reads the MQTT RGB values and displays it).

I just have to figure out how to pass the RGB values from when you click on the color wheel to the script (and have the script accept them & use them).

template:
  - light:
      - name: "Bedroom Glow"
        state: "{{ states('sensor.bedroom_glow') != '0,0,0' }}"
        turn_on:
          action: script.bedroom_glow_on
        turn_off:
          action: script.bedroom_glow_off
        rgb: "{{ states('sensor.bedroom_glow') }}"
        set_rgb:
          action: script.bedroom_glow_rgb
        unique_id: "bedroom_glow_helper"

Victory is mine!

I was apparently over-complicating most of this

template:
  - light:
      - name: "Bedroom Glow"
        state: "{{ states('sensor.bedroom_glow') != '0,0,0' }}"
        turn_on:
          action: script.bedroom_glow_on
        turn_off:
          action: script.bedroom_glow_off
        rgb: "{{ states('sensor.bedroom_glow') }}"
        set_rgb:
          action: script.bedroom_glow_rgb
          data:
            rgb_color: "{{ rgb }}"
        unique_id: "bedroom_glow_helper"

The script then references {{ rgb_color }} in the MQTT publish and it populates in the format I already needed.

Special thanks to @dtrott for the help (if I can figure out how to give him an award here, I will).