Created
December 26, 2023 08:42
-
-
Save instance-id/e67882bd09470ab8962f572058f46bdf to your computer and use it in GitHub Desktop.
A script for a node-red function node to handle Hue Tap Dial rotation and button presses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var action = msg.payload.action; | |
var direction = msg.payload.action_direction; | |
var step = msg.payload.action_time; | |
if (action == null || action == undefined || action == "") { | |
return; | |
} | |
var dialDirections = ["right", "left"]; | |
var buttonNumbers = ["1", "2", "3", "4"]; | |
var dialActions = ["step", "slow", "fast"]; | |
var buttons = ["button_1", "button_2", "button_3", "button_4"]; | |
var buttonActions = ["press", "press_release", "hold", "hold_release"]; | |
// --| Set the desired button state in which to trigger the action | |
var desiredButtonTrigger = buttonActions[1]; | |
var buttonStrings = {}; | |
buttonNumbers.forEach(function (number) { | |
buttonActions.forEach(function (action) { | |
buttonStrings["button_" + number + "_" + action] = | |
"button_" + number + "_" + action; | |
}); | |
}); | |
var dialStrings = {}; | |
dialDirections.forEach(function (direction) { | |
dialActions.forEach(function (action) { | |
dialStrings["dial_rotate_" + direction + "_" + action] = | |
"dial_rotate_" + direction + "_" + action; | |
}); | |
}); | |
// --| In the dial outputs, you can use either | |
// --| 'msg.payload.params.step_size' - to get a fixed step size per rotation increment based on the speed | |
// --| 'msg.payload.params.bump_value' - to use the speed of the rotation as the step size | |
// --| Example using Home Assistant Call Service node for a light, calling the 'light.turn_on' service | |
// --| Then in the 'Data' field, enter the following: | |
// --| { "brightness_step_pct": msg.payload.params.bump_value,"transition": 0.5 } | |
if (direction == "left" || direction == "right") { | |
var cmd = { | |
command: direction == "left" ? "down" : "up", | |
step_size: direction == "left" ? -1 : 1, | |
bump_value: (direction == "left" ? -step : step) * 0.5, | |
}; | |
if (action == dialStrings["dial_rotate_" + direction + "_step"]) { | |
cmd.step_size *= 5; | |
} | |
if (action == dialStrings["dial_rotate_" + direction + "_slow"]) { | |
cmd.step_size *= 10; | |
} | |
if (action == dialStrings["dial_rotate_" + direction + "_fast"]) { | |
cmd.step_size *= 20; | |
} | |
msg.payload.params = cmd; | |
return direction == "left" | |
? [msg, null, null, null, null, null] | |
: [null, msg, null, null, null, null]; | |
} | |
for (var i = 0; i < buttons.length; i++) { | |
if (action == buttonStrings[buttons[i] + "_press_release"]) { | |
var btncmd = { | |
command: "release", | |
button: i + 1, | |
}; | |
msg.payload.params = btncmd; | |
// --| Use the index determine the output in which the return message is sent | |
// --| We use +2 because the dial actions are at index 0 and 1 | |
var returnArray = [null, null, null, null, null, null]; | |
returnArray[i + 2] = msg; | |
return returnArray; | |
} | |
} | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment