Jump to content

HA / MotionBlinds / Shelly


Recommended Posts

  • Members

Hi all,

I have no scripting skills at all. But with help from ChatGPT and Dimitars HA/Tuya script, I managed to make a script and virtual components to control my MotionBlinds. This is awesome! 🙂

Here's a link to the blinds, if you don't know them: https://motionblinds.com/en/

Now, I hope that we will be able to add the virtual components to our dashboard and Wall Display soon 🙂

image.thumb.png.029c4d7f4236c78a7f125b7fc0a45055.png
 

// Replace these with your actual Home Assistant URL and access token
let homeAssistantUrl = 'http://192.168.X.XX:8123/'; // Replace with your HA IP
let accessToken = "YourAccessToken"; // Replace with your HA token
let entityId = 'cover.yourcover'; // Replace with the actual entity

const headers = {
  "Authorization": "Bearer " + accessToken,
  "Content-Type": "application/json",
};

const url = homeAssistantUrl + "api/states/" + entityId;

function checkBlindStatus() {
  Shelly.call("http.request", {
    method: "GET",
    url: url,
    headers: headers
  }, function (response, error_code, error_message) {
    if (response.code === 200 && response.body) {
      let responseBody = JSON.parse(response.body);
      if (responseBody.hasOwnProperty('state')) {
        let state = responseBody.state;
        print("state: " + state);
        Shelly.call("enum.set", { id: 200, value: state });
      }
      
      // Print 'current_position' from attribute
      if (responseBody.attributes && responseBody.attributes.hasOwnProperty('current_position')) {
        let currentPosition = responseBody.attributes.current_position;
        print("Current position: " + currentPosition);
        Shelly.call("number.set", { id: 200, value: currentPosition });
        Shelly.call("number.set", { id: 201, value: currentPosition });
      }
    } else {
      // Handle errors
      print("Error checking blind status - Code: " + error_code + " - Message: " + error_message);
    }
  });
}

function setBlindPosition(position) {
  const positionUrl = homeAssistantUrl + "api/services/cover/set_cover_position";

  Shelly.call("http.request", {
    method: "POST",
    url: positionUrl,
    headers: headers,
    body: JSON.stringify({
      entity_id: entityId,
      position: position
    })
  }, function (response, error_code, error_message) {
    if (response && response.code === 200) {
      print("Blind position set to " + position);
    } else {
      print("Error setting blind position - Code: " + error_code + " - Message: " + error_message);
    }
  });
}

Shelly.addStatusHandler(function(event) {
  if (event.component === "enum:200") {
      let newmode = Shelly.getComponentStatus("enum", 200)["value"];
      if (newmode === 'open') {
          setBlindPosition(100); // Open
      } else if (newmode === 'close') {
          setBlindPosition(0); // Close
      }
  }
  if (event.component === "number:201") {
      let newPos = Shelly.getComponentStatus("number", 201)["value"];
      setBlindPosition(newPos); // Cover position
    }
});

Timer.set(
  /* number of miliseconds */ 5000,
  /* repeat? */ true,
  /* callback */ checkBlindStatus
);
Link to comment
Share on other sites

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...