[Tuto]Météo Yahoo

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
Répondre
fratton
Messages : 150
Inscription : 19 août 2015, 16:38

[Tuto]Météo Yahoo

Message par fratton »

Si comme moi vous n'êtes pas satisfait des infos météo obtenu par le capteur virtuel Weather Underground (qui se base sur des stations météo non professionnelle), voici un script PHP pour récupérer les infos météo de Yahoo qui à l'avantage de proposer un flux RSS.

il vous faut PHP5, sur Raspberry :

Code : Tout sélectionner

sudo apt-get update
sudo apt-get install php5
Dans domoticz, créer 2 capteurs virtuels et récupérer leur numéro d'index :
Un capteur Température, Hydrométrie, Pression atmosphérique, IDX = 99 dans l'exemple ci-dessous
Un capteur Vent, IDX = 100 dans l'exemple ci-dessous

Il vous faut ensuite le code WOEID de votre lieu d'habitation, pour le trouver : http://woeid.rosselliot.co.nz/
Par exemple : 615702 pour Paris

créer un fichier meteo.php en modifiant les variables :
$WOEID, $IPPORT et les $IDX

Code : Tout sélectionner

<?php
// Code WOEID à récupérer sur woeid.rosselliot.co.nz
$WOEID="615702";

// Adresse et port de Domoticz
$IPPORT="192.168.1.2:8080";

// Index Domoticz du capteur de température Humidité et Pression atmosphérique
$IDX_THP=99;

// Index Domoticz du capteur de Vent
$IDX_VENT=100;


$result = file_get_contents("http://xml.weather.yahoo.com/forecastrss?w=$WOEID&u=c");
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->channel->xpath('yweather:location');
$wind =  $xml->channel->xpath('yweather:wind');
$atmosphere = $xml->channel->xpath('yweather:atmosphere');
$astronomy = $xml->channel->xpath('yweather:astronomy');
$units = $xml->channel->xpath('yweather:units');
$condition = $xml->channel->item->xpath('yweather:condition');

$location = $location[0];
$wind =  $wind[0];
$atmosphere = $atmosphere[0];
$astronomy = $astronomy[0];
$units = $units[0];
$condition = $condition[0];

// Capteur de Température Humidité et Pression atmosphérique dans Domoticz
$rTemp=$condition['temp'];
$rPres=$atmosphere['pressure'];
$rHumi=$atmosphere['humidity'];
$majdomoticz="http://$IPPORT/json.htm?type=command&param=udevice&idx=$IDX_THP&nvalue=0&svalue=$rTemp;$rHumi;0;$rPres;0";
echo file_get_contents($majdomoticz);

// Capteur de vent
$rVent=$wind['speed'];
$rDirec=$wind['direction'];
$rTempRessentie=$wind['chill']; ; // Temperature Windchill
$majdomoticz="http://$IPPORT/json.htm?type=command&param=udevice&idx=$IDX_VENT&nvalue=0&svalue=$rDirec;N;$rVent;0;$rTemp;$rTempRessentie";
echo file_get_contents($majdomoticz);
?>
Exécuter le script par la commande :

Code : Tout sélectionner

php5 -n meteo.php
Il ne reste plus qu'a exécuter la le script php toute les 5 minutes, par exemple par un cron.
Raspberry Pi B+ / RF-Link 433MHz / divers device maison 433MHz et Wifi
heartblood
Messages : 189
Inscription : 13 août 2015, 09:50

Re: Météo Yahoo

Message par heartblood »

Cool! Merci! Je testerais ça pour voir la différence avec weather underground
atlantis007
Messages : 65
Inscription : 11 janv. 2015, 22:31
Localisation : biarritz (64)

Re: Météo Yahoo

Message par atlantis007 »

bonjour
le script fonctionne très bien
mais j ai pb avec le crontab il ne se lance pas toute les 5 minute


*/5 * * * * /home/pi/domoticz/scripts/meteo.php
fratton
Messages : 150
Inscription : 19 août 2015, 16:38

Re: Météo Yahoo

Message par fratton »

Heu, il te manque ça :

*/5 * * * * php5 -n /home/pi/domoticz/scripts/meteo.php
Raspberry Pi B+ / RF-Link 433MHz / divers device maison 433MHz et Wifi
baumansp
Messages : 6
Inscription : 17 juil. 2016, 20:37
Localisation : Belgique - Leglise

Re: Météo Yahoo

Message par baumansp »

Pour information Google a fermé l'accès à cette API en mars 2016

https://www.igorkromin.net/index.php/20 ... ppling-it/
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Re: Météo Yahoo

Message par papoo »

baumansp a écrit :Pour information Google a fermé l'accès à cette API en mars 2016
ce ne serait pas plutot yahoo qui aurait fermé l'accès?
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
baumansp
Messages : 6
Inscription : 17 juil. 2016, 20:37
Localisation : Belgique - Leglise

Re: Météo Yahoo

Message par baumansp »

papoo a écrit :
baumansp a écrit :Pour information Google a fermé l'accès à cette API en mars 2016
ce ne serait pas plutot yahoo qui aurait fermé l'accès?
Oui :) j'avais la tête ailleurs
kronotops
Messages : 5
Inscription : 09 août 2016, 19:19
Localisation : Sauzet (30)

Re: Météo Yahoo

Message par kronotops »

Bonjour tout le monde,

je remonte un peu le sujet car en fouillant un peu partout j'ai réussi à trouver un lien jquery exploitable par la suite... par contre mes compétences en codage ne sont pas encore assez poussées pour ça donc voici le lien des fois que quelqu'un arrive à sortir un LUA ou Python avec :

https://query.yahooapis.com/v1/public/y ... ormat=json

où les ...... correspondent au code woeid. Exemple pour chez moi :
https://query.yahooapis.com/v1/public/y ... ormat=json

je continue de bosser dessus et vous tiens au courant si j'arrive à quelque chose... :D
Raspberry Pi 3 + RFLink + Domoticz
Prise mural Chacon
Module ECR-100 avec fil pilote
Module température + humidité Alecto v4 Banggood
Téléinfo sans-fil en cours de réalisation
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

LUA : Météo Yahoo

Message par papoo »

j'ai pas mal galéré pour arriver à faire fonctionner quelque chose en lua

mais ça fonctionne
l'api ne donne pas les unités métriques il m'a fallu convertir
temp yahoo.png
temp yahoo.png (13.04 Kio) Consulté 15425 fois
à toi de jouer si tu souhaite ajouter d'autres infos
log yahoo.png
log yahoo.png (14.85 Kio) Consulté 15425 fois

Code : Tout sélectionner

--[[
name : script_time_yahoo.lua
auteur : papoo
version : 1.00
maj : 10/08/2016
date : 10/08/2016
 Ce script a pour but de remonter les informations météo du site https://www.yahoo.com/news/weather/ sur domoticz 
 URL post : http://easydomoticz.com/forum/viewtopic.php?f=17&t=1078#p20931
]]--
--------------------------------------------
------------ Variables à éditer ------------
-------------------------------------------- 
local WOEID='608140'  --L'API yahoo necessite un code WOEID. Pour trouver le code WOEID de votre lieu d'habitation, rendez vous sur : http://woeid.rosselliot.co.nz/

local dz_temp_hu_pre = 739   -- renseigner l'id du device Température Humidité et Pression atmosphérique associé si souhaité, sinon nil
local debugging = true     -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir

--------------------------------------------
----------- Fin variables à éditer ---------
-------------------------------------------- 
local indexArray=0   

--------------------------------------------
---------------- Fonctions -----------------
-------------------------------------------- 
function voir_les_logs (s)
    if (debugging) then 
      if s ~= nil then
        print ("<font color='#f3031d'>".. s .."</font>");
      else
      print ("<font color='#f3031d'>aucune valeur affichable</font>");
      end
    end
end   

function round(value, digits)
  local precision = 10^digits
  return (value >= 0) and
      (math.floor(value * precision + 0.5) / precision) or
      (math.ceil(value * precision - 0.5) / precision)
end

function fahrenheit_to_celsius(fahrenheit, digits) 
    if fahrenheit ~= nil then
   return round((5/9) * (fahrenheit - 32), digits or 2)
   else
   print ("<font color='#f3031d'>pas de fahrenheit a convertir en celsius</font>");
   end
end
function miles_to_km(miles, digits) 
    if miles ~= nil then
   return round((miles * 1.609344), digits or 2)
   else
   print ("<font color='#f3031d'>pas de miles a convertir en km</font>");
   end
end

--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------

commandArray = {}
now=os.date("*t")
if now.min % 5 == 0 then  -- exécution toutes les 5 minutes
voir_les_logs("script_time_yahoo.lua",debugging)


      local fname ="/tmp/yahoo.xml"
      os.execute("curl 'http://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid=".. WOEID .."&format=xml' > ".. fname .." && chmod 777 " .. fname)
      
      
      -- voir_les_logs("Fichier local : " ..fname,debugging)
      local f = io.open(fname, "r")

          if f == nil then
          print("Error opening file '" .. fname .. "'.")
          os.exit(1)
          end
 
      local xml = f:read("*all")
               f:close()


   for instance in xml:gmatch("<channel>(.-)</channel>") do 
   
   local distance_unit, pressure_unit, speed_unit, temp_unit = instance:match('<yweather:units.*distance="(.-)".*pressure="(.-)".*speed="(.-)".*temperature="(.-)"/>')
   local city, country, region = instance:match('<yweather:location.*city="(.-)".*country="(.-)".*region="(.-)"/>')
   local wind_chill, wind_direction, wind_speed = instance:match('<yweather:wind.*chill="(.-)".*direction="(.-)".*speed="(.-)"/>')
   local humidity, pressure= instance:match('<yweather:atmosphere.*humidity="(.-)".*pressure="(.-)"')
   local sunrise, sunset = instance:match('<yweather:astronomy.*sunrise="(.-)".*sunset="(.-)"/>')
   local temp = instance:match('<yweather.*condition.*temp="(.-)"')

   voir_les_logs("--- --- --- unite distance= "..distance_unit..", unite pression = "..pressure_unit..", unite vitesse = "..speed_unit..", unite temperature = "..temp_unit,debugging)

         voir_les_logs("--- --- --- Ville = "..city..", Pays = "..country..", Region = "..region,debugging)
         voir_les_logs("--- --- --- Temperature Ressentie = "..fahrenheit_to_celsius(wind_chill,1)..", direction du vent = "..wind_direction..", vitesse du vent = "..miles_to_km(wind_speed,1),debugging)
         voir_les_logs("--- --- --- humidite = "..humidity..", pression = "..pressure,debugging)
         voir_les_logs("--- --- --- lever soleil ="..sunrise..", coucher soleil = "..sunset,debugging)
         voir_les_logs("--- --- --- Temperature = "..fahrenheit_to_celsius(temp,1),debugging)


       if dz_temp_hu_pre ~= nil then  
          commandArray[indexArray] = {['UpdateDevice'] = dz_temp_hu_pre..'|0|'..fahrenheit_to_celsius(temp,1)..';'..humidity..';0;'..pressure..';0;'}
          indexArray=indexArray+1
       end

   end   


end
return commandArray
Dernière modification par papoo le 17 août 2016, 14:17, modifié 1 fois.
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
kronotops
Messages : 5
Inscription : 09 août 2016, 19:19
Localisation : Sauzet (30)

Re: Météo Yahoo

Message par kronotops »

Impeccable !!! C'est parfait tout fonctionne correctement :D Merci beaucoup.
Raspberry Pi 3 + RFLink + Domoticz
Prise mural Chacon
Module ECR-100 avec fil pilote
Module température + humidité Alecto v4 Banggood
Téléinfo sans-fil en cours de réalisation
Répondre