Forecast values from Weather Underground in PHP

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Forecast values from Weather Underground in PHP

Post by woody4165 »

Hi all

I saw, if I am not wrong, that in the Weather Underground dummy devices there isn't any information regarding forecast for next days.

Starting from the php found in the wiki for Automatic Weather forecast tweet (https://www.domoticz.com/wiki/Automatic ... P_version) I've made a php that will collect the forecast data using the "forecast" data features and getting the name of the day of the forecast, the forecast type (sunny, partlycloudy,...) and also the icon that WU use for that forecast type.

Of course, the data grabbed can be more than mine, accordingly to what's inside the data from WU.

What I would like to show in my frontpage is three icons with the forecast for the next three days.

The code is here:

Code: Select all

    #!/usr/bin/php
    <?php

    //---get forecast for the next three days -------------------------
    //---see the wunderground webpage for other weather information download
    //---uselang:xx for language of your country
    //---change hourly to other values for other weather information
    //---change country and city after q
    $json_string = file_get_contents("http://api.wunderground.com/api/xxxxxxxxxx/forecast/lang:EN/q/locid:xxxxxxxx;loctype:1.json");
    $parsed_json = json_decode($json_string, true);
 
 
    //---for debugging and development of own forecast combination
    //---this file will show the complete array of a weather forecast
    
    //$wetterdatei = "/home/pi/domoticz/scripts/wetter_json.txt";
    //$wd = fopen ($wetterdatei, "w+");
    //fwrite ($wd, print_R($parsed_json, TRUE));
    //fclose ($wd);
    
 
    //--------- Weather in xh ------------------------------------
    //---look at the wetter_json.txt file about the structure
    
    $parsed_json = $parsed_json['forecast']['txt_forecast'];
    //print_r($parsed_json);
 
    $date_forecast = $parsed_json['date'];
    echo $date_forecast."\n";
    

    $parsed_json = json_decode($json_string, true);
    $parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][0];
    $icon1 = $parsed_json['icon'];
    $icon_url1 = $parsed_json['icon_url'];
    $day1 = $parsed_json['title'];
    echo $icon1.":".$day1."\n";
    
    $parsed_json = json_decode($json_string, true);
    $parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][2];
    $icon2 = $parsed_json['icon'];
    $icon_url2 = $parsed_json['icon_url'];    
    $day2 = $parsed_json['title'];
    echo $icon2.":".$day2."\n";
   
    
    $parsed_json = json_decode($json_string, true);
    $parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][4];
    $icon3 = $parsed_json['icon'];
    $icon_url3 = $parsed_json['icon_url'];    
    $day3 = $parsed_json['title'];
    echo $icon3.":".$day3."\n";
   
     ?>
Starting from a frontpage made with html and json, also this grabbed from a thread in this forum from Gerard33 (thanks!!!), how may I call the php and get the values in the variables from php to json to evaluate with if clause?

I'm not very good in coding, just some, and I am not aware of how to do it.

Can you help on this?

Thanks
Woody
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Forecast values from Weather Underground in PHP

Post by Egregius »

The code could definnitely use some cleaning.
Why is there so many times "$parsed_json = json_decode($json_string, true);"?
One time should be enough.

I think (didn't test it) this code should do the same:

Code: Select all

#!/usr/bin/php
<?php
$parsed_json = json_decode(file_get_contents("http://api.wunderground.com/api/xxxxxxxxxx/forecast/lang:EN/q/locid:xxxxxxxx;loctype:1.json"), true);
echo $parsed_json['forecast']['txt_forecast']['date']."\n";
$icon1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['icon'];
$icon_url1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['icon_url'];
$day1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['title'];
echo $icon1.":".$day1."\n";
$icon2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['icon'];
$icon_url2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['icon_url'];   
$day2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['title'];
echo $icon2.":".$day2."\n";
$icon3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['icon'];
$icon_url3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['icon_url'];   
$day3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['title'];
echo $icon3.":".$day3."\n";
Now, what do you want to do with the data? Don't understand your last paragraph.
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

Thanks for the cleaning I will test it. I did it like this because the original code make something like this.

Once got all those variables I would like to use it in a Javascript code that I use in the frontpage. How can I call this php from within the Javascript and get back the variables?

Thanks
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Forecast values from Weather Underground in PHP

Post by Egregius »

Why not display a PHP page? I would find that much easier, don't know anything from Javascript.
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: RE: Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

Egregius wrote:Why not display a PHP page? I would find that much easier, don't know anything from Javascript.
Because all the frontpage is written in html5 calling Javascript that manage the layout.
Maybe I have to find the way to write the code directly in Js.
Thanks
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
gizmocuz
Posts: 2712
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Forecast values from Weather Underground in PHP

Post by gizmocuz »

Why not using the build in WU ? If you miss something you can always add it, and make a pull request ?
Quality outlives Quantity!
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

Can you explain more? I am a newbie in this.
Thanks
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
gizmocuz
Posts: 2712
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Forecast values from Weather Underground in PHP

Post by gizmocuz »

In settings, enable 'accept new hardware'
In the hardware setup, add the WU hardware and provide your API key and location
And your done
Quality outlives Quantity!
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

I have already set, but it doesn't report any forecast value, but only the actual ones. I would like to get forecast values like sunny, cloudy and show image accordingly.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Forecast values from Weather Underground in PHP

Post by Westcott »

It could be done directly in Lua - I'll have a go.
To get your own PWS forecast, try this -
http://api.wunderground.com/api/<yourAP ... WSid>.json
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

Egregius wrote:The code could definnitely use some cleaning.
Why is there so many times "$parsed_json = json_decode($json_string, true);"?
One time should be enough.

I think (didn't test it) this code should do the same:

Code: Select all

#!/usr/bin/php
<?php
$parsed_json = json_decode(file_get_contents("http://api.wunderground.com/api/xxxxxxxxxx/forecast/lang:EN/q/locid:xxxxxxxx;loctype:1.json"), true);
echo $parsed_json['forecast']['txt_forecast']['date']."\n";
$icon1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['icon'];
$icon_url1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['icon_url'];
$day1 = $parsed_json['forecast']['txt_forecast']['forecastday'][0]['title'];
echo $icon1.":".$day1."\n";
$icon2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['icon'];
$icon_url2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['icon_url'];   
$day2 = $parsed_json['forecast']['txt_forecast']['forecastday'][2]['title'];
echo $icon2.":".$day2."\n";
$icon3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['icon'];
$icon_url3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['icon_url'];   
$day3 = $parsed_json['forecast']['txt_forecast']['forecastday'][4]['title'];
echo $icon3.":".$day3."\n";
Now, what do you want to do with the data? Don't understand your last paragraph.

I tried your code, but it doesn't work.
After getting the date correctly, I get this error for each of the parsed_json line:

PHP Notice: Undefined index: forecast in /home/pi/domoticz/scripts/forecast.php on line 29


Thanks anyway.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

Westcott wrote:It could be done directly in Lua - I'll have a go.
To get your own PWS forecast, try this -
http://api.wunderground.com/api/<yourAP ... WSid>.json
It would be really nice if you make in Lua!!!

Actually don't have any weather sensor to share and create a PWS, but it's cool!
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Forecast values from Weather Underground in PHP

Post by Egregius »

Had some time to look at this again...

Code: Select all

<?php
$parsed_json = json_decode(file_get_contents("http://api.wunderground.com/api/c12345fe1234567e/forecast/q/BX/Beitem.json"), true);

echo "<table cellspacing='20px'>";
echo "<tr valign='middle'><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][0]['date']['weekday']."</td><td><img src='".$parsed_json['forecast']['simpleforecast']['forecastday'][0]['icon_url']."'/></td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][0]['low']['celsius']."-".$parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius']."°C</td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][0]['qpf_allday']['mm']." mm regen</td></tr>";
echo "<tr valign='middle'><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][1]['date']['weekday']."</td><td><img src='".$parsed_json['forecast']['simpleforecast']['forecastday'][1]['icon_url']."'/></td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][1]['low']['celsius']."-".$parsed_json['forecast']['simpleforecast']['forecastday'][1]['high']['celsius']."°C</td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][1]['qpf_allday']['mm']." mm regen</td></tr>";
echo "<tr valign='middle'><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][2]['date']['weekday']."</td><td><img src='".$parsed_json['forecast']['simpleforecast']['forecastday'][2]['icon_url']."'/></td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][2]['low']['celsius']."-".$parsed_json['forecast']['simpleforecast']['forecastday'][2]['high']['celsius']."°C</td><td>".$parsed_json['forecast']['simpleforecast']['forecastday'][2]['qpf_allday']['mm']." mm regen</td></tr>";
echo "</table>";
Gives a nice output:
Image

If you add this to the end of the code you can see all possible values:

Code: Select all

echo '<pre>';print_r($parsed_json);echo '</pre>';
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Forecast values from Weather Underground in PHP

Post by woody4165 »

How can I add this in a frontpage.html on a "onclick" clicking this?
Image
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest