MQTT Template for Lights

I am at a real lose right now. I have setup a light bulb with ESPHome using MQTT instead of the API. I can get the light working using the JSON schema but I really need to be able to use the template schema. In doing so anytime I try to input the state_template it isn’t getting any information back from my MQTT and the switch in Home assistant just shuts right back off but the light will turn on.

My light code is as follows:

  - platform: mqtt
    schema: template
    name: 'Office Test'
    command_topic: 'LB_OfficeMainLight2/cmnd'
    state_topic: 'LB_OfficeMainLight2/stat'
    command_on_template: '{"state":"on"}'
    command_off_template: '{"state":"off"}'
    state_template: '{{ value_json.state }}'
#    brightness_template: '{{ value_json.brightness }}'
#    white_value_template: '{{ value_json.white_value }}'
#    red_template: '{{ value_json.color["r"] }}'
#    green_template: '{{ value_json.color["g"] }}'
#    blue_template: '{{ value_json.color["b"] }}'
#    effect_template: '{{ value_json.effect }}'
    availability_topic: 'LB_OfficeMainLight2/LWT'
    payload_available: 'online'
    payload_not_available: 'offline'
    qos: 1

I have those other lines commented out while I’m testing.

If I paste the JSON code in Home Assistant Template creator everything pulls out fine. I’m just really lost on this one.

My MQTT Browser showing the light info:

My ESPHome Log showing the topics

[20:38:54][C][mqtt:060]:   Topic Prefix: 'ESPHome'
[20:38:54][C][mqtt:062]:   Log Topic: 'LB_OfficeMainLight2/debug'
[20:38:54][C][mqtt:065]:   Availability: 'LB_OfficeMainLight2/LWT'
[20:38:54][C][mqtt.light:053]: MQTT Light 'LB_OfficeMainLight2':
[20:38:54][C][mqtt.light:054]:   State Topic: 'LB_OfficeMainLight2/stat'
[20:38:54][C][mqtt.light:054]:   Command Topic: 'LB_OfficeMainLight2/cmnd'

My setup:
Home Assistant 0.99.3 running in a docker on unRAID
ESPHome running in a docker on unRAID (latest version)
MQTT running in a docker on unRAID (latest version)

The reason I am wanting to use the template schema is, so I can hopefully make it where the white value turns all the way down when rgb has a value less then 255 and have it turn back up when they are all at 255.

Thank you for any help

I did find this in the logs today if it helps

2019-10-03 08:08:33 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on LB_OfficeMainLight2/stat (retained): b'{"state":"ON","brightness":1,"color":{"r":255,"g":0,"b":0},"white_value":211}'
2019-10-03 08:08:33 WARNING (MainThread) [homeassistant.components.mqtt.light.schema_template] Invalid state value received

I’m not sure why it is thinking it is invalid since the returned message should either be “ON” or “OFF”

In the documentation, there’s an example containing the following comment:

 # must return `on` or `off`

It seems it is upper-case for most other MQTT components like binary_sensors and switches and even the default light but here, for the template schema, it’s suggesting it is lower-case.

Try this:

    state_template: '{{ value_json.state | lower }}'

Thank you so much Taras. I can’t believe it was that this whole time.

Here is the completed light.yaml code:

  - platform: mqtt
    schema: template
    name: 'Office Test'
    command_topic: 'LB_OfficeMainLight2/cmnd'
    state_topic: 'LB_OfficeMainLight2/stat'
    command_on_template: '{"state":"ON"
       {%- if brightness is defined -%}
       , "brightness": {{ brightness }}
       {%- endif -%}
       {%- if white_value is defined -%}
       , "white_value": {{ white_value }}
       {%- endif -%}
       {%- if red is defined and green is defined and blue is defined -%}
       , "color": {"r":{{ red }},"g":{{ green }},"b":{{ blue }}}
       {%- endif -%}
       }'
    command_off_template: '{"state":"OFF"}'
    state_template: '{{ value_json.state | lower }}'
    brightness_template: '{{ value_json.brightness }}'
    white_value_template: '{{ value_json.white_value }}'
    red_template: '{{ value_json.color["r"] }}'
    green_template: '{{ value_json.color["g"] }}'
    blue_template: '{{ value_json.color["b"] }}'
    availability_topic: 'LB_OfficeMainLight2/LWT'
    payload_available: 'online'
    payload_not_available: 'offline'
    qos: 1
    optimistic: false

Hopefully this will help someone in the future.

1 Like

Hi

My setting is a bit different, but I also have a problem with a template in an MQTT light.

I want to control Dali lights from HA via an MQTT-Dali Gateway. I can control the Dali lights, but I do not get an update in HA of the actual state of the Dali lights (needed since the Dali lights can also be controlled outside of HA via some Dali controllers).

I have a problem with templates on state and brightness. The state JSON payload does not contain an actual state. I need to calculate the state based on brightness. If brightness is 0, state is “off”. Otherwise it’s “on”.

Here’s my config:

  - platform: mqtt
    name: "Dani Desk"
    schema: template
    command_topic: "light/dali-eg/set"
    state_topic: "light/dali-eg/15/state"
    command_on_template: >
      {
        "channel": 15,
        "value": {{ brightness - 1}}
      }
    command_off_template:  >
      {
        "channel": 15,
        "value": 0
      }
    state_template: >
      {%- if value_json.brightness == 0 -%}
        off
      {%- else -%}
        on
      {%- endif -%}
    brightness_template: '{{ value_json.brightness }}'

And here are the errors from the logs:

Logger: homeassistant.components.mqtt.light.schema_template
Source: components/mqtt/light/schema_template.py:173
Integration: MQTT (documentation, issues)
First occurred: 9:46:07 PM (1 occurrences)
Last logged: 9:46:07 PM
Invalid state value received

Logger: homeassistant.helpers.template
Source: helpers/template.py:1791
First occurred: 9:46:07 PM (2 occurrences)
Last logged: 9:46:07 PM
Template variable error: ‘value_json’ is undefined when rendering ‘{%- if value_json.brightness == 0 -%} off {%- else -%} on {%- endif -%}’
Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.brightness }}’

Logger: homeassistant.helpers.template
Source: helpers/template.py:574
First occurred: 9:46:07 PM (2 occurrences)
Last logged: 9:46:07 PM
Error parsing value: ‘value_json’ is undefined (value: { “brightness”: 234, “channel”: 15,}, template: {%- if value_json.brightness == 0 -%} off {%- else -%} on {%- endif -%})
Error parsing value: ‘value_json’ is undefined (value: { “brightness”: 234, “channel”: 15,}, template: {{ value_json.brightness }})

Logger: homeassistant.components.mqtt.light.schema_template
Source: components/mqtt/light/schema_template.py:183
Integration: MQTT (documentation, issues)
First occurred: 9:46:07 PM (1 occurrences)
Last logged: 9:46:07 PM
Invalid brightness value received