Bonus! As you can imagine, I’m using the correct units (
) - and that’s why having “wrong ones” in the mix is just as bad. So I refactored everything and now I only have one item in the items file - and the good thing is: depending on your configured locale - it even comes in the right unit, regardless of Input.
Why? Because what I learned, Unit of Measurement is as cool as it gets:
- you define in your locale, what unit you like
- if the item is updated (either via binding or REST-API), you can add the unit in which you have in your source
- openHAB will automagically store the value and give you the right unit back, as you requested.
So in my case: my source is imperial and my locale is metric. If the source updates the items and while doing this is telling openHAB the unit - openHAB calculates it accordingly.
So - my PHP now looks like this (I know, it’s a bit dirty, but works):
<?
$weatherprefix = "WeatherStation_"; // if you have a prefix in your OH2 items configuration
// resends all GET-Values directly to OH2 API
foreach ($_GET as $key => $value) {
// looking for suffixes and redirecting them
/* Weatherunderground format looks like this:
* ID ID (configuratble in the App)
* PASSWORD KEY (configurable in the App)
* indoortempf in Fahrenheit
* windchillf in Fahrenheit
* indoorhumidity in percent
* humidity in pfpro_cleanup
* windspeedmph in mp/h
* windgustmph in mp/h
* winddir in degrees
* absbaromin in inch of mercury
* baromin in inch of mercury
* rainin in inch
* dailyrainin in inch
* weeklyrainin in inch
* monthlyrainin in inch
* solarradiation in W/m2
* UV in index (0 - 11)
* dateutc current date (UTC)
* softwaretype used software
* action usually "updateraw"
* realtime using real-time updates (0/1)
* rtfreq update frequency (configurable in the App)
as seen here, with the last character(s) of a attribute you can tell, which unit is used
*/
switch (substr($key, -3)) {
case "mpf":
// unit is in Fahrenheit (remove "f")
sendOH2Command($weatherprefix.str_replace("mpf", "mp", $key), f2c($value));
break;
case "ptf":
// unit is in Fahrenheit also (remove "f")
sendOH2Command($weatherprefix.str_replace("ptf", "pt", $key), f2c($value));
break;
case "llf":
// unit is in Fahrenheit also (remove "f")
sendOH2Command($weatherprefix.str_replace("llf", "ll", $key), f2c($value));
break;
case "min":
// unit is in inHg (remove "min")
sendOH2Command($weatherprefix.str_replace("min", "", $key), inHG2hPa($value));
break;
case "nin":
// unit is in inch (remove "in")
sendOH2Command($weatherprefix.str_replace("nin", "n", $key), in2cm($value));
break;
case "mph":
// unit is in mph (remove "mph")
sendOH2Command($weatherprefix.str_replace("mph", "", $key), mph2kmh($value));
break;
case "utc":
// unit is in UTC
sendOH2Command($weatherprefix.$key, str2UTC($value)."");
break;
default:
// everything else: not a unit, or percent or string
sendOH2Command($weatherprefix.$key, $value);
}
}
function sendOH2Command($item, $data) {
$url = "http://192.168.xxx.xxx:8080/rest/items/" . $item;
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => $data //http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function f2c($given_value) {
$celsius = round(5/9*($given_value-32),2);
settype($celsius, "string");
return $celsius;
}
function inHG2hPa($given_value) {
$hpa = round(floatval($given_value) * 33.862,2);
settype($hpa, "string");
return $hpa;
}
function in2cm($given_value){
$cm = round(floatval($given_value) * 2.54,2);
settype($cm, "string");
return $cm;
}
function mph2kmh($given_value) {
$kmh = round(floatval($given_value) * 1.6,2);
settype($kmh, "string");
return $kmh;
}
function str2UTC($given_value) {
$UTC = str_replace(" ", "T", $given_value);
return $UTC;
}
?>
my items look like this:
String Weatherstation_JSON "Wetterstation JSON"
Group Wetter "Wetter Überblick" (All)
// Allgemeine Infos
String Weatherstation_ID "ID"
String Weatherstation_PASSWORD "PWD"
DateTime Weatherstation_dateutc "Uhrzeit" <temperature> (gWeather, Wetter)
String Weatherstation_softwaretype "Software" <temperature> (gWeather, Wetter)
String Weatherstation_action "Wetteraktion" <temperature> (gWeather, Wetter)
String Weatherstation_realtime "Realtime" <temperature> (gWeather, Wetter)
String Weatherstation_rtfreq "Updatefrequenz" <temperature> (gWeather, Wetter)
// Temperaturen
Number:Temperature Weatherstation_indoortemp "Innen Temperature [%.1f %unit%]" <temperature> (gWeatherTemp, Wetter)
Number:Temperature Weatherstation_temp "Aussen Temperature [%.1f %unit%]" <temperature> (gWeatherTemp, Wetter)
Number:Temperature Weatherstation_dewpt "Taupunkt [%.1f %unit%]" <temperature> (gWeatherTemp, Wetter)
Number:Temperature Weatherstation_windchill "Windchill [%.1f %unit%]" <temperature> (gWeatherTemp, Wetter)
// Luftdruck
Number:Pressure Weatherstation_baro "Luftdruck min [%.1f %unit%]" <temperature> (gWeatherPressure, Wetter)
Number:Pressure Weatherstation_absbaro "Abs. Luftdruck min [%.1f %unit%]" <temperature> (gWeatherPressure, Wetter)
// Niederschlag
Number:Length Weatherstation_rain "Niederschlag [%.1f mm]" <temperature> (gWeatherHeight, Wetter)
Number:Length Weatherstation_dailyrain "Niederschlag/Tag [%.1f mm]" <temperature> (gWeatherHeight, Wetter)
Number:Length Weatherstation_weeklyrain "Niederschlag/Woche %.1f mm]" <temperature> (gWeatherHeight, Wetter)
Number:Length Weatherstation_monthlyrain "Niederschlag/Monata [%.1f mm]" <temperature> (gWeatherHeight, Wetter)
// Wind
String Weatherstation_winddir "Windrichtung" <temperature> (gWeather, Wetter)
Number:Speed Weatherstation_windspeed "Windgeschwindigkeit [%.1f %unit%]" <temperature> (gWeatherSpeed, Wetter)
Number:Speed Weatherstation_windgust "Windböen [%.1f %unit%]" <temperature> (gWeatherSpeed, Wetter)
// Feuchtigkeit
Number Weatherstation_humidity "Außen Feuchtigkeit [%.1f %%]" <temperature> (gWeather, Wetter)
Number Weatherstation_indoorhumidity "Innen Feuchtigkeit [%.1f %%]" <temperature> (gWeather, Wetter)
// Sonneneinstrahlung
Number Weatherstation_solarradiation "Solareintrag [%.1f]" <temperature> (gWeather, Wetter)
Number Weatherstation_UV "UV-Index [%.1f]" <temperature> (gWeather, Wetter)