Hi,
I initially implemented the way described by you, but since I also do run Weewx, I managed to alter the code in order to retrieve data just from one source. It will also prevent from reading very high values because the data creation is driven by the setting in the sensor/configuration/APIs and a PHP script.
1. PHP script. I keep that one in my /srv/http/weewx folder. I created an empty feinstaub.json (and sds011pmdata.txt which is needed for Weewx and filepile). I think you can obmit the part after feinstaub.json). The name of the file is sds011.php (in my case) and this needs to be set in the API of sensor.
Code: Select all
<?php
$json = file_get_contents("php://input");
$json = file_put_contents("feinstaub.json", $json);
$json_string = file_get_contents("/srv/http/weewx/feinstaub.json");
$parsed_json = json_decode($json_string, true);
$sds011file = fopen("/srv/http/weewx/sds011pmdata.txt", "w") or die("Unable to open file!");
$txt = "pm10_0 = ".($parsed_json['sensordatavalues'][0]['value'])."\n";
fwrite($sds011file, $txt);
$txt = "pm2_5 = ".($parsed_json['sensordatavalues'][1]['value'])."\n";
fwrite($sds011file, $txt);
$txt = "temp = ".($parsed_json['sensordatavalues'][2]['value'])."\n";
fwrite($sds011file, $txt);
$txt = "humid = ".($parsed_json['sensordatavalues'][3]['value'])."\n";
fwrite($sds011file, $txt);
$txt = "signal = ".($parsed_json['sensordatavalues'][8]['value'])."\n";
fwrite($sds011file, $txt);
fclose($sds011file);
?>
This will update/overwrite the data in the feinstaub.json file according your cycle set in sensor (default 145 seconds, I changed to 300 since Weewx just records every 5 mins).
I then created a dvents script looking like this; I kept the email notification for skipped reading. I think this is now not needed anymore since the file is always fed by the API in the sensor.
Code: Select all
return {
active = true,
on = {
timer = { 'every minute' },
httpResponses = { 'luftdatenRetrieved' } -- matches callback string below
},
execute = function(domoticz, item)
local maxValue = 100000
local minValue = -50
if (item.isTimer) then
domoticz.openURL({
url = 'http://192.168.178.39/weewx/feinstaub.json',
method = 'GET',
callback = 'luftdatenRetrieved'
})
elseif (item.isHTTPResponse) then
if (item.ok and item.isJSON) then -- statusCode == 2xx
-- 1: SDS_P1 PM10, 2: SDS_P2 PM2.5, 3: DHT22 temp, 4: DHT22 hum, 5: BME280 temp, 6: BME280 baro, 7: BME280 hum
domoticz.log(item.json,domoticz.LOG_FORCE)
if item.json.sensordatavalues and
item.json.sensordatavalues[2] and
item.json.sensordatavalues[2].value and
tonumber(item.json.sensordatavalues[2].value) ~= nil and
tonumber(item.json.sensordatavalues[2].value) < maxValue and
tonumber(item.json.sensordatavalues[2].value) > minValue then
domoticz.devices('Feinstaub PM10').updateCustomSensor(item.json.sensordatavalues[1].value)
domoticz.devices('Feinstaub PM2.5').updateCustomSensor(item.json.sensordatavalues[2].value)
domoticz.devices('Signal SDS011').updateCustomSensor(item.json.sensordatavalues[9].value)
domoticz.devices('Luftdaten DHT22').updateTempHum(item.json.sensordatavalues[3].value,item.json.sensordatavalues[4].value,0)
else
domoticz.log('Skipping this reading', domoticz.LOG_ERROR)
domoticz.email('Error fetching Luftdaten', 'PM10 was above 100000 and measurement was skipped', '[email protected]')
end
--[[
domoticz.devices('Luftdaten DHT22').updateTempHum(item.json.sensordatavalues[3].value,item.json.sensordatavalues[4].value,0)
if item.json.sensordatavalues[5] and item.json.sensordatavalues[6] and item.json.sensordatavalues[6].value and item.json.sensordatavalues[6].value then
if ( ( tonumber (item.json.sensordatavalues[5].value) or 100 ) < 100) and ( (tonumber (item.json.sensordatavalues[6].value) or 105000 ) < 105000) then
domoticz.devices('Luftdaten BME280').updateTempHumBaro(item.json.sensordatavalues[5].value,item.json.sensordatavalues[7].value,0,(item.json.sensordatavalues[6].value/100),0)
end
end
]]--
else
-- oops
domoticz.log('Error fetching Luftdaten data', domoticz.LOG_ERROR)
domoticz.log(item.data, domoticz.LOG_ERROR)
domoticz.email('Error fetching Luftdaten', 'Check log for details', '[email protected]')
end
end
end
}
This could be an alternative to the pulling mode from sensor since it is driven by the sensor updating the .json file after the sensor measured the data.