d_sellers1

IKEA Tradfri Remote

Oct 11th, 2021 (edited)
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This function is for use in Node-Red within Home Assistant for the IKEA
  2. // Trafdri Remote Control. The function can turn on/off a light (or group of
  3. // lights), adjust the brightness (in stepped increments), and cycle through
  4. // defined colors (white, red, green, and blue).
  5.  
  6. // Usage: An Events: All node with an Event Type of zha_event is used to handle
  7. // the events. It is connected to a Switch node with a Property of
  8. // msg.payload.event.device_ieee. The outputs correspond to the device_ieee of
  9. // the remote being used. This allows for multiple remotes to use different
  10. // functions. The output is connected to this Function node. Multiple outputs
  11. // can be connected to this function. The output is connected to an empty
  12. // Service Call node.
  13.  
  14. // The function requires an entity to be defined. A group defined in Home
  15. // Assistant can also be used to control multiple lights simultaneously. If you
  16. // want to use this function with only one bulb, enter its entity name in both
  17. // entity and entity_group.
  18. var entity = "light.living_room1";
  19. var entity_group = "light.living_room_lights";
  20. // Define your desired color temperature when the light cycles back to white
  21. var color_temp = 154;
  22. // Define the colors to cycle through using RGB format.
  23. const color_options = ['255,255,255','0,0,255','255,0,0','0,255,0']
  24.  
  25. // ======
  26. // This function helps prevent multiple entities within a group from being
  27. // out-of-sync with the other entities. If any entities within the group are on,
  28. // the group itself will be on and, therefore, the turn_off service will be
  29. // called in the following call service node. This is more effective than
  30. // relying on a toggle service.
  31.  
  32. var button_pressed = msg.payload.event.command
  33. var left_right_button_pressed = msg.payload.event.args[0]
  34. var state = global.get('homeassistant').homeAssistant.states[entity_group].state;
  35.  
  36. // Establish standard payload to be passed to service call node
  37. msg.payload = {domain: "light", service: "turn_on",
  38.     data: {
  39.         entity_id: entity_group,
  40.         transition: 1, }
  41.     };
  42.  
  43. if (button_pressed === "checkin") { return }
  44. if (button_pressed === "on_with_timed_off") { return }
  45.  
  46. if (button_pressed === "toggle") {
  47.     if (state === "off") { msg.payload.service = "turn_on" }
  48.     if (state === "on") { msg.payload.service = "turn_off" }
  49. }
  50.  
  51. if (button_pressed === "step_with_on_off" || button_pressed === "step") {
  52.     var brightness = global.get('homeassistant').homeAssistant.states[entity].attributes.brightness;
  53.     brightness = parseInt(brightness);
  54.  
  55.     if (button_pressed == "step_with_on_off") { //Brightness Up pressed
  56.         if (brightness < 204) {
  57.             brightness = brightness + 50;
  58.         } else {
  59.             brightness = 255;
  60.         }
  61.     }
  62.  
  63.     if (button_pressed == "step") { //Brightness Down pressed
  64.         if (brightness > 51) {
  65.             brightness = brightness - 50;
  66.         } else {
  67.         brightness = 1;
  68.         }
  69.     }
  70.     msg.payload.data.brightness = brightness.toString();
  71. }
  72.  
  73. if (button_pressed === "press") {
  74.     // This section uses an older revision of the code where strings were
  75.     // evaluated and used instead of objects. I was unable to figure out how to
  76.     // make this section work using objects so it is still using the older
  77.     // string format.
  78.     var rgb_in = [], rgb_out = []
  79.     //const color_options = ['255,255,255','0,0,255','255,0,0','0,255,0']
  80.     //const color_options = [[255,255,255],[0,0,255],[255,0,0],[0,255,0]];
  81.  
  82.     //Determines if the light is running in color_temp mode or RGB.
  83.     //Color_temp will not have RGB associated with it even though it is
  84.     //white is [255,255,255].
  85.     const d = global.get('homeassistant').homeAssistant.states[entity].attributes
  86.     if (d.rgb_color != null) {
  87.         rgb_in = d.rgb_color.toString();
  88.         //rgb_in = d.rgb_color;
  89.     } else {
  90.         rgb_in = "255,255,255";
  91.         //rgb_in = [255,255,255];
  92.     }
  93.  
  94.     //Finds the current RGB in the color_options array and determines which RGB
  95.     //will be displayed next depending on advancing in the array or stepping
  96.     //backwards.
  97.     var key = color_options.indexOf(rgb_in)
  98.     if (key == -1) { key = 0; } //If RGB not found in color_options, set to 0
  99.     if (left_right_button_pressed == 256) { //Right button pressed (257 = Left)
  100.         direction = 1
  101.     } else {
  102.         direction = -1
  103.     }
  104.  
  105.     //Provides 'loop around' functionality for the array
  106.     new_key = (key + direction) % color_options.length
  107.     if (new_key == -1) { new_key = color_options.length - 1; }
  108.     rgb_out = color_options[new_key];
  109.  
  110.     //Generates the msg.payload with the new RGB or sets the color to white with
  111.     //color_temp setting.
  112.     if (rgb_out == '255,255,255') {
  113.     //if (rgb_out == [255,255,255]) {
  114.         msg.payload.data.color_temp = color_temp;
  115.     } else {
  116.         //Recent NR/HA update broke sending the rgb_color as a string. Call
  117.         //service node wanted an object. This RegEx fixed the problem without
  118.         //having to re-write the entire RGB changing section, which still
  119.         //needs to be done at some point.
  120.         msg.payload.data.rgb_color = rgb_out.replace(/[^\d,]/g, '').split(',');
  121.         //msg.payload.data.rgb_color = [rgb_out];
  122.     }
  123. }
  124.  
  125. return msg;
Advertisement
Add Comment
Please, Sign In to add comment