Weewx & Domoticz sont dans un bateau ...
Publié : 22 févr. 2017, 18:45
Pour ceux qui ont des stations météo compatibles weewx ( ou wview) mais pas domoticz et qui voudraient voir les infos météo de leur petite station sur leur interface domoticz.
Voici une démarche qui permet cela.
Dans Domoticz, créer les dispositifs ( à partir d'un matériel de type 'Dummy', appelons le 'Weewx') correspondant à votre matériel météo (par exemple pour une Oregon Scientific WMR88 j'ai créé : 1 'Temp + Humidity + Baro' nommé 'Météo Jardin', 1 'Temp + Humidity' nommé 'Temp. Salon', 1 'Temp + Humidity' nommé 'Temp. Cave',
, 1 'Rain' nommé 'Précipitations Jardin' et un 'Wind+Temp+Chill' nommé 'Vent Jardin') et repérer les Idx de ces périphériques.
Dans le fichier de configuration de votre skin weewx (/etc/weewx/skins/MySkin/skin.conf), créer un template de generateur [CheetahGenerator] et le renseigner comme suit:
exemple :
-----skin.conf----
...
[CheetahGenerator]
...
[[ToDate]]
...
[[[domoticz]]]
template = domoticz.xml.tmpl
...
-------------------
Puis créer le fichier domoticz.xml.tmpl (avec les informations nécessaires à vos dispositifs)
Weewx va alors générer, à intervalle régulier, un fichier domoticz.xml sur votre site web météo.
Le principe est de récupérer ce fichier xml sur le serveur domoticz et de le parcourir avec un script lua afin de mettre à jour vos capteurs.
Exemple de script lua qui va renseigner la BdD domoticz (dans ... domoticz/scripts/lua/script_time_weewx.lua)
---------------------------script_time_weewx.lua-------------------------
NB: pour accélerer le script lua, la récupération du fichier domoticz.xml sur le site de weewx ne sera pas faite par le script lua mais par un crontab.
-----------Exemple de crontab -------------
Ce crontab récupère chaque 5 minutes (à partir de NNh02mn, car weewx génére à NNh01mn ) le fichier depuis le site weewx et il remplace les ',' par des '.' (weewx sépare partie entière et décimle par des ',' alors que domoticz veut des '.' ).
Vous pouvez ainsi avoir un truc du genre: Have fun !!
Remarque: Sur le snapshot ci-dessus, indice UV et Prévisions météos proviennent de WeatherUndergound, pas de weewx (je n'ai pas encore de capteur UV ;o) .
Voici une démarche qui permet cela.
Dans Domoticz, créer les dispositifs ( à partir d'un matériel de type 'Dummy', appelons le 'Weewx') correspondant à votre matériel météo (par exemple pour une Oregon Scientific WMR88 j'ai créé : 1 'Temp + Humidity + Baro' nommé 'Météo Jardin', 1 'Temp + Humidity' nommé 'Temp. Salon', 1 'Temp + Humidity' nommé 'Temp. Cave',
, 1 'Rain' nommé 'Précipitations Jardin' et un 'Wind+Temp+Chill' nommé 'Vent Jardin') et repérer les Idx de ces périphériques.
Dans le fichier de configuration de votre skin weewx (/etc/weewx/skins/MySkin/skin.conf), créer un template de generateur [CheetahGenerator] et le renseigner comme suit:
exemple :
-----skin.conf----
...
[CheetahGenerator]
...
[[ToDate]]
...
[[[domoticz]]]
template = domoticz.xml.tmpl
...
-------------------
Puis créer le fichier domoticz.xml.tmpl (avec les informations nécessaires à vos dispositifs)
Code : Tout sélectionner
<weewx>
<data realtime="temp">$current.outTemp.formatted<!--Temp Jardin--></data>
<data realtime="hum">$current.outHumidity.formatted<!--Humidity Jardin--></data>
<data realtime="inTemp">$current.inTemp.formatted<!--Temp Salon--></data>
<data realtime="inHum">$current.inHumidity.formatted<!--Humidity Salon--></data>
<data realtime="auxTemp">$current.extraTemp1.formatted<!--Temp Cave--></data>
<data realtime="auxHum">$current.extraHumid1.formatted<!--Humidity Cave--></data>
<data realtime="barometer">$current.barometer.formatted<!--Barometre--></data>
<data realtime="wind">$current.windSpeed.formatted<!--Vitesse Vent--></data>
<data realtime="Winddir">$current.windDir.formatted<!--Direction vent--></data>
<data realtime="Windgust">$current.windGust.formatted<!--Rafales--></data>
<data realtime="Windcomp">$current.windDir.ordinal_compass<!--Vent Compas--></data>
<data realtime="WindChill">$current.windchill.formatted<!--Temp Windchill--></data>
<data realtime="rainrate">$current.rainRate.formatted<!--Taux de precipitation--></data>
<data realtime="Rain">$alltime.rain.sum.formatted<!-- Total des Precipitations --></data>
</weewx>Le principe est de récupérer ce fichier xml sur le serveur domoticz et de le parcourir avec un script lua afin de mettre à jour vos capteurs.
Exemple de script lua qui va renseigner la BdD domoticz (dans ... domoticz/scripts/lua/script_time_weewx.lua)
---------------------------script_time_weewx.lua-------------------------
Code : Tout sélectionner
-- Title: script_device_weewx.lua
-- Date: 10-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device
-- format of the xml file =
-- <weewx>
-- <data realtime="temp">8.5<!--outsideTemp--></data>
-- <data realtime="hum">73.2<!--outsideHumidity--></data>
-- <data realtime="inTemp">19.5<!--outsideTemp--></data>
-- <data realtime="inHum">53.2<!--outsideHumidity--></data>
-- <data realtime="barometer">1025.5<!--Pressure--></data>
-- </weewx>
--
--
-- Config
local wind_device_idx = 13
local rain_device_idx = 11
local garden_device_idx = 8
local room_device_idx = 9
local aux_device_idx = 14
-- Attention au nom du "Dispositif" ,
-- c'est celui que l'on a mis dans la Config (on en choisi un au hasard parmis l'un des dispositifs de notre weewx)
s = otherdevices_lastupdate['Température Salon']
-- returns a date time like 2013-07-11 17:23:12
-- Remplacer 'Vent Jardin' par le nom de votre dispositif anémomètre
prev_wind = otherdevices_svalues['Vent Jardin']
-- End Config
commandArray = {}
t1 = os.time()
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
-- ON ne mets à jour que tous les 5 mns
if ( difference < 300 ) then
return commandArray
end
-- ca fonctionne mais c'est parfois un peu long et on dépasse les 10 secondes... on fait donc cela par crontab
-- os.execute('wget http://my-weewx.site.fr/domoticz.xml -O /tmp/domoticz.xml')
-- os.execute('sed s#,#.# /tmp/domoticz.xml > /tmp/domoticz.xml-pre')
local filename = "/tmp/domoticz.xml-pre"
local line , tempdevice = ''
local temperature,humidity,pressure,inhumidity,intemperature,auxhumidity,auxtemperature,rain,rainrate,wind,winddir,windgust,windchill = 0
local windcomp = 'N'
for line in io.lines(filename) do
if string.find(line,"\"temp\"") then
temperature=tonumber(string.match(line, "%-?%d+.?%d*"))
elseif string.find(line,"\"hum\"") then
humidity=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"inTemp\"") then
intemperature=tonumber(string.match(line, "%-?%d+.?%d*"))
elseif string.find(line,"\"inHum\"") then
inhumidity=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"auxTemp\"") then
auxtemperature=tonumber(string.match(line, "%-?%d+.?%d*"))
elseif string.find(line,"\"auxHum\"") then
auxhumidity=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"barometer\"") then
pressure=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"rainrate\"") then
rainrate=100 * tonumber(string.match(line, "%d+.?%d*"))
elseif string.find(line,"\"Rain\"") then
rain=tonumber(string.match(line, "%d+.?%d*"))
elseif string.find(line,"\"wind\"") then
wind=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"Winddir\"") then
winddir=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"Windgust\"") then
windgust=tonumber(string.match(line, "%d+"))
elseif string.find(line,"\"Windcomp\"") then
windcomp=string.match(line, "Windcomp\">([NESO/A]+)<!--")
elseif string.find(line,"\"WindChill\"") then
windchill=tonumber(string.match(line, "%-?%d+.?%d*"))
end
end
-- Si la vitesse du vent est nulle winddir et windcomp garde la valeur précédente
if ( wind == 0 or wind == nil ) then
winddir,windcomp = prev_wind:match("([^;]+);([^;]+)")
wind = 0
end
-- A l'initialisation du device domoticz on peut avoir des valeurs farfelues
if winddir == nil then
winddir = 0
end
if windcomp =='N/A' then
windcomp = 'N'
end
if windchill == nil then
windchill = 0
end
if windgust== nil then
windgust= 0
end
-- Conversion de win et wingust (domoticz attend 10 * Winspeed [m/s] -- weewx nous donne Winspeed [km/h]
wind = math.ceil(wind / 0.36)
windgust = math.ceil(windgust / 0.36)
-- Conditions Salon
if not ( inhumidity == nil or intemperature == nil ) then
tempdevice = room_device_idx..|0|'..intemperature..';'..inhumidity..';0'
commandArray [1] = {['UpdateDevice'] = tempdevice }
end
-- Meteo Locale idx = 8
if not ( humidity == nil or temperature == nil or pressure == nil ) then
tempdevice = garden_device_idx..'|0|'..temperature..';'..humidity..';0;'..pressure..';0'
commandArray [2] = {['UpdateDevice'] = tempdevice }
end
-- Pluie Locale idx = 11
if not ( rainrate == nil or rain == nil ) then
tempdevice = rain_device_idx..'|0|'..rainrate..';'..rain
commandArray [3] = {['UpdateDevice'] = tempdevice }
end
-- Vent Locale idx=13 dir;comp;speed;gust;temp;windchill
if not ( temperature == nil ) then
tempdevice = wind_device_idx..'|0|'..winddir..';'..windcomp..';'..wind..';'..windgust..';'..temperature..';'..windchill
commandArray [4] = {['UpdateDevice'] = tempdevice }
end
-- Conditions Aux idx=14 (WC ou Cave)
if not ( auxhumidity == nil or auxtemperature == nil ) then
tempdevice = aux_device_idx..'|0|'..auxtemperature..';'..auxhumidity..';0'
commandArray [5] = {['UpdateDevice'] = tempdevice }
end
return commandArrayNB: pour accélerer le script lua, la récupération du fichier domoticz.xml sur le site de weewx ne sera pas faite par le script lua mais par un crontab.
-----------Exemple de crontab -------------
Code : Tout sélectionner
2-57/5 * * * * wget http://my-weewx.site.fr/domoticz.xml -O /tmp/domoticz.xml && sed s#,#.# /tmp/domoticz.xml > /tmp/domoticz.xml-preVous pouvez ainsi avoir un truc du genre: Have fun !!
Remarque: Sur le snapshot ci-dessus, indice UV et Prévisions météos proviennent de WeatherUndergound, pas de weewx (je n'ai pas encore de capteur UV ;o) .