MQTT V2 - Zigbee2MQTT - Alexa. How would you do it

Hi There.

This post is to try and find out the best way to accomplish what I want to do.

I have Zigbee2MQTT setup controlling all my lights and I want to interface it with OpenHAB the best way possible.

What I want to have is a single Dimmer item i can share with Alexa. This dimmer need to take a 0-100 value and change it into a 0-255 value and put that into a JSON format. When this dimmer revieves an OFF command or is set to 0 I need it to ignore the brightness and set “state”=OFF, if it then recieves a command of ON it sets “state”=ON. My reason for doing this is if I tell Alexa to turn the light on I want ti to come back on at the previous brightness.

Originally I followed several example on this forum to get it working using MQTT V1 and Javascript transformations, I’ve been advised to avoid Javascript transformations because they are resource hungry and to get it working on MQTT V2 sooner rather than later as V1 won’t be supported forever.

So I’ve managed to get it working using MQTT V2 and I have written two rules to do the incoming and outgoing transformations. The only problem I am now getting is when an automation runs and tries to change several lights at the same time I get errors about the rule and not everything works, I believe there is a limit to how many rules can run at the same time and this is causing my problem. to prove this I added a couple of timers to slow the process down and everything ran without problem.

So, basically, I just wanted to ask what you thought the best practise was.

I’m not posting my code nor do I want you to spend loads of time writing any examples, I just want some general advise as to the best way to go about this.


With the latest Dev branch of Zigbee2MQTT you should be able to change the min and max values of the dimmer to 0-255 and without the bug that returned a brightness value of 1 when set to 0 which would make OH think the bulb was still on.

Add persistence to the item and see if that helps with returning to the previous brightness. Works with good with my zigbee2mqtt and Alexa.

No need for a rule if you add

transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{\"brightness\":%s}"

add min and max if needed:

 min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{\"brightness\":%s}"

Full Things file example:

Type dimmer : dimmer "Dimmer"              [ stateTopic="zigbee/0xb0ce1814030ac279", commandTopic="zigbee/0xb0ce1814030ac279/set", min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{\"brightness\":%s}" ]

Correct, there is a limit but I do not remember the default number. You can increase the thread pool by adding org.eclipse.smarthome.threadpool:RuleEngine=20 to /etc/openhab2/services/runtime.cfg If that doesn’t help then timers, as you have already discovered, are the solution.

Your rules could possibly be modified to run/work more efficient but without seeing them no one can say. My general advice is, if what your doing works, then stick with it.:wink:

1 Like