Wetterstation BloomSky und Storm

Hallo zusammen

Da es noch keinen fertigen Beispielscript für die Wetterstation BloomSky (Version 1 oder 2) inkl. Storm gab, habe ich mir selber etwas programmiert. Ich habe den Script dabei so geschrieben, dass alle nötigen Variablen und Variablenprofile automatisch beim ersten Ausführen erstellt werden.

Anleitung:

[ol]
[li]BloomSky-Wetterstaion gemäss App installieren und in Betrieb nehmen[/li][li]Im BloomSky-Dashboard (BloomSky Portal Login) links unten auf Developper klicken und den API-key notieren[/li][li]Irgendwo im IP-Symcon-Baum eine neue Kategorie erstellen (bsp. BloomSky) und die ID im Script bei $ParentID eintragen[/li][li]Den API-key im Script bei der Variable $apikey eintragen[/li][li]Den Script periodisch ausführen (lassen)[/li][/ol]

Update: V0.41 Kleinigkeiten korrigiert


<?
/*
Bloomsky Sky2 + Storm
Script-Coder: Letraz 
V0.1		17.06.2017	- Initialversion
V0.2		19.06.2017	- Automatisches Anlegen der Variablen inkl. Datentyp und Unterkategorien
V0.3		21.06.2017	- Automatisches Erstellen der nicht vorhandenen Variablenprofile (Millivolt und Candela)
V0.4		22.06.2017	- Umrechnung Luftdruck von QFE(lokal) nach QNH (reduziert auf Meereshöhe)
V0.41	24.06.2017	- Timeout curl-Abfrage angepasst (hat mit automatischem Ausführen des Scriptes nicht funktioniert)
					- Luftdruck auf Float umgestellt

*/

//Initiale Konfiguration
// Den apikey erhält man vom Bloomsky-Dashboard, Einloggen und auf "Developper" klicken
$apikey = "<API-Key>";						

// Die Daten werden in der europäischen Variante abgerufen, wer amerikanische Enheiten (°F, mph, inHG, inch) will lässt "?unit=intl" im Aufruf weg
$datasource = "https://api.bloomsky.com/api/skydata/?unit=intl";	

// Hier die ID der übergeordneten Instanz angeben (bsp die ID des Verzeichnisses in welchem sich dieser Script befindet) 
// --> dort werden die Variablen beim 1. Ausführen automatisch angelegt
$ParentID = <Instanz-ID>;				

// Debug = 1 um Ausgaben vom Script auf der Konsole zu sehen
$debug = 0;															

//ab hier nichts mehr verändern
//-------------------------------------------------------------------------

  //Header definieren
  $header = array("header" => "Authorization: ".$apikey."
");
  
  $arrstring = array("CityName","UVIndex","DeviceName","BoundedPoint",'Null',"1","2","3","4","DeviceID","ImageURL","DeviceType","FullAddress","StreetName","RegisterTime","ImageTS","TS","WindDirection");
  $arrint = array("UTC","DST","NumOfFollowers","Humidity","UVIndex",);
  $arrfloat = array("RainDaily","24hRain","LON","LAT","ALT","Temperature","WindGust","SustainedWindSpeed","RainRate","Voltage","Luminance","Pressure");
  $arrbool = array("Searchable","Rain","Night");

  //Senden vorbereiten
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_VERBOSE, false);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_URL, $datasource);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  //Befehl senden
  $result = curl_exec($ch);
  if (curl_errno($ch))
  {
  	 die("Fehler aufgetreten, Returncode: ".curl_errno($ch)."
<br>");
  }
  if ($debug) var_dump(json_decode($result, true));						//Rohwerte vom Aufruf zeigen
  
  //Auswerten
  $result = json_decode($result,true);
  foreach ($result as $key => $value) 
  {
    if (is_array($value))			//alles ist in einem Array
	{ 
      foreach ($value as $message => $data) 
	  {
        if (is_array($data))		//Storm, Videolist, Videolist_C, Data, PreviewImageList ist in einem zusätzlichem Array
		{
		  foreach ($data as $wert => $inhalt)
		  {
		    if ($wert === "TS" || $wert === "ImageTS")  //Timestamp in menschenlesbares Format umwandeln
			{
			  $inhalt = date("d.m.Y H:i:s",$inhalt);
			}
			if ($inhalt == "")							//Boleanwert False auf 0 setzen
			{
			  $inhalt = 0;
			}
			if($wert === 0)								//Wenn der Variablenname 0 lautet das Wort dafür verwenden
			{
			  $wert = "Null";
			}
			
			//Unterkategorie suchen und anlegen wenn nötig
			$subParentID = @IPS_GetCategoryIDByName($message,$ParentID);
			if ($subParentID === false)
			{
			  $subkatid = IPS_CreateCategory();     	
			  IPS_SetName($subkatid, $message); 		
			  IPS_SetParent($subkatid, $ParentID);	
			  $subParentID = $subkatid;	
			}
			
			//Variable suchen und anlegen wenn nötig, Wert in Variable schreiben		
			$vid = @IPS_GetVariableIDByName($wert,$subParentID);		
    		if ($vid === false) 															
			{
			  //Variablen je nach Datentyp anlegen
			  if (in_array($wert, $arrint)) 
			  {
			    $vid = IPS_CreateVariable(1); 	//Variable vom Typ Integer
			  }
			  elseif (in_array($wert, $arrstring))
			  {
			    $vid = IPS_CreateVariable(3); 	//Variable vom Typ String
			  }
			  elseif (in_array($wert, $arrfloat))
			  {
			    $vid = IPS_CreateVariable(2); 	//Variable vom Typ Float
			  }
			  else $vid = IPS_CreateVariable(0);//Variable vom Typ Boolean
			}
			IPS_SetParent($vid, $subParentID);
      		IPS_SetName($vid, $wert);
		    SetValue ($vid,$inhalt);
			if ($debug) echo($message ." -> ".$wert." = ".$inhalt."
");
	  	  }  
		}
		else
		{
		  if ($message === "RegisterTime")				//Timestamp in menschenlesbares Format umwandeln
		  {
		 	$data." = ".date("d.m.Y H:i:s",$data);
		  }
		  if ($data == "")  							//Boleanwert False auf 0 setzen
		  {
		    $data = 0;
		  }
		  
		  //Variable suchen und anlegen wenn nötig, Wert in Variable schreiben
		  $vid = @IPS_GetVariableIDByName($message,$ParentID);		
    	  if ($vid === false) 															
		  {
		    //Variablen je nach Datentyp anlegen
			if (in_array($message, $arrint)) 
		    {
			  $vid = IPS_CreateVariable(1); 	//Variable vom Typ Integer
			}
			elseif (in_array($message, $arrstring))
			{
			  $vid = IPS_CreateVariable(3); 	//Variable vom Typ String
			}
			elseif (in_array($message, $arrfloat))
			{
			  $vid = IPS_CreateVariable(2); 	//Variable vom Typ Float
			}
			else $vid = IPS_CreateVariable(0); 	//Variable vom Typ Boolean
		  }
		  IPS_SetParent($vid, $ParentID);
      	  IPS_SetName($vid, $message);
		  SetValue ($vid,$data); 
		  if ($debug) echo($message." = ".$data."
");
	    }
      }
	} 
	else
	{
	  echo("Unerwarteter Wert: ".$key." = ".$value."
");		//Sollte nie auftraten, ausser Bloomsky ändert die API
	}
  }
								    
  curl_close($ch);

  //Kategorien-ID suchen
  $katiddata = @IPS_GetCategoryIDByName("Data",$ParentID);
  $katidstorm = @IPS_GetCategoryIDByName("Storm",$ParentID);
    
  //Umrechnungen
  //Zeitstempel "RegisterTime" (aus irgend einem Grunde funktioniert das direkt im foreach-Loop nicht
  $vid = @IPS_GetVariableIDByName("RegisterTime",$ParentID);		
  if ($vid) 															
  {
    $registertime_old = GetValue($vid);
	$registertime_new = date("d.m.Y H:i:s",$registertime_old);
	SetValue($vid,$registertime_new);
  }  
  
  //Variablenprofil zuweisen falls nötig
  if ($katiddata)
  {
    //Voltage
	$vid = @IPS_GetVariableIDByName("Voltage",$katiddata);		
    if ($vid) 															
    {
      //in Volt umrechnen
	  SetValue($vid,GetValue($vid) / 1000); 
	  
	  $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "Voltage")
	  {
	    if ($debug) echo("Voltage hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    //Prüfen ob das Variablenprofil bereits existiert ansonsten anlegen
		$varprofiles = IPS_GetVariableProfileList ();
		if (!in_array("Voltage", $varprofiles)) 
		{
		  if ($debug) echo("Profile Voltage nicht vorhanden
");
		  IPS_CreateVariableProfile("Voltage", 2);
		  IPS_SetVariableProfileText("Voltage", "", " V");
		  IPS_SetVariableProfileDigits("Voltage", 3);
		}
		IPS_SetVariableCustomProfile($vid, "Voltage");
	  }
    }
	
	//Feuchtigkeit
	$vid = @IPS_GetVariableIDByName("Humidity",$katiddata);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~Humidity")
	  {
	    if ($debug) echo("Feuchtigkeit hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~Humidity");
	  }
    }
    
	//Luftdruck
    $vid = @IPS_GetVariableIDByName("Pressure",$katiddata);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~AirPressure.F")
	  {
	    if ($debug) echo("Pressure hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~AirPressure.F");
	  }
	  //Luftdruck von QFE (lokal) nach QNH (Meereshöhe) umrechnen nach DWD-Formel
	  //Notwendige Werte ermitteln
	  $temp = GetValue(@IPS_GetVariableIDByName("Temperature",$katiddata));	
	  $altitude = @GetValue(IPS_GetVariableIDByName("ALT",$ParentID));
	  $tg = 0.0065; //Temperaturgradient °C/Meter
	  $pressureqfe = GetValue($vid);
	  
	  //Dampfdruck ausrechnen
	  if ($temp >= 9.1)
	  {
	    $dampfdruck = 18.2194 * (1.0463 - exp(-0.0666 * $temp));
	  }
	  else $dampfdruck = 5.6402 * (-0.0916 + exp(0.06 * $temp));
		 	  
	  //Barometrische Höhenformel
	  $exponent = (9.80665 / (287.05 * ($temp + 273.15) + 0.12 * $dampfdruck + ($tg * $altitude / 2))) * $altitude; 
	  $pressureqnh = round($pressureqfe * exp($exponent),1);
	  SetValue($vid,$pressureqnh); 
    }
    
	//Temperatur
    $vid = @IPS_GetVariableIDByName("Temperature",$katiddata);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~Temperature")
	  {
	    if ($debug) echo("Temperature hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~Temperature");
	  }
    }
	
	//Luminance neues Variablenprofil erstellen, da Einheit in cd/m2 ist
	$vid = @IPS_GetVariableIDByName("Luminance",$katiddata);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "Candela")
	  {
	    if ($debug) echo("Liminance hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    
		//Prüfen ob das Variablenprofil bereits existiert ansonszen anlegen
		$varprofiles = IPS_GetVariableProfileList ();
		if (!in_array("Candela", $varprofiles)) 
		{
		  if ($debug) echo("Profile Candela nicht vorhanden
");
		  IPS_CreateVariableProfile("Candela", 2);
		  IPS_SetVariableProfileText("Candela", "", " cd/m2");
		  IPS_SetVariableProfileDigits("Candela", 1);
		}
		IPS_SetVariableCustomProfile($vid, "Candela");
	  }
    }
  }
  
  //Wenn vorhanden Storm-werte
  if ($katidstorm)
  {
    //Regen
	$vid = @IPS_GetVariableIDByName("24hRain",$katidstorm);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~Rainfall")
	  {
	    if ($debug) echo("24hRain hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~Rainfall");
	  }
    }
    $vid = @IPS_GetVariableIDByName("RainDaily",$katidstorm);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~Rainfall")
	  {
	    if ($debug) echo("RainDaily hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~Rainfall");
	  }
    }
	$vid = @IPS_GetVariableIDByName("RainRate",$katidstorm);		
    if ($vid) 															
    {
      $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~Rainfall")
	  {
	    if ($debug) echo("RainRate hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~Rainfall");
	  }
    }
    
	//Wind von m/s nach km/h umrechnen
	$vid = @IPS_GetVariableIDByName("SustainedWindSpeed",$katidstorm);		
    if ($vid) 															
    {
      //in km/h umrechnen
	  $windspeed=GetValue($vid);
	  if ($windspeed < 9999)		//wenn 9999 dann ist der Storm nicht verbunden oder hat keinen Empfang
	  {
	    SetValue($vid,$windspeed * 3.6);
	  }
	  
	  $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~WindSpeed.kmh")
	  {
	    if ($debug) echo("SustainedWindSpeed hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~WindSpeed.kmh");
	  }
    }
		$vid = @IPS_GetVariableIDByName("WindGust",$katidstorm);		
    if ($vid) 															
    {
      //in km/h umrechnen
	  $windspeed=GetValue($vid);
	  if ($windspeed < 9999)		//wenn 9999 dann ist der Storm nicht verbunden oder hat keinen Empfang
	  {
	    SetValue($vid,$windspeed * 3.6);
	  }
	  
	  $varprofhum = IPS_GetVariable($vid);
	  if ($varprofhum["VariableCustomProfile"] != "~WindSpeed.kmh")
	  {
	    if ($debug) echo("WindGust hat falsches oder kein Profil -->".$varprofhum["VariableCustomProfile"]."
");
	    IPS_SetVariableCustomProfile($vid, "~WindSpeed.kmh");
	  }
    }
  }
?>

Viel Spass damit.

Gruss Letraz