Page 1 of 1

Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 11:12
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

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 11:46
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.

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:04
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

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:07
by Egregius
Why not display a PHP page? I would find that much easier, don't know anything from Javascript.

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

Posted: Sunday 20 March 2016 12:11
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

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:21
by gizmocuz
Why not using the build in WU ? If you miss something you can always add it, and make a pull request ?

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:23
by woody4165
Can you explain more? I am a newbie in this.
Thanks

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:24
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

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:53
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.

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 12:58
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

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 13:02
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.

Re: Forecast values from Weather Underground in PHP

Posted: Sunday 20 March 2016 13:06
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!

Re: Forecast values from Weather Underground in PHP

Posted: Saturday 02 April 2016 11:05
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>';

Re: Forecast values from Weather Underground in PHP

Posted: Saturday 02 April 2016 20:53
by woody4165
How can I add this in a frontpage.html on a "onclick" clicking this?
Image