Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ha = global.get('homeassistant').homeAssistant
- //Currently uses a Blueprint that enables automation for the Aqara cube.
- //Because the cube doesn't say what side is facing up, an input_helper
- //is used to remember the last side facing.
- //This ability will need to be incorporated into the function at some
- //point so the Blueprint will not be required.
- var side_facing = parseInt(ha.states["input_number.cube_helper"].state);
- var command = msg.payload.event.command
- //The "knock" command is not reliable and should not be used!
- //UPDATE: It seems to work if I "double-tap" the cube on my couch cushion.
- if (command === "knock") {
- node.status({text: "Knock detected!" });
- return
- }
- if (command === "wake") {
- node.status({text: "Cube awakened" });
- return
- }
- if (command === "flip") {
- var activated_face = msg.payload.event.args.activated_face;
- if (activated_face === 1) {
- node.status({text: "Turned on bedroom lights" });
- msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: "light.bedroom_lights",
- transition: 1, }
- };
- return msg; }
- if (activated_face === 4) {
- node.status({text: "Turned off bedroom lights" });
- msg.payload = {domain: "light", service: "turn_off",
- data: {
- entity_id: "light.bedroom_lights",
- transition: 1, }
- };
- return msg
- }
- node.status({text: "New side: " + activated_face + "; no command" });
- return
- }
- if (side_facing === 1) {
- if (command === "rotate_right" || command == "rotate_left") {
- var brightness = ha.states["light.bedroom1"].attributes.brightness;
- brightness = parseInt(brightness);
- if (command === "rotate_right") {
- if (brightness < 204) {
- brightness = brightness + 50;
- } else {
- brightness = 255;
- }
- msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: "light.bedroom_lights",
- brightness: brightness,
- transition: 1, }
- };
- node.status({text: "Bedroom brightness increased" });
- return msg;
- }
- if (command === "rotate_left") {
- if (brightness < 204) {
- brightness = brightness + 50;
- } else {
- brightness = 255;
- }
- msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: "light.bedroom_lights",
- brightness: brightness,
- transition: 1, }
- };
- node.status({text: "Bedroom brightness decreased" });
- return msg;
- }
- }
- if (command === "shake") {
- var r = Math.floor(Math.random() * 256);
- var g = Math.floor(Math.random() * 256);
- var b = Math.floor(Math.random() * 256);
- var rgb_out = [r,g,b];
- msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: "light.bedroom_lights",
- rgb_color: rgb_out,
- transition: 1, }
- };
- node.status({text: "Bedroom lights to random RGB" });
- return msg;
- }
- if (command === "slide") {
- //NOT USED YET. Maybe return to white or cycle defined RGB.
- node.status({text: "side 1 slide" });
- return
- }
- }
- node.status({text: "Side " + side_facing + " " + command + "; no command set" });
- return
Add Comment
Please, Sign In to add comment