Jump to content
Visit us at IFA2024 / Besuche uns auf der IFA2024 06.-10.09.2024 Stand H1.2-420 ×
Shelly wiring diagram Now LIVE ×

Measuring outdoor air temperature - how to be accurate


Recommended Posts

When we want to measure the outside air temperature, we usually have a lot of trouble finding a place in the garden where we can:
- is in the shade all day, never in the sun
- is not next to a wall or fence,
- there is electricity and,
- there is WiFi.


If these conditions are not met, we will not have accurate thermometry, especially during the day and in sunny weather. And with inaccurate outdoor temperatures, it's not worth automating.

Although there is a weather widget in Shelly's premium service, unfortunately I cannot work with the temperature data displayed there. Or at least I couldn't find it, but maybe I'm just lame 🙂 If so, the topic can be closed.


So yes, the solution is to take the local temperature data from one of the cloud platforms and pass it to a virtual button. Here is a script that works with openweather, but of course it can be used to retrieve temperature data from any other weather website:

First create a Text virtual component, let the type be Label. If it's your first virtual component, its ID will probably be 200, that's what the script has, so you don't need to change it.

What you need is the key to the openweather API, after registration you get one from them for free, then you need to specify the city and country from which to load the outside temperature.

The script updates every 15 minutes, but this can be adjusted in the TimerSet for those who prefer a different interval. For debugging purposes, there are a few console logs included. You will only see these if you log into the device on the local network and enable websocket debug in the settings/debug section.

 

// API key and city settings
let api_key = "OPENWEATHERAPIKEY"; //  OpenWeatherMap API key
let city = "Hajduboszormeny,hu"; // Change it to your city, country


// Virtual component id ID
let component_id = 200;

// Function to fetch weather data from OpenWeatherMap
function fetchWeatherData() {
    print("Starting fetchWeatherData function");
    
    // URL az OpenWeatherMap API kéréshez
    let url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api_key;

    Shelly.call("HTTP.GET", { url: url }, function (result, error_code, error_message) {
        if (error_code !== 0) {
            print("Error fetching weather data: " + error_message);
            print("Error code: " + error_code);
        } else {
            print("HTTP GET request successful. Parsing result...");
            try {
                let weatherData = JSON.parse(result.body);
                let temperature = weatherData.main.temp;
                print("External temperature in your city: " + temperature + "°C");
       

         Shelly.call(
                  "Number.Set",
                  {
                    "id": component_id,
                    "value": temperature
                  },
                  function(result, error_code, error_message) {
                    if (error_code !== 0) {
                      print("An error occurred when updating the virtual component: " + error_message);
                    } else {
                      print("Virtual componentupdated. Value: " + temperature);
                    }
                  }
                );
            } catch (e) {
                print("Error parsing weather data: " + e.message);
                print("Received data: " + result.body); // Print the actual data received for debugging
            }
        }
    });
}

// Fetch weather data every 15 min
Timer.set(900000, true, function () {
    print("Timer triggered. Fetching weather data...");
    fetchWeatherData();
});

// Initial fetch to verify script is working
print("Initial fetch to verify script is working...");
fetchWeatherData();

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

Dear Heinz!

I'm sorry, but my answer was incorrect, so:

- The script will run on any script capable device: Plus/Gen2 - Gen3 - Pro devices
- Virtual Components works only in Gen3 devices now, and the Pro devices will get it later

So you get a nice icon only in Gen3 devices at the moment, but you can work with the data in scipt in any script capable devices.

 

The first icon in the virtual component's tab  is the openweather outside temperature:

shelly-fronius-virtual-components-icon.thumb.jpg.02ff479d6c5c93806df119afd5435a51.jpg

  • Thanks 1
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...