mochman / node-red-contrib-light-transition

Node-red light transition
https://flows.nodered.org/node/node-red-contrib-light-transition
Apache License 2.0
10 stars 4 forks source link

Possibility to use brightness (0-255) instead of percentage #3

Closed mortenmoulder closed 3 years ago

mortenmoulder commented 3 years ago

Awesome node. I've just used it to dim some IKEA bulbs and it works great. However, I had to use a function, because I grab my bulb's brightness first, and then pass that into the change node. When I grab my bulb's brightness, I do not get a percentage back, but instead get a 0-255 value.

It would be great if transitioning between 0 and 255 is possible with 2 more attributes (optional of course). Here is my workaround:

Create a new function node:

function newBrightness(current_brightness) {
    newBrightnessPct = (current_brightness / 256) * 100
    return Math.max(0, Math.min(100, Math.round(newBrightnessPct)))
}

newMsg = {
    payload: {
        "data": {
            "entity_id": msg.topic,
            "old_brightness": msg.data.attributes.brightness,
            "brightness_pct": newBrightness(msg.data.attributes.brightness)
        }
    }
}

return newMsg;

That converts 255 to 100%, 127 to 50%, and so on.

Then pass that node to the change node (look at startBright):

{
    "duration": 5,
    "units": "Second",
    "steps": 10,
    "startRGB": "#ffffff",
    "transitionRGB": "#ffffff",
    "endRGB": "#ffffff",
    "startBright": msg.payload.data.brightness_pct,
    "endBright": 100,
    "transitionType": "Linear",
    "colorTranstitionType": "None"
}

Thanks

mochman commented 3 years ago

Sounds like a good feature. I think the easiest way for me to handle that is to have a separate selector for brightness type, where someone can select brightness or brightness_pct.

mochman commented 3 years ago

I just pushed this feature to flows.nodered.org It should be version 1.3 if you want to check it out.
You should be able to get rid of your helper function and set:

"brightnessType": "Integer"