d_sellers1

IKEA Shortcut Button

Oct 25th, 2021 (edited)
157
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 Shortcut Button (or similar device from other manufacturers). The
  3. // function can turn toggle a light (or group of lights) based on how the
  4. // button was pressed: single press, double press, or press and hold.
  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 entities to be defined. A group defined in Home
  15. // Assistant can also be used to control multiple lights simultaneously. To
  16. // disable a specific button press, replace the entity name with double quotes
  17. // ("").
  18.  
  19. var single_press_entity = "light.stove";
  20. var double_press_entity = "light.kitchen";
  21. var push_and_hold_entity = "group.kitchen";
  22.  
  23. // Assign output variable based on how the button was pressed
  24. var output_entity = "";
  25. switch(msg.payload.event.command) {
  26.     case "toggle":
  27.         output_entity = single_press_entity;
  28.         break;
  29.     case "on":
  30.         output_entity = double_press_entity;
  31.         break;
  32.     case "off":
  33.         output_entity = push_and_hold_entity;
  34.         break;
  35. }
  36.  
  37. // Payload to be passed to service call node
  38. msg.payload = {domain: "light", service: "toggle",
  39.     data: {
  40.         entity_id: output_entity, }
  41.     };
  42.  
  43. return msg;
Add Comment
Please, Sign In to add comment