From 7c042850ae6947f8c95bfcef9907b73d600d3b7d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 14 Jul 2023 12:13:55 +0200 Subject: [PATCH] . --- .idea/workspace.xml | 7 ++++--- scripts/lib/homeassistant.php | 25 +++++++++++++++++++------ scripts/lib/weatherstation.php | 8 ++++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 526a0f3..7b90449 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,7 +6,8 @@ - + + diff --git a/scripts/lib/homeassistant.php b/scripts/lib/homeassistant.php index 898f343..a0a1c38 100644 --- a/scripts/lib/homeassistant.php +++ b/scripts/lib/homeassistant.php @@ -8,7 +8,7 @@ const HA_ECO_BASIC = 'switch.ecomode_basic'; const HA_ECO_SUPER = 'switch.ecomode_super'; const HA_HOTTE = 'switch.hotte'; const HA_BAR = 'switch.bar'; -const HA_PLANETARIUM='switch.0xa4c138788071821e'; +const HA_PLANETARIUM = 'switch.0xa4c138788071821e'; function haGetState($id, $attribute = null) { //ID to read - for example sensor.foo @@ -21,12 +21,25 @@ function haGetState($id, $attribute = null) { //ID to read - for example sensor. "Authorization: Bearer " . HA_KEY . "\r\n" ] ]; - $ctx = stream_context_create($opts); - $data = @file_get_contents(HA_URL . 'api/states/' . $id, false, $ctx); - if (!$data) { - return false; + + $ids = !is_array($id) ? explode(',', $id) : $id; + $d = []; + + foreach ($ids as $id) { + $ctx = stream_context_create($opts); + $data = @file_get_contents(HA_URL . 'api/states/' . $id, false, $ctx); + if (!$data) { + continue; + } + $d[$id] = json_decode($data); } - $data = json_decode($data); + + usort($d, function ($a, $b) { + return strcmp($a->last_updated, $b->last_updated) * -1; + }); + + + $data = array_shift($d); if (null === $attribute) { if (!$data) diff --git a/scripts/lib/weatherstation.php b/scripts/lib/weatherstation.php index cb00e53..11255a5 100644 --- a/scripts/lib/weatherstation.php +++ b/scripts/lib/weatherstation.php @@ -15,8 +15,8 @@ function getWeather() { $weather = json_decode(file_get_contents($cache), true); $weather = $weather[0]; $res['temp'] = round(($weather['Temperature']['Metric']['Value']/* + getState('airtemperature')*/)); - $res['outdoor_temp'] = toNumber(haGetState('sensor.indoor_outdoor_meter_ddf7_temperature'), 1); - $res['outdoor_humidity'] = toNumber(haGetState('sensor.indoor_outdoor_meter_ddf7_humidty'), true); + $res['outdoor_temp'] = toNumber(haGetState(['sensor.indoor_outdoor_meter_ddf7_temperature', 'sensor.w340001x_tempc']), 1); + $res['outdoor_humidity'] = toNumber(haGetState(['sensor.indoor_outdoor_meter_ddf7_humidity', 'sensor.w340001x_hum']), true); $res['outdoor_pressure'] = toNumber(haGetState('sensor.0x00158d000464c885_pressure'), true); $res['backyard_temp'] = toNumber(haGetState('sensor.0x00158d000464c885_temperature'), 1); $res['backyard_humidity'] = toNumber(haGetState('sensor.0x00158d000464c885_humidity'), true); @@ -32,8 +32,8 @@ function getWeather() { $res['kitchen_humidity'] = toNumber(haGetState('sensor.0x00158d000418441f_humidity'), true); $res['wc_temp'] = toNumber(haGetState('sensor.0x00158d00046574da_temperature'), 1); $res['wc_humidity'] = toNumber(haGetState('sensor.0x00158d00046574da_humidity'), true); - $res['office_temp'] = toNumber(haGetState('sensor.meter_c7c3_temperature'), 1); - $res['office_humidity'] = toNumber(haGetState('sensor.meter_c7c3_humidite'), true); + $res['office_temp'] = toNumber(haGetState(['sensor.meter_c7c3_temperature', 'sensor.thx1_w230150x_tempc']), 1); + $res['office_humidity'] = toNumber(haGetState(['sensor.meter_c7c3_humidite', 'sensor.thx1_w230150x_hum']), true); // $res['cellar_humidity'] = toNumber(getDomoticzDeviceStatus(4818), true); // $res['cellar_temp'] = toNumber(getDomoticzDeviceStatus(4817), 1); $res['cellar_humidity'] = 0; -- 2.39.5