Jump to content
Gen2 Devices: FW Update Required / Gen2 Geräte: FW-Update erforderlich ×

Control ESPhome devices using WebServer REST API


Recommended Posts

  • Members

After small modification of @Dimitar provided Tuya/HA control code to directly connect and manage Mitsubishi HVAC what have ESPhome wifi board in serial port (CN105) and Webserver running so device REST API is available 

Plus PM mini gen3 monitor HVAC power consumption and no also have management UI for HVAC device
image.png.5b05c0a4d27021f7e55a4b5e613a5565.png

Virtual components can be done with few RPC calls. Change "shelly_g3_ip" to point shelly devices IP
if links to icons are not working, those can be adjusted later from WebUI

Create virtual components:

http://shelly_g3_ip/rpc/Virtual.Add?type="enum"
http://shelly_g3_ip/rpc/Virtual.Add?type="number"
http://shelly_g3_ip/rpc/Virtual.Add?type="number"
http://shelly_g3_ip/rpc/Virtual.Add?type="group"

Set parameters to components:

http://shelly_g3_ip/rpc/Enum.SetConfig?id=200&config={"name":"HVAC_State","options":["FAN_ONLY","HEAT","COOL","OFF"],"meta":{"ui":{"view":"dropdown","titles":{"FAN_ONLY":"Fan","HEAT":"Heat","COOL":"Cool","OFF":"Off"},"icon":"https://tmpfiles.nohat.cc/full-m2i8N4A0d3m2G6b1.png","images":{"FAN_ONLY":null,"OFF":null,"HEAT":null,"COOL":null}}},"persisted":true,"default_value":"FAN_ONLY"}

http://shelly_g3_ip/rpc/number.SetConfig?id=200&config={"name":"CurrentTemperature","min":16,"max":30,"meta":{"ui":{"view":"label","unit":"°C","step":1,"icon":"https://tmpfiles.nohat.cc/full-m2i8A0m2N4m2m2i8.png"}},"persisted":true,"default_value":22}

http://shelly_g3_ip/rpc/number.SetConfig?id=201&config={"name":"TargetTemperature","min":16,"max":28,"meta":{"ui":{"view":"slider","unit":"°C","step":1,"icon":"https://tmpfiles.nohat.cc/nohat-kpng-1559442694.png"}},"persisted":false,"default_value":24}

Group components:

http://shelly_g3_ip/rpc/Group.SetConfig?id=200&config={"name":"Mitsubishi HVAC","meta":{}}
http://shelly_g3_ip/rpc/Group.Set?id=200&value=["enum:200","number:201","number:200"]

script to add:

// ESPhome devices must have webserver running
// to get device domain and entityID use http://device_ip/events 

const espDeviceUrl = 'http://esphome_device_ip/';// change correct ESPhome device IP
const domain = 'climate'; // change correct ESPhome Domain
const entityId = 'livingroom_hvac'; // change correct ESPhome ID

const headers = {
 "Content-Type": "application/json",
};
const url = espDeviceUrl + domain + '/' + entityId;

function checkStatus() {
  Shelly.call("http.request", {
    method: "GET",
    url: url,
    headers: headers
}, function (response, error_code, error_message) {
    if (response && response.code === 200 && response.body) {
        let responseBody = JSON.parse(response.body);
        if (responseBody.hasOwnProperty('mode')) {
           let state = responseBody.mode;
            print("state: " + state);
            Shelly.call("enum.set", { id: 200, value: state });
        }
        // Print 'current_temperature' and 'temperature' from attributes
        if (responseBody) {
            if (responseBody.hasOwnProperty('current_temperature')) {
                let currenttemp = parseFloat(responseBody.current_temperature);
                print("current_temperature: " + currenttemp);
                Shelly.call("number.set", { id: 200, value: currenttemp });
            }
            if (responseBody.hasOwnProperty('target_temperature')) {
                let targettemp = parseFloat(responseBody.target_temperature);
                print("temperature: " + targettemp);
                Shelly.call("number.set", { id: 201, value: targettemp });
            }
        }
    } else {
        print("Error Code: " + error_code + " - Message: " + error_message);
    }
    });
}

// Function to change the HVAC state
function setControl(newValue,type) {
  const controlUrl = url + '/set?'+ type + '=';
  
  Shelly.call("http.post", {
        url: controlUrl + newValue,
        headers: headers,
        body: JSON.stringify({
            id: domain + '-' + entityId,
            type: newValue
        })
    });
}

Shelly.addStatusHandler(function(event) {
  if (event.component === "number:201") {
      let newTemp = Shelly.getComponentStatus("number", 201)["value"];
      setControl(newTemp,"target_temperature");
    }
  if (event.component === "enum:200") {
      let newMode = Shelly.getComponentStatus("enum", 200)["value"];
      setControl(newMode,"mode");    
  }
});

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


 

  • Like 1
  • Thanks 3
Link to comment
Share on other sites

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.

×
×
  • Erstelle neue...