One button - one bulb - on/off/brightness

controlling the light by zigbee2mqtt dashboard works as designed. But now I get weird node red context data?

And another thing:


Who sent this? This is the /set topic set to different values! Have you refreshed the context data? After the flow sending the brightness message the context data should show the brightness message as it always pings back from the mqtt broker (unless you refine your subscription to “zigbee2mqtt/+/+” but leave it as it is for now because you have better control of all the messages on your mqtt brkoker)

Forget the array (this comes for converting a string into an array somewhere down the line).

The state is still off and the brightness is on it’s highes value.

Have you inspected the data with mqtt explorer? (App available on all desktop OS). This takes node-red out of the equation and give you a good insight what is happening on your broker

What node are you using to receive data. Try an MQTT Node with the topic as: zigbee2mqtt/bulb_RGB_01

You can also use the output of the switch directly once you have sorted out why the lamp is not responding (did you check the lamp after sending the 'brightness' command?)

From the relevant zigbee2mqtt device page:

Moving/stepping

Instead of setting a value (e.g. brightness) directly it is also possible to:

  • move: this will automatically move the value over time, to stop send value stop or 0 .
  • step: this will increment/decrement the current value by the given one.

The direction of move and step can be either up or down, provide a negative value to move/step down, a positive value to move/step up. To do this send a payload like below to zigbee2mqtt/FRIENDLY_NAME/set

NOTE : brightness move/step will stop at the minimum brightness and won't turn on the light when it's off. In this case use brightness_move_onoff / brightness_step_onoff

{
  "brightness_move": -40, // Starts moving brightness down at 40 units per second
  "brightness_move": 0, // Stop moving brightness
  "brightness_step": 40 // Increases brightness by 40
}

I am realy confused at the moment and I need one or days to understand the situation.
It is also difficult for me to formulate that in English.
Now MQTT Explorer is installed, ...this should help me.

btw, I saw, that the last node send to the onebutton device (see picture).
This is ok, should that be the case?

Unbenannt

Nope, not ok! You have to change the topic to the topic of your bulb here:

Then the +/- function node finds your last value and send the new brightness to the bulb, not the button.

With mqtt explorer you should see the contents of your broker see all changes and you can play with values.

Maybe a quick fix but I would prefer to have control over the brightness value instead of only asking for move up/down. But only my personal preference.

...but the bulb do not give me a msg like "brightness_move_up"(??)

I put the change node (onebutton -> bulb) behind the switch unit and now the brightness decrease. But not in the SET section :confused:

You receive a message from your key … then you change the topic to the topic of the bulb in this node because the bulb is the target now. (It is in the flow I’ve posted):

image

in your case change it to:
zigbee2mqtt/bulb_RGB_01

Do not add /set! The /set is added to the topic at the end of the flow

image

So would I :grin:, but this shows how the combination of the switch & lamp are probably intended to be used. As it is (as you said earlier) until we get a message to the lamp it is all moot.

This is the function node code I used for changing the brightness when the button is held down. It ignores the states of the button and uses it as a trigger instead (because other automation can have changed it's state too). It also "bounces" the direction of the brightness change when it gets to the limit (so that if you miss where you want to get, then you can just wait for it to get back to where you wanted). I use it in a sub-flow, but I think it will work as a function node on its own too.

const brightness_step = 2;
const brightness_lo = 20;
const brightness_hi = 100;
const brightness_fast = 100;
const brightness_slow = 800;

function update_status(local)
{
    const on = msg.payload.on;
    const brightness = msg.payload.brightness || brightness_hi;
    context.set('on', on);
    context.set('brightness', brightness);
    const battery = context.get('battery');
    let text = `(${on ? 'on' : 'off'} ${brightness})`;
    if (battery) {
       text += ` ⚡${battery}%`;
    }
    const fill = local ? 'blue' : 'green';
    node.status({fill:fill,shape:"dot",text:text});
}

if (msg.payload.battery != null) {
    context.set('battery', msg.payload.battery);
}

if (msg.payload.on != null) {
    update_status(false);
    return;
}

const action = msg.payload.action;
if (action == null) {
    return;
}

const last_action = context.get('action') || action;
context.set('action', action);
if (last_action == action) {
    return;
}

msg.payload = {
    'on': context.get('on') || false,
    'brightness': context.get('brightness') || 100,
}

const timer = context.get('timer') || false;
if (timer) {
    clearTimeout(timer);
}

function brightness_move(direction) {
    if ((direction < 0 && msg.payload.brightness <= brightness_lo) ||
        (direction > 0 && msg.payload.brightness >= brightness_hi)) {
        direction = -direction;
    }
    context.set('direction', direction);
    msg.payload.brightness = Math.max(brightness_lo, Math.min(brightness_hi, msg.payload.brightness + direction));
    update_status(true);
    node.send(msg);
    let delay = ((msg.payload.brightness <= brightness_lo) || 
                 (msg.payload.brightness >= brightness_hi)) ? brightness_slow : brightness_fast;
    context.set('timer', setTimeout(brightness_move, delay, direction));
}

if (action == 'on' || action == 'off') {
    msg.payload.on = !msg.payload.on;
    update_status(true);
    return msg;
}

if (action == 'brightness_move_up' || action == 'brightness_move_down') {
    if (!msg.payload.on) {
        msg.payload.on = true;
        msg.payload.brightness = brightness_lo;
    }
    const direction = context.get('direction') > 0 ? -brightness_step : brightness_step;
    brightness_move(direction);
    return;
}

if (action == 'brightness_stop') {
    // clearTimeout happened above
    update_status(true);
    return;
}

node.error('unknown action: ' + action);
return;

Thank you all for the hints and example snippets. Finally I found a simple and working way for my request.

[{"id":"c80d1737eca733c6","type":"tab","label":"Onebutton","disabled":false,"info":"","env":[]},{"id":"63e0a5986cba44f0","type":"switch","z":"c80d1737eca733c6","name":"switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"brightness_move_up","vt":"str"},{"t":"eq","v":"brightness_move_down","vt":"str"},{"t":"eq","v":"brightness_stop","vt":"str"}],"checkall":"false","repair":false,"outputs":5,"x":350,"y":160,"wires":[["c8f0304f1aa8e1c0"],["c8f0304f1aa8e1c0"],["febe4d331e9b5f78"],["febe4d331e9b5f78"],["febe4d331e9b5f78"]]},{"id":"9d682cd16cb0e817","type":"mqtt in","z":"c80d1737eca733c6","name":"mqtt/onebutton_01","topic":"zigbee2mqtt/onebutton_01","qos":"2","datatype":"json","broker":"4803db9febf3ecc7","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":120,"wires":[["138316ed6fa2843a"]]},{"id":"c8f0304f1aa8e1c0","type":"mqtt out","z":"c80d1737eca733c6","name":"mqtt/bulb_RGB_01","topic":"zigbee2mqtt/bulb_RGB_01/set","qos":"0","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"4803db9febf3ecc7","x":690,"y":120,"wires":[]},{"id":"febe4d331e9b5f78","type":"trigger","z":"c80d1737eca733c6","name":"repeat","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-100","extend":false,"overrideDelay":false,"units":"ms","reset":"brightness_stop","bytopic":"all","topic":"topic","outputs":1,"x":510,"y":200,"wires":[["44cf486c63679f1b"]]},{"id":"44cf486c63679f1b","type":"change","z":"c80d1737eca733c6","name":"change payload","rules":[{"t":"change","p":"payload","pt":"msg","from":"brightness_move_up","fromt":"str","to":"{\"brightness_step\":5}","tot":"json"},{"t":"change","p":"payload","pt":"msg","from":"brightness_move_down","fromt":"str","to":"{\"brightness_step\":-5}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":200,"wires":[["c8f0304f1aa8e1c0"]]},{"id":"138316ed6fa2843a","type":"change","z":"c80d1737eca733c6","name":"payload.action","rules":[{"t":"move","p":"payload.action","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":200,"wires":[["63e0a5986cba44f0"]]},{"id":"4803db9febf3ecc7","type":"mqtt-broker","name":"MQTT","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.