Jump to content
NOTICE / HINWEIS: iOS 18 Update and Today Widgets ×

HA to Shelly APP


Dimitar

Recommended Posts

Interesting script I make today. 

You can use HomeAssistant to unify App access to any device ZigBee, Tuya, Thread in all integrations and you then you can control device from Shelly App. Still working on it but hope during Xmas will be ready. You are welcome to comment, I may open a github repository where everyone can commit and make changes. 

Link to comment
Share on other sites

And it's done. Tuya enabled Air-conditioner connected to HA with Tuya Integration and controlled from Shelly App via Shelly Gen3 device. Crazy!image.thumb.png.adae117fe2abd89677ffdc2a4b8db0bd.png

END TO END REACTION TIME < 1 SEC 🙂

image.thumb.png.2bc723201bd7d4a7471e202e62a74cae.png          image.thumb.png.705117ce74f9f93be483b04607dd5c28.png

image.thumb.png.be71f8abad2ed3b00c2348ff1d214de5.png

You can see what Virtual components must be created. 

This is the script: 

// Replace these with your actual Home Assistant URL and access token
let  homeAssistantUrl = 'IP:port';
let  accessToken = "ACCESS Token";
let entityId = 'Air.conditioner.ID'; // Replace with the actual entity ID

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

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

function checkStatus() {
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_temperature' and 'temperature' from attributes
        if (responseBody.attributes) {
            if (responseBody.attributes.hasOwnProperty('current_temperature')) {
                let currenttemp = JSON.stringify(responseBody.attributes.current_temperature);
                print("current_temperature: " + currenttemp);
                Shelly.call("number.set", { id: 200, value: currenttemp });
            }
            if (responseBody.attributes.hasOwnProperty('temperature')) {
                let targettemp = JSON.stringify(responseBody.attributes.temperature);
                print("temperature: " + targettemp);
                Shelly.call("number.set", { id: 201, value: targettemp });
            }
        }
    } else {
        // Handle errors
        print("Error Code: " + error_code + " - Message: " + error_message);
    }
 });
}

// Function to change the temperature
function setTemperature(newTemp) {
    const temperatureUrl = homeAssistantUrl + "api/services/climate/set_temperature";

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

function setHvacMode(mode) {
    const hvacModeUrl = homeAssistantUrl + "api/services/climate/set_hvac_mode";

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

Shelly.addStatusHandler(function(event) {
//  print (event.component);
  if (event.component === "enum:200") {
      let newmode = Shelly.getComponentStatus("enum", 200)["value"];
      setHvacMode(newmode);    
  }
  if (event.component === "number:201") {
      let newTemp = Shelly.getComponentStatus("number", 201)["value"];
      setTemperature(newTemp);
    }
});

Timer.set(
  /* number of miliseconds */ 5000,
  /* repeat? */ true,
  /* callback */ checkStatus
);

Enjoy

image.png

 

Link to comment
Share on other sites

  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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...