home-assistant.io/source/_components/light.mqtt_template.markdown
Alok Saboo 1aca7b08cf Misc fixes: e.g. -> e.g., and proper case for Home Assistant (#4942)
* e.g. to e.g., and proper case for Home Assistant

* Instructions how to -> Instructions on how to
2018-03-17 20:20:37 +01:00

9.6 KiB
Raw Blame History

layout title description date sidebar comments sharing footer logo ha_category ha_iot_class ha_release
page MQTT Template Light Instructions for how to setup MQTT Template lights within Home Assistant. 2016-11-06 21:16 true false true true mqtt.png Light depends 0.33

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
light:
  - platform: mqtt_template
    command_topic: "home/rgb1/set"
    command_on_template: "on"
    command_off_template: "off"

{% configuration %} name: description: The name of the light. required: false type: string default: MQTT Template Light effect_list: description: List of possible effects. required: false type: string list command_topic: description: The MQTT topic to publish commands to change the lights state. required: true type: string state_topic: description: The MQTT topic subscribed to receive state updates. required: false type: string command_on_template: description: "The template for on state changes. Available variables: state, brightness, red, green, blue, flash, transition and effect." required: true type: string command_off_template: description: "The template for off state changes. Available variables: state and transition." required: true type: string state_template: description: "Template to extract state from the state payload value." required: false type: string brightness_template: description: "Template to extract brightness from the state payload value." required: false type: string red_template: description: "Template to extract red color from the state payload value." required: false type: string green_template: description: "Template to extract green color from the state payload value." required: false type: string blue_template: description: "Template to extract blue color from the state payload value." required: false type: string color_temp_template: description: "Template to extract color temperature from the state payload value." required: false type: string effect_template: description: "Template to extract effect from the state payload value." required: false type: string white_value_template: description: "Template to extract white value from the state payload value." required: false type: string optimistic: description: Flag that defines if the light works in optimistic mode. required: false type: string default: "true if no state topic or state template is defined, else false." qos: description: The maximum QoS level of the state topic. required: false type: integer default: 0 availability_topic: description: The MQTT topic subscribed to receive availability (online/offline) updates. required: false type: string payload_available: description: The payload that represents the available state. required: false type: string default: online payload_not_available: description: The payload that represents the unavailable state. required: false type: string default: offline {% endconfiguration %}

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

{% linkable_title Comparison of light MQTT platforms %}

Function mqtt mqtt_json mqtt_template
Brightness
Color temperature
Effects
Flashing
RGB Color
Transitions
XY Color
White Value

{% linkable_title Examples %}

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

{% linkable_title 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
light:
  - platform: mqtt_template
    command_topic: "home/rgb1/set"
    state_topic: "home/rgb1/status"
    command_on_template: "{% raw %}on,{{ brightness|d }},{{ red|d }}-{{ green|d }}-{{ blue|d }}{% endraw %}"
    command_off_template: "off"
    state_template: "{% raw %}{{ value.split(',')[0] }}{% endraw %}"  # must return `on` or `off`
    brightness_template: "{% raw %}{{ value.split(',')[1] }}{% endraw %}"
    red_template: "{% raw %}{{ value.split(',')[2].split('-')[0] }}{% endraw %}"
    green_template: "{% raw %}{{ value.split(',')[2].split('-')[1] }}{% endraw %}"
    blue_template: "{% raw %}{{ value.split(',')[2].split('-')[2] }}{% endraw %}"

{% linkable_title 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
light:
  - platform: mqtt_template
    effect_list:
      - rainbow
      - colorloop
    command_topic: "home/rgb1/set"
    state_topic: "home/rgb1/status"
    command_on_template: >{% raw %}
      {"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 -%}
      }{% endraw %}
    command_off_template: '{"state": "off"}'
    state_template: '{% raw %}{{ value_json.state }}{% endraw %}'
    brightness_template: '{% raw %}{{ value_json.brightness }}{% endraw %}'
    red_template: '{% raw %}{{ value_json.color[0] }}{% endraw %}'
    green_template: '{% raw %}{{ value_json.color[1] }}{% endraw %}'
    blue_template: '{% raw %}{{ value_json.color[2] }}{% endraw %}'
    effect_template: '{% raw %}{{ value_json.effect }}{% endraw %}'

{% linkable_title No brightness or color support %}

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