d_sellers1

Aqara Cube WIP

Apr 17th, 2022 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ha = global.get('homeassistant').homeAssistant
  2. //Currently uses a Blueprint that enables automation for the Aqara cube.
  3. //Because the cube doesn't say what side is facing up, an input_helper
  4. //is used to remember the last side facing.
  5. //This ability will need to be incorporated into the function at some
  6. //point so the Blueprint will not be required.
  7. var side_facing = parseInt(ha.states["input_number.cube_helper"].state);
  8. var command = msg.payload.event.command
  9.  
  10. //The "knock" command is not reliable and should not be used!
  11. //UPDATE: It seems to work if I "double-tap" the cube on my couch cushion.
  12. if (command === "knock") {
  13.     node.status({text: "Knock detected!" });
  14.     return
  15. }
  16. if (command === "wake") {    
  17.     node.status({text: "Cube awakened" });
  18.     return
  19. }
  20.  
  21. if (command === "flip") {
  22.     var activated_face = msg.payload.event.args.activated_face;
  23.     if (activated_face === 1) {
  24.         node.status({text: "Turned on bedroom lights" });
  25.         msg.payload = {domain: "light", service: "turn_on",
  26.         data: {
  27.             entity_id: "light.bedroom_lights",
  28.             transition: 1, }
  29.         };
  30.         return msg; }
  31.     if (activated_face === 4) {
  32.         node.status({text: "Turned off bedroom lights" });
  33.         msg.payload = {domain: "light", service: "turn_off",
  34.         data: {
  35.             entity_id: "light.bedroom_lights",
  36.             transition: 1, }
  37.         };
  38.         return msg
  39.     }
  40.     node.status({text: "New side: " + activated_face + "; no command" });
  41.     return
  42. }
  43.        
  44. if (side_facing === 1) {
  45.     if (command === "rotate_right" || command == "rotate_left") {
  46.         var brightness = ha.states["light.bedroom1"].attributes.brightness;
  47.         brightness = parseInt(brightness);
  48.         if (command === "rotate_right") {  
  49.             if (brightness < 204) {
  50.                 brightness = brightness + 50;
  51.             } else {
  52.                 brightness = 255;
  53.             }
  54.             msg.payload = {domain: "light", service: "turn_on",
  55.             data: {
  56.                 entity_id: "light.bedroom_lights",
  57.                 brightness: brightness,
  58.                 transition: 1, }
  59.             };
  60.             node.status({text: "Bedroom brightness increased" });
  61.             return msg;
  62.         }
  63.         if (command === "rotate_left") {
  64.             if (brightness < 204) {
  65.                 brightness = brightness + 50;
  66.             } else {
  67.                 brightness = 255;
  68.             }
  69.             msg.payload = {domain: "light", service: "turn_on",
  70.             data: {
  71.                 entity_id: "light.bedroom_lights",
  72.                 brightness: brightness,
  73.                 transition: 1, }
  74.             };
  75.             node.status({text: "Bedroom brightness decreased" });
  76.             return msg;
  77.         }
  78.     }
  79.     if (command === "shake") {    
  80.         var r = Math.floor(Math.random() * 256);
  81.         var g = Math.floor(Math.random() * 256);
  82.         var b = Math.floor(Math.random() * 256);
  83.         var rgb_out = [r,g,b];
  84.         msg.payload = {domain: "light", service: "turn_on",
  85.             data: {
  86.                 entity_id: "light.bedroom_lights",
  87.                 rgb_color: rgb_out,
  88.                 transition: 1, }
  89.             };
  90.         node.status({text: "Bedroom lights to random RGB" });
  91.         return msg;        
  92.     }
  93.     if (command === "slide") {
  94.         //NOT USED YET. Maybe return to white or cycle defined RGB.
  95.         node.status({text: "side 1 slide" });
  96.         return  
  97.     }
  98. }
  99.  
  100. node.status({text: "Side " + side_facing + " " + command + "; no command set" });
  101. return
Add Comment
Please, Sign In to add comment