Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This function is for use in Node-Red within Home Assistant for the IKEA
- // Trafdri Remote Control. The function can turn on/off a light (or group of
- // lights), adjust the brightness (in stepped increments), and cycle through
- // defined colors (white, red, green, and blue).
- // Usage: An Events: All node with an Event Type of zha_event is used to handle
- // the events. It is connected to a Switch node with a Property of
- // msg.payload.event.device_ieee. The outputs correspond to the device_ieee of
- // the remote being used. This allows for multiple remotes to use different
- // functions. The output is connected to this Function node. Multiple outputs
- // can be connected to this function. The output is connected to an empty
- // Service Call node.
- // The function requires an entity to be defined. A group defined in Home
- // Assistant can also be used to control multiple lights simultaneously. If you
- // want to use this function with only one bulb, enter its entity name in both
- // entity and entity_group.
- var entity = "light.living_room1";
- var entity_group = "light.living_room_lights";
- // Define your desired color temperature when the light cycles back to white
- var color_temp = 154;
- // Define the colors to cycle through using RGB format.
- const color_options = ['255,255,255','0,0,255','255,0,0','0,255,0']
- // ======
- // This function helps prevent multiple entities within a group from being
- // out-of-sync with the other entities. If any entities within the group are on,
- // the group itself will be on and, therefore, the turn_off service will be
- // called in the following call service node. This is more effective than
- // relying on a toggle service.
- var button_pressed = msg.payload.event.command
- var left_right_button_pressed = msg.payload.event.args[0]
- var state = global.get('homeassistant').homeAssistant.states[entity_group].state;
- // Establish standard payload to be passed to service call node
- msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: entity_group,
- transition: 1, }
- };
- if (button_pressed === "checkin") { return }
- if (button_pressed === "on_with_timed_off") { return }
- if (button_pressed === "toggle") {
- if (state === "off") { msg.payload.service = "turn_on" }
- if (state === "on") { msg.payload.service = "turn_off" }
- }
- if (button_pressed === "step_with_on_off" || button_pressed === "step") {
- var brightness = global.get('homeassistant').homeAssistant.states[entity].attributes.brightness;
- brightness = parseInt(brightness);
- if (button_pressed == "step_with_on_off") { //Brightness Up pressed
- if (brightness < 204) {
- brightness = brightness + 50;
- } else {
- brightness = 255;
- }
- }
- if (button_pressed == "step") { //Brightness Down pressed
- if (brightness > 51) {
- brightness = brightness - 50;
- } else {
- brightness = 1;
- }
- }
- msg.payload.data.brightness = brightness.toString();
- }
- if (button_pressed === "press") {
- // This section uses an older revision of the code where strings were
- // evaluated and used instead of objects. I was unable to figure out how to
- // make this section work using objects so it is still using the older
- // string format.
- var rgb_in = [], rgb_out = []
- //const color_options = ['255,255,255','0,0,255','255,0,0','0,255,0']
- //const color_options = [[255,255,255],[0,0,255],[255,0,0],[0,255,0]];
- //Determines if the light is running in color_temp mode or RGB.
- //Color_temp will not have RGB associated with it even though it is
- //white is [255,255,255].
- const d = global.get('homeassistant').homeAssistant.states[entity].attributes
- if (d.rgb_color != null) {
- rgb_in = d.rgb_color.toString();
- //rgb_in = d.rgb_color;
- } else {
- rgb_in = "255,255,255";
- //rgb_in = [255,255,255];
- }
- //Finds the current RGB in the color_options array and determines which RGB
- //will be displayed next depending on advancing in the array or stepping
- //backwards.
- var key = color_options.indexOf(rgb_in)
- if (key == -1) { key = 0; } //If RGB not found in color_options, set to 0
- if (left_right_button_pressed == 256) { //Right button pressed (257 = Left)
- direction = 1
- } else {
- direction = -1
- }
- //Provides 'loop around' functionality for the array
- new_key = (key + direction) % color_options.length
- if (new_key == -1) { new_key = color_options.length - 1; }
- rgb_out = color_options[new_key];
- //Generates the msg.payload with the new RGB or sets the color to white with
- //color_temp setting.
- if (rgb_out == '255,255,255') {
- //if (rgb_out == [255,255,255]) {
- msg.payload.data.color_temp = color_temp;
- } else {
- //Recent NR/HA update broke sending the rgb_color as a string. Call
- //service node wanted an object. This RegEx fixed the problem without
- //having to re-write the entire RGB changing section, which still
- //needs to be done at some point.
- msg.payload.data.rgb_color = rgb_out.replace(/[^\d,]/g, '').split(',');
- //msg.payload.data.rgb_color = [rgb_out];
- }
- }
- return msg;
Advertisement
Add Comment
Please, Sign In to add comment