MQTT Template Light

The mqtt_template light platform lets you control a MQTT-enabled light that receive commands on a command topic and optionally sends status update on a state topic. It is format-agnostic so you can use any data format you want (i.e. string, JSON), just configure it with templating.

This platform supports on/off, brightness, RGB colors, XY colors, color temperature, transitions, short/long flashing, effects and white values.

In an ideal scenario, the MQTT device will have a state topic to publish state changes. If these messages are published with the RETAIN flag, the MQTT light will receive an instant state update after subscription and will start with the correct state. Otherwise, the initial state of the light will be off.

When a state topic is not available, the light will work in optimistic mode. In this mode, the light will immediately change state after every command. Otherwise, the light will wait for state confirmation from the device (message from state_topic).

Optimistic mode can be forced, even if state topic is available. Try enabling it if the light is operating incorrectly.

# Example configuration.yaml entry
  - platform: mqtt_template
    command_topic: "home/rgb1/set"
    command_on_template: "on"
    command_off_template: "off"

Configuration Variables


(string)(Optional)The name of the light.

Default value: MQTT Template Light


(string list)(Optional)List of possible effects.


(string)(Required)The MQTT topic to publish commands to change the light’s state.


(string)(Optional)The MQTT topic subscribed to receive state updates.


(string)(Required)The template for on state changes. Available variables: state, brightness, red, green, blue, white_value, flash, transition and effect.


(string)(Required)The template for off state changes. Available variables: state and transition.


(string)(Optional)Template to extract state from the state payload value.


(string)(Optional)Template to extract brightness from the state payload value.


(string)(Optional)Template to extract red color from the state payload value.


(string)(Optional)Template to extract green color from the state payload value.


(string)(Optional)Template to extract blue color from the state payload value.


(string)(Optional)Template to extract color temperature from the state payload value.


(string)(Optional)Template to extract effect from the state payload value.


(string)(Optional)Template to extract white value from the state payload value.


(string)(Optional)Flag that defines if the light works in optimistic mode.

Default value: true if no state topic or state template is defined, else false.


(integer)(Optional)The maximum QoS level of the state topic.

Default value: 0


(string)(Optional)The MQTT topic subscribed to receive availability (online/offline) updates.


(string)(Optional)The payload that represents the available state.

Default value: online


(string)(Optional)The payload that represents the unavailable state.

Default value: offline

Make sure that your topics match exact. some-topic/ and some-topic are different topics.

Comparison of light MQTT platforms

Function mqtt mqtt_json mqtt_template
Color temperature
RGB Color
XY Color
HS Color
White Value


In this section you find some real-life examples of how to use this light.

Simple string payload

For a simple string payload with the format state,brightness,r-g-b (e.g., on,255,255-255-255), add the following to your configuration.yaml file:

# Example configuration.yaml entry
  - platform: mqtt_template
    command_topic: "home/rgb1/set"
    state_topic: "home/rgb1/status"
    command_on_template: "on,{{ brightness|d }},{{ red|d }}-{{ green|d }}-{{ blue|d }}"
    command_off_template: "off"
    state_template: "{{ value.split(',')[0] }}"  # must return `on` or `off`
    brightness_template: "{{ value.split(',')[1] }}"
    red_template: "{{ value.split(',')[2].split('-')[0] }}"
    green_template: "{{ value.split(',')[2].split('-')[1] }}"
    blue_template: "{{ value.split(',')[2].split('-')[2] }}"

JSON payload

For a JSON payload with the format {"state": "on", "brightness": 255, "color": [255, 255, 255], "effect": "rainbow"}, add the following to your configuration.yaml file:

# Example configuration.yaml entry
  - platform: mqtt_template
      - rainbow
      - colorloop
    command_topic: "home/rgb1/set"
    state_topic: "home/rgb1/status"
    command_on_template: >
      {"state": "on"
      {%- if brightness is defined -%}
      , "brightness": {{ brightness }}
      {%- endif -%}
      {%- if red is defined and green is defined and blue is defined -%}
      , "color": [{{ red }}, {{ green }}, {{ blue }}]
      {%- endif -%}
      {%- if effect is defined -%}
      , "effect": "{{ effect }}"
      {%- endif -%}
    command_off_template: '{"state": "off"}'
    state_template: '{{ value_json.state }}'
    brightness_template: '{{ value_json.brightness }}'
    red_template: '{{ value_json.color[0] }}'
    green_template: '{{ value_json.color[1] }}'
    blue_template: '{{ value_json.color[2] }}'
    effect_template: '{{ value_json.effect }}'

No brightness or color support

If you don’t want brightness, color or effect support, just omit the corresponding configuration sections.