Jump to content
Shelly IoT Innovation Challenge started! ×

Example script for Thermostat which use virtual components


Dimitar

Recommended Posts

To work you need to create virtual same virtual components and then change ID in the script: 

let CONFIG = {
  accuWeatherAPIKEY: "YOUR KEY",
  weatherForecastEndpoint:
    "http://dataservice.accuweather.com/forecasts/v1/daily/1day/",
  weatherCurrentEndpoint:
    "http://dataservice.accuweather.com/currentconditions/v1/",
  locations: {
    Chicago: 348308,
    Miami: 347936,
    Mosfellsbaer: 190395,
    Sofia: 51097
  },
};

let checkInterval = 10000;
let timerID = null;
let roomtemp = 0;

//Event register
Shelly.addEventHandler(function (event) {
  if (event.name === "script" && event.id === 5) {
     roomtemp = event.info.data.temp;
     Shelly.call("number.set", { id: 204, value: roomtemp });
     print ("Room temp: " + roomtemp);
  }
});


// Initialize timer outside the updateSettings function
timerID = Timer.set(checkInterval, true, function() {
 // console.log("Checking weather");
  let city = Shelly.getComponentStatus("enum", 200)["value"];
 // print(city);
  TemperatureControlLocation(city);
});

// Add a new endpoint to serve the form for updating the API key
HTTPServer.registerEndpoint("update-api-key-form", function(req, res) {
  let htmlForm = '<html>' +
    '<head>' +
      '<style>' +
        'body {' +
          'background-color: #11191f;' +
          'font-family: Arial, sans-serif;' +
          'text-align: center;' +
          'padding-top: 50px;' +
        '}' +
        'form {' +
          'background: #C0C0C0;' +
          'padding: 20px;' +
          'border-radius: 8px;' +
          'box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);' +
          'display: inline-block;' +
          'margin-top: 20px;' +
        '}' +
        'input, button {' +
          'margin-bottom: 15px;' +
          'padding: 10px;' +
          'width: 250px;' +
          'border-radius: 4px;' +
          'border: 1px solid #ddd;' +
        '}' +
        'button {' +
          'background-color: #007bff;' +
          'color: white;' +
          'border: none;' +
          'cursor: pointer;' +
          'width: 270px;' +
        '}' +
        'button:hover {' +
          'background-color: #0056b3;' +
        '}' +
        'label {' +
          'display: block;' +
          'margin-bottom: 5px;' +
        '}' +
      '</style>' +
    '</head>' +
    '<body>' +
      '<div style="text-align: center;">' +
        '<img src="https://info.shelly.cloud/wp-content/uploads/2021/07/shelly_logo_blue.png" alt="Shelly Logo" style="max-width: 200px; margin-top: 20px;">' +
      '</div>' +
      '<form id="updateForm" action="/script/1/update-settings" method="post" style="margin: auto; width: 300px;">' +
        '<label for="apikey"><img src="http://minhome.net:8080/key.png" alt="Key Icon" class="key-icon">AccuWeather API Key:</label>' +
        '<input type="text" id="apikey" name="apikey" value="' + CONFIG.accuWeatherAPIKEY + '"><br>' +
        '<label for="checkInterval">Check Interval (ms):</label>' +
        '<input type="number" id="checkInterval" name="checkInterval" value="' + checkInterval + '"><br>' +
        '<button type="submit">Update Settings</button>' +
      '</form>' +
'<div style="margin-top: 20px;">' +
  '<a href="#" onclick="this.href=\'http://\' + window.location.hostname + \'/#\'" style="text-decoration: none; color: inherit;">' +
    '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">' +
            '<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>' +
            '<polyline points="9 22 9 12 15 12 15 22"></polyline>' +
          '</svg>' +
        '</a>' +
  '<a href="#" onclick="this.href=\'http://\' + window.location.hostname + \'/#/script/1\'" style="text-decoration: none; color: inherit; margin-left: 10px;">' +
    '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-code">' +
            '<polyline points="16 18 22 12 16 6"></polyline>' +
            '<polyline points="8 6 2 12 8 18"></polyline>' +
          '</svg>' +
        '</a>' +
      '</div>' +
    '</body>' +
  '</html>

 

image.thumb.png.7b1e29c7ad83a7289638a3510d23217c.png

Link to comment
Share on other sites

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