MQTT light template config issue

Good afternoon!

Please help me with configuration of the MQTT led light. Prerequisites: I have a led dimmer which has 1 mqtt channel for brightness. the only one, this is important. No On/Off or anything - just brightness from 0 to 255.

I want to be able to operate this light.And it represents no problem with templates using brightness only.

- platform: mqtt
  schema: template
  name: led.kitchen.workzone
  command_off_template: "0"
  command_on_template: '{{ brightness}}'
  brightness_template: '{{ value_json.brightness}}'
  state_template: '{% if value | int(0) == 0 %}off{% else %}on{% endif %}'
  state_topic: /devices/wb-mrgbw-d_84/controls/Channel G
  command_topic: /devices/wb-mrgbw-d_84/controls/Channel G/on

This construction works perfectly as far as I don’t try to just push “Turn On” switch in HA UI. In this case it just does not do anything, saying that the brightness vale was not provided. Well, ok.

But actually I really need to be able to turn the light on using just the ON switch at some pre-defined brightness OR using the slider. Both methods have to work.

I try to use the following construction:

- platform: mqtt
  schema: template
  name: led.kitchen.workzone
  command_off_template: "0"
  command_on_template: '{ {%- if brightness is defined -%} {{ brightness / 1.2 }} {%- else -%} 220 {%- endif -%} }'
  brightness_template: '{{ value_json.brightness}}'
  state_template: '{% if value | int(0) == 0 %}off{% else %}on{% endif %}'
  state_topic: /devices/wb-mrgbw-d_84/controls/Channel G
  command_topic: /devices/wb-mrgbw-d_84/controls/Channel G/on

But it doesnt work at all:

2022-07-17 22:38:55 WARNING (MainThread) [homeassistant.components.mqtt.light.schema_template] Invalid brightness value received
2022-07-17 22:38:55 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'int object' has no attribute 'brightness' when rendering '{{ value_json.brightness }}'
2022-07-17 22:38:55 WARNING (MainThread) [homeassistant.components.mqtt.light.schema_template] Invalid brightness value received

What am I doing wrong?

brightness_template: "{{ value | int(0) }}"

Nope, nothing changes

- platform: mqtt
  schema: template
  name: led.kitchen.workzone
  command_off_template: "0"
  command_on_template: '{ {%- if brightness is defined -%}{{ brightness / 1.2 }}{%- else -%}220{%- endif -%} }'
  brightness_template: "{{ value | int(0) }}"
  state_template: '{% if value | int(0) == 0 %}off{% else %}on{% endif %}'
  state_topic: /devices/wb-mrgbw-d_84/controls/Channel G
  command_topic: /devices/wb-mrgbw-d_84/controls/Channel G/on

The light doesn’t react to any control inputs

this should be all you need

- platform: mqtt
  schema: template
  name: led.kitchen.workzone
  command_off_template: "0"
  command_on_template: >
    {%- if brightness is defined -%}
      {{ brightness / 1.2 }}
    {%- else -%}
      220
    {%- endif -%}
  brightness_template: "{{ value | int(0) }}"
  state_template: >
    {% if value | int(0) == 0 %}
      off
    {% else %}
      on
    {% endif %}
  state_topic: /devices/wb-mrgbw-d_84/controls/Channel G
  command_topic: /devices/wb-mrgbw-d_84/controls/Channel G/on
1 Like

Thank you very much! This code works just perfectly!