Injecting attributes into MQTT messages on state change

I have several Hue bulbs hooked up to HA via Zigbee2MQTT. All is working well.

I recently set up a ‘cosy’ scene where I dim most of the bulbs, and set a couple to run ‘Candle’ effect (because cosy…). Looks lovely, much spousal approval etc.

However, there’s no easy way to get the bulbs out of candle effect mode again… sure I could create another ‘normal’ scene and have to run that to reset the light effects, but that’s a clunky user experience - ideally whenever any setting of the light gets changed (i.e. if someone moves the brightness slider in lovelace) then the effect should be cancelled.

So here’s the question; how can I get any change in HA to automatically include effect: stop_hue_effect attribute (unless already containing an explicit effect attribute)?

I briefly looked into z2m extensions, but it’s not clear from the code whether they work like middleware (to allow me to modify the mqtt message before it passes to the zigbee coordinator) or whether it just forks off the main bus as a separate event.

Any other ways I could approach this?

An automation

Trigger - when the light brightness changes

Condition - if candle mode is set

Action - turn candle mode off

I did consider that, haven’t tried it yet, however my concerns with that approach would be:

  • There is no way to read current effect state - so there’s no way to tell if the light is in candle mode or not

  • Automation would fire a follow-up event after the state has changed, which would mean the hue bulb would already have received and rejected ignored the initial setting change (If a hue bulb is in ‘candle’ mode and it’s brightness is changed, the candleness overrides the new brightness setting)

Isn’t the effect an attribute (I just realized I don’t currently have a light with effects to try it out)

I don’t quite follow your second point.

So without knowing how the internal firmware logic works, what i’ve observed is the following behaviour sequence:

Set brightness=255 → bulb brightness changes
Set effect=candle → bulb goes into candle mode
Set brightness=200 → no visible change, bulb stays in candle mode (but mqtt state reflects brightness change)
Set effect=none → candle mode stops, but remains at the final brightness of the candle flicker, not at the expected 200 brightness from my last set action, despite the state brightness value reading correctly

I will double-check these observations when I get home later, but assuming i didn’t dream them then having an automation run on_change it’d need to clear both the effect and also re-set the brightness to force it to update again.