Gábor Tályai Posted July 17 Posted July 17 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(); 1 1 Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Heinz Posted July 19 Posted July 19 Awsome this looks epic. What shelly did you run this on ? Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Gábor Tályai Posted July 20 Author Posted July 20 Thanks 🙂 Shelly 1 Gen3, but it works on any Shelly device that can run scripts. As far as I know, Plus/Gen2 - Gen3 - Pro devices 1 Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Heinz Posted July 22 Posted July 22 I will give it a go at some point when I have time Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Gábor Tályai Posted July 22 Author Posted July 22 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: 1 Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.