Need some help configuring an MQTT light in HA

Hi everyone!

I recently migrated all of my Zigbee devices to the HA Yellow radio with ZHA. For a couple of reasons, I was previously using the Yellow radio with Zigbee2MQTT and had a couple of “incompatible devices” paired to a Sonoff Zigbee Bridge Pro (flashed with Tasmota) connected to HA through ZHA.

I tried to migrate all the devices to the Yellow’s radio + ZHA but there is one device that won’t play nice with the Yellow’s radio, but it worked fine with the ZBBridge (it’s a Philips Hue Filament Bulb that I already had exchanged because it was giving some issues, but now I assume it is just incompatible with the Yellow radio).

So, I set the ZBBridge to connect to my MQTT server, I paired the light and I’m seeing the MQTT messages but I’m not able to make it into a fully working Home Assistant device/entity. I’m not used to this level of complexity with MQTT devices, I only configured a couple of Sonoff switches before, but this requires some extra digging.

This is my current MQTT config:

light:
  - name: "Globe Light"
    command_topic: "cmnd/ZBBridgePro/ZbSend"
    payload_on: '{"device":"0xEB8C","send":{"Power": 1}}'
    payload_off: '{"device":"0xEB8C","send":{"Power": 0}}'
    state_topic: "tele/ZBBridgePro/SENSOR"
    state_value_template: '{{ trigger.payload_json["ZbReceived"]["0xEB8C"]["Dimmer"] }}'
    brightness_scale: 254
    brightness_command_topic: "cmnd/ZBBridgePro/ZbSend"
    brightness_command_template: '{"Device":"0xEB8C", "Send": {"Dimmer": {{ value }} } }'
#    availability_topic: "tele/ZBBridgePro/LWT"
#    payload_available: "Online"
#    payload_not_available: "Offline"
    optimistic: false
    qos: 0

I started by copying lines from this post and I think I need help with the state_value_template.

These are the messages received by the MQTT server when the light turns on:

{"ZbReceived":{"Globe_Light":{"Device":"0xEB8C","Name":"Globe_Light","Power":1,"Endpoint":11,"LinkQuality":167}}}

And off:

{"ZbReceived":{"Globe_Light":{"Device":"0xEB8C","Name":"Globe_Light","Power":0,"Endpoint":11,"LinkQuality":167}}}

The current situation is: I can send brightness changes from HA and the lights responds normally, but the entity’s state shows up as “unknown” and I cannot send on/off commands from the HA entity.
I can send these commands with 0 or 1 from the MQTT settings and the light turns on or off, but the entity doesn’t get the updates:

{"Device":"Globe_Light","Send":{"Power":0}}

Any help is appreciated :slight_smile: I would like to see some examples of how this should be set up.

Thanks,
Rodrigo

light:
  - name: "Globe Light"
    command_topic: "cmnd/ZBBridgePro/ZbSend"
    payload_on: '{"device":"0xEB8C","send":{"Power": 1}}'
    payload_off: '{"device":"0xEB8C","send":{"Power": 0}}'
    state_topic: "tele/ZBBridgePro/SENSOR"
    state_value_template: '{{ iif (value_json["ZbReceived"]["Globe_Light"]["Power"] == 1, "on", "off") }}'
    brightness_scale: 254
    brightness_command_topic: "cmnd/ZBBridgePro/ZbSend"
    brightness_command_template: '{"Device":"0xEB8C", "Send": {"Dimmer": {{ value }} } }'
    optimistic: false
    qos: 0

Changed:

  • state_value_template
1 Like

Thanks @koying for your help! Unfortunately I’m getting this error message:

Invalid config for [mqtt]: [command_on_template] is an invalid option for [mqtt]. Check: mqtt->mqtt->light->0->command_on_template. (See /config/configuration.yaml, line 10). Please check the docs at MQTT - Home Assistant

The situation remains the same, light shows up as unavailable, and I can only control brightness, not on/off.

R.

Right. Answer updated.

1 Like

Thanks again @koying, I had also tried that, changing the command_on_template for payload_on and off, but it didn’t work, the device still shows up as unavailable.

Reading the # comments from the code in the post I sent before, and also reading the HA MQTT documentation on state_value_template, I understand that the result from this template should match the payload values.
So I think that I need a code that matches the whole ‘{“device”:“0xEB8C”,“send”:{“Power”: 1}}’. I’m just guessing, I’m not sure about this or how to achieve this.

I think I still need a bit more help here on how to shape this template. Thanks again,
R.

Well, I have some progress here.

After reading and understanding things a bit more, I tried to use the Template Schema instead of the default schema.

In this schema, I can use the command_on_template (and off) as @koying suggested initially, and some things are working better with this code:

light:
  - schema: template
    name: "Globe Light"
    command_topic: "cmnd/ZBBridgePro/ZbSend"
    command_on_template: >
      {% if brightness is defined %}
        {"device":"Globe_Light","send":{"Dimmer": {{ brightness }} }}
      {% else %}
        {"device":"Globe_Light","send":{"Power": 1}}
      {% endif %}
    command_off_template: '{"device":"Globe_Light","send":{"Power": 0}}'
    state_topic: "tele/ZBBridgePro/SENSOR"
    state_template: >
      {% if value_json.ZbReceived.Globe_Light.Power == 1 or value_json.ZbReceived.Globe_Light.Dimmer is defined %}
        on
      {% else %}
        off
      {% endif %}
    brightness_template: >
      {% if value_json.ZbReceived.Globe_Light.Dimmer is defined %}
        {{ value_json.ZbReceived.Globe_Light.Dimmer }}
      {% endif %}
    optimistic: false
    qos: 0 

I am currently having one issue:

This light bulb does its brightness changes with 1 second fades, and during the fade it reports intermediate brightness values. It seems that some of these changes have a little delay, so when I send the turn off command (by clicking the power button in the entity) the “off” message arrives first and then, during the fade off, the light reports new messages with brightness changes, and the result is that the bulb turns off but the entity turns off and immediately on again.
[Screen capture showing the turn off issue]

How could I manage this issue? Is there any way to make the entity ignore the new messages it receives immediately after turning off?

Thanks!
Rodrigo

Solved:
When I send the Power:0 command, this mixed messages issue happens. But if I send Dimmer:0 instead, then the last message received says Power:0 and it all works fine.

R.