MQTT json template brightness_template Light feedback

Hi.

Can someone please tell me how to grab a json brightness and update the status in HA before i break the “Check Configuration” and “Restart button”?

I have a MQTT light that only outputs brightness states in json when updated status, there is no state command from the light. I am able to control the light as expected from HA using the set topic and status shown in GUI is correct, but if i change the light from any other source it does not update the status in HA.

Examples from MQTT explorer:


The json string from the dimmer(hdl/1.61/2) contains channel nr for the light, execution status and level witch is the brightness from the light and the value i want in to HA. Also you can see the set string from HA(hdl/1.61/2/set)

My configuration.yaml file for the light

light:
  - platform: mqtt
    schema: template
    name: "TV Light"
    state_topic: "hdl/1.61/2"
    command_topic: "hdl/1.61/2/set"
    command_on_template: >
      {"state": "on"
      {%- if brightness is defined -%}
      , "level": {{ brightness }}
      {%- else -%}
      , "level": 255
      {%- endif -%}
      }
    command_off_template: '{"level": 0}'
    brightness_template: "{{ value_json.level }}"

Have tried changing “brightness_template” to the following:

    brightness_template: "{{ level }}"

and:

    brightness_template: "{{ value_json['level'] }}"

Brightness does not change status in HA when i change the light manually.

That’s not too surprising because the configuration doesn’t include a state_template for interpreting the payload received via the state_topic.

There’s nothing in the payload indicating on or off so we will use the value of level.

Add this line to the light’s configuration and then execute Configuration > Server Controls > Reload Manually Created MQTT Entities (or do a Check Configuration first to ensure your modifications are valid).

    state_template: "{{ 'on' if value_json.level | int > 0 else 'off' }}"

Your brightness_template seems fine so maybe the lack of a state_template is what prevented it from working.

1 Like

Thanks, that worked :smiley:

That was actually a big surprise to me. I was sure it was me formating the brightness_template wrong as i did not get an updated brightness status on the GUI or under the developer tools. And was kind of expecting the brightness to change without state update.

Thanks for the hint, this could have saved me a lot of time instead of waiting for complete reboots during my attempts.

1 Like