Displaying the netatmo ombrometer & anemometer data in the Fibaro HC2
After we have already integrated the netatmo anemometer and the netatmo welcome into the Fibaro HomeCenter 2, the ombrometer cannot be missing either. How to do it can be read below.
The script of the netatmo anemometer serves as the basis for our scene. The script is now able to request the values of both sensors, the anemometer’s, as well as the rain sensor’s, via API at the netatmo servers and save them in variables, as well as display them in a virtual device. In case you should not have one of the two devices: No problem, the script is being tested automatically and the values will then not be requested at the netatmo servers.
As far as you do not own an ombrometer yet, you can currently buy one for 62€. For instance on amazon.
New functions of the netatmo script:
- Script does automatically recognize, which additional sensors (besides the weather station + outdoor module) are arranged and only requests the arranged sensors
- 4 values are being requested for the amount of rainfall: hourly amount of rainfall, daily amount of rainfall, weekly amount of rainfall and monthly amount of rainfall
- Test, if all needed variables have been arranged properly
- Debug can be turned off
- Bugfix: The script does not crash anymore, when the API cannot be contacted via netatmo, instead there is an error report regardless from the Debug-settings
There is a 98 % chance of an error report when first starting it. This is, because the variables have not been arranged yet. You will be informed through the script, which variables have to be arranged via the variable-panel. They are shown in the picture. Once you have arranged the variables, the scene will go on as usual and without any changes.
netatmo script:
The only thing that has to be changed at the script is your data of the site dev.netatmo.com. In case you are wanting to use the virtual device (can be found at the end of the text) as well, you need to additionally adjust the ID of the virtual device in line 15 to your ID.
You can optionally activate or deactivate the debug-mode in line 13 (0 = deactivated; 1 = activated).
When deactivating the debug, only the following two lines will show up in the debug. The script will afterwards renew the values every 5 minutes:
You should not change anything at the local variable “refresh”. The data is only sent from the respective station to the netatmo servers every 5 minutes, which is why a shorter refresh-time between the requests would not make sense.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | --[[ %% properties %% autostart %% globals --]] -- DIESE DATEN ANPASSEN local client_id = 'CLIENT_ID' local client_secret = 'CLIENT_SECRET' local username = 'MAIL' local password = 'PASSWORD' local refresh = 300 local debug = 1 local vd_ID = 178 -- AB HIER NICHTS MEHR ANPASSEN local token = '' local request_body = '' local rains = {hour = -1000, day = -1000, week = -1000, month = -1000} local sourceTrigger = fibaro:getSourceTrigger() Debug = function ( color, message ) fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span")) end DebugError = function ( color, message ) fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span")) end Debug( 'orange', 'netatmo Zusatzmodul Integration v.1.2 gestartet.' ) Debug( 'white', 'Daten werden alle 5 min aktualisiert. Bei debug = 0 wird nichts debuggt' ) function oAuth(nextFunction) local request_body = 'grant_type=password&client_id=' .. client_id .. '&client_secret=' .. client_secret .. '&username=' .. username .. '&password=' .. password .. '&scope=read_station' getResponseData('https://api.netatmo.net/oauth2/token', request_body, function(data) if (data.access_token ~= nil) then token = data.access_token if (debug == 1) then Debug( 'green', 'oAuth 2.0 durchgeführt.' ) end getDevices() fibaro:call(vd_ID, 'pressButton', '9') else DebugError( 'red', 'oAuth 2.0 konnte nicht durchgeführt werden! Bitte die Anmeldedaten überprüfen') end end ) setTimeout(oAuth, refresh*1000); end function getResponseData(url, body, func) local http = net.HTTPClient() http:request(url, { options = { method = 'POST', headers = { ['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' }, data = body }, success = function(response) func(json.decode(response.data)) end }) end function getDevices() getResponseData('https://api.netatmo.net/api/devicelist','access_token='..token, function(data) if (data.body ~= nil) then if (debug == 1) then Debug( 'green', 'netatmo Sensoren werden gesucht...' ); end for _, v in pairs(data.body.modules) do --fibaro:debug('Tabelle auslesen') if (v.data_type[1] == 'Rain') then rain_id = v._id if (debug == 1) then Debug( 'green', 'Regensensor ' ..rain_id.. ' erkannt.' ); end if rain_id ~= nil then getSumRain(60 * 60, 'hour') getSumRain(60 * 60 * 24, 'day') getSumRain(60 * 60 * 24 * 7, 'week') getSumRain(60 * 60 * 24 * 30, 'month') end elseif (v.data_type[1] == 'Wind') then wind_id = v._id if (debug == 1) then Debug( 'green', 'Windmesser ' ..rain_id.. ' erkannt.' ); end if wind_id ~= nil then getmeasureWind() end end int_id = data.body.devices[1]._id end else DebugError( 'red', 'device-list konnte nicht abgefragt werden! Bitte nächsten Durchlauf abwarten') end end ) end function getmeasureWind() request_body_wind = 'access_token='..token..'&device_id='..int_id..'&module_id='..wind_id..'&scale=max&type=WindStrength,WindAngle,GustStrength,GustAngle&date_end=last' getResponseData('https://api.netatmo.net/api/getmeasure', request_body_wind, function(getData) if (getData.body ~= nil) then if (debug == 1) then Debug( 'green', 'Windmesser wird ausgelesen...' ); end WindStrength = getData.body[1].value[1][1] WindAngle = getData.body[1].value[1][2] GustStrength = getData.body[1].value[1][3] GustAngle= getData.body[1].value[1][4] if (debug == 1) then Debug( 'green', 'Windgeschwindigkeit: ' .. WindStrength .. ' km/h' ); end if fibaro:getGlobalValue('windstaerke') ~= nil then fibaro:setGlobal('windstaerke', WindStrength) else DebugError( 'red', 'Varible windstaerke nicht gefunden. Bitte anlegen') end if fibaro:getGlobalValue('windrichtung') ~= nil then fibaro:setGlobal('windrichtung', WindAngle) else DebugError( 'red', 'Varible windrichtung nicht gefunden. Bitte anlegen') end if fibaro:getGlobalValue('boenstaerke') ~= nil then fibaro:setGlobal('boenstaerke', GustStrength) else DebugError( 'red', 'Varible boenstaerke nicht gefunden. Bitte anlegen') end if fibaro:getGlobalValue('boenrichtung') ~= nil then fibaro:setGlobal('boenrichtung', GustAngle) else DebugError( 'red', 'Varible boenrichtung nicht gefunden. Bitte anlegen') end if (debug == 1) then Debug( 'green', 'Windmesser auslesen beendet.' ); end else DebugError( 'red', 'API-Call konnte nicht durchgeführt werden! API nicht erreichbar! Bitte nächsten Durchlauf abwarten.') end end ) end function getSumRain(dauer, variable) local now = os.time(); getResponseData('https://api.netatmo.net/api/getmeasure','access_token='..token..'&device_id='..int_id..'&module_id='..rain_id..'&scale=1hour&type=sum_rain&real_time=true&date_begin='..os.date('!%c', (now - dauer)), function(data) local sum_rain = 0 for k, v in pairs(data.body) do for l, w in pairs(v.value) do sum_rain = sum_rain + w[1] end end if fibaro:getGlobalValue('rain_' ..variable) ~= nil then fibaro:setGlobal('rain_' ..variable, sum_rain) else DebugError( 'red', 'Varible rain_' ..variable.. ' nicht gefunden. Bitte anlegen') end if (debug == 1) then fibaro:debug('Regenmenge: ' ..sum_rain.. ' mm2 (' .. variable .. ')') end end ) end if (sourceTrigger['type'] == 'autostart') then oAuth(); end |
And of course, as promised, there is a matching virtual device for that. You can download it on here (simply click on the picture). You can simply import it via “Add/Remove device”. Please make sure that the file has the ending .vfib.
In case you should have further ideas to what can be done with the integration, we would be happy to read them in the comments. You can also download the two icons for the scene and the virtual device.
Hi
It will not work for me :-(
Here is the text popping up when i try to Debug.
[ERROR] 17:11:05: line 22: attempt to call method ‘getSourceTrigger’ (a nil value)
Hi
tks for the super VD but where is the scene you can upload sample pls
You can find the scene in the article. ;)
Cant get it to work, no data shows up in the virtual device.
Can you post the Debug of the scene?
Sorry but I don’t fully understand german..
[DEBUG] 17:50:36: netatmo Zusatzmodul Integration v.1.2 gestartet.
[DEBUG] 17:50:36: Daten werden alle 5 min aktualisiert. Bei debug = 0 wird nichts debuggt
[DEBUG] 17:50:36: oAuth 2.0 durchgeführt.
[DEBUG] 17:50:38: netatmo Sensoren werden gesucht…
[DEBUG] 17:50:38: Regensensor 05:00:00:00:58:40 erkannt.
[DEBUG] 17:50:38: Windmesser 05:00:00:00:58:40 erkannt.
[DEBUG] 17:50:40: Windmesser wird ausgelesen…
[DEBUG] 17:50:40: Windgeschwindigkeit: 2 km/h
[DEBUG] 17:50:40: Varible windstaerke nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Varible windrichtung nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Varible boenstaerke nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Varible boenrichtung nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Windmesser auslesen beendet.
[DEBUG] 17:50:40: Varible rain_day nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Regenmenge: 0 mm2 (day)
[DEBUG] 17:50:40: Varible rain_week nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Regenmenge: 0 mm2 (week)
[DEBUG] 17:50:40: Varible rain_hour nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Regenmenge: 0 mm2 (hour)
[DEBUG] 17:50:40: Varible rain_month nicht gefunden. Bitte anlegen
[DEBUG] 17:50:40: Regenmenge: 7.07 mm2 (month)
[DEBUG] 17:55:36: oAuth 2.0 durchgeführt.
[DEBUG] 17:55:37: netatmo Sensoren werden gesucht…
[DEBUG] 17:55:37: Regensensor 05:00:00:00:58:40 erkannt.
[DEBUG] 17:55:37: Windmesser 05:00:00:00:58:40 erkannt.
[DEBUG] 17:55:38: Varible rain_hour nicht gefunden. Bitte anlegen
[DEBUG] 17:55:38: Regenmenge: 0 mm2 (hour)
[DEBUG] 17:55:38: Varible rain_week nicht gefunden. Bitte anlegen
[DEBUG] 17:55:38: Regenmenge: 0 mm2 (week)
[DEBUG] 17:55:38: Varible rain_day nicht gefunden. Bitte anlegen
[DEBUG] 17:55:38: Regenmenge: 0 mm2 (day)
[DEBUG] 17:55:38: Varible rain_month nicht gefunden. Bitte anlegen
[DEBUG] 17:55:38: Regenmenge: 7.07 mm2 (month)
[DEBUG] 17:55:39: Windmesser wird ausgelesen…
[DEBUG] 17:55:39: Windgeschwindigkeit: 2 km/h
[DEBUG] 17:55:39: Varible windstaerke nicht gefunden. Bitte anlegen
[DEBUG] 17:55:39: Varible windrichtung nicht gefunden. Bitte anlegen
[DEBUG] 17:55:39: Varible boenstaerke nicht gefunden. Bitte anlegen
[DEBUG] 17:55:39: Varible boenrichtung nicht gefunden. Bitte anlegen
[DEBUG] 17:55:39: Windmesser auslesen beendet.
[DEBUG] 17:57:55: netatmo Zusatzmodul Integration v.1.2 gestartet.
[DEBUG] 17:57:55: Daten werden alle 5 min aktualisiert. Bei debug = 0 wird nichts debuggt
How should the Variables Panel be?
Also could you make a english version of this?
Thanks
Hi,
I can translate the code ;) You need the variables windstaerke, windrichtung, boenstaerke, boenrichtung, rain_hour, rain_week, rain_month.
Greetz
Hello,
Much appreciated if you could translate it – thanks!
I didn’t know/understod that you had to add Variables Panels (I saw that on previous pages/code).
Hi again,
How is it going with the translation?
Hi, this is perfekt. Only sometime break with this error
[DEBUG] 16:25:50: [1;31m2016-07-25 16:25:50.742614 [ fatal] LUA error: /usr/share/lua/5.2/json/decode/util.lua:35: unexpected character @ character: 1 0:1 [<] line:
[DEBUG] 16:25:50: <
Any idea ?
Hi
First I want to thank you for this wonderful solution / script, I really appreciated it !
But so now and then I got the following error message (see below) and the script will stop :(
I’m currently using the 1.2version ?
-My question to you, is this a known error ?
-What is the solution to prevent this from happening ?
[DEBUG] 13:40:51: _[1;31m2016-08-02 13:40:51.524189 [ fatal] LUA error: /usr/share/lua/5.2/json/decode/util.lua:35: unexpected character @ character:
Hi,
yes, it is a known error. It is a problem with the api of netatmo. I try to code a exception. Stay tuned :)
Greetz
Hi Boomx,
Great to hear, and I will definitely stay tuned !!
Thanks a lot !
Hi boomx, thanks for the fantastic interface, all working great.
However, I also get the breakdown with the unexpected character @ from time to time. If you could indeed code an exception, that would be fabulous
Cheers
This lua script look great. I’ve found a problem when you use 2 base stations. the script is at a certain point looking for a rain gauge (which is connected to the second station) at the data of the first station and gives an error:
[DEBUG] 17:12:10: netatmo Zusatzmodul Integration v.1.2 gestartet.
[DEBUG] 17:12:10: Daten werden alle 5 min aktualisiert. Bei debug = 0 wird nichts debuggt
[DEBUG] 17:12:10: oAuth 2.0 durchgeführt.
[DEBUG] 17:12:11: netatmo Sensoren werden gesucht…
[DEBUG] 17:12:11: Regensensor 05:00:00:00:48:58 erkannt.
[DEBUG] 17:12:11: Regensensor 05:00:00:00:93:80 erkannt.
[DEBUG] 17:12:11: Windmesser 05:00:00:00:93:80 erkannt.
[DEBUG] 17:12:13: Neerslag: 0 mm (hour)
[DEBUG] 17:12:13: Neerslag: 5.151 mm (day)
[DEBUG] 17:12:13: Neerslag: 40.4 mm (month)
[DEBUG] 17:12:13: [1;31m2016-08-21 17:12:13.849540 [ fatal] LUA error:
/opt/fibaro/scenes/239.lua:165: bad argument #1 to ‘pairs’ (table expected, got nil)
(line 165 is: for k, v in pairs(data.body) do )
any ideas of how to deal with 2 base stations??
Very nice to have a script like this, but unfortunately it leaves more questions than answers to me.
Would be nice when you have a small user manual how to install.
Did I read in one of the comments that I have to define variables? Where en what variables?
In the section -DIESE DATEN ANPASSEN- I have to fill in CLIENT_ID and CLIENT_SECRET. It’s not clear what this refers to. Can someone help?
The username and password below that, do they refer to the login at auth.netatmo.com? If not, then what does it refer to?
Thanks for help!
Boomx,
I understood from Netatmo that the API devicelist will be faded out by end of November, needs to be replaced with getstationsdata.
Would you have the possibility to update your fantastic tool? (I unfortunate lack the LUA programming knowledge)
Thanks,
WimJanse
Boomx
I learned from Netatmo that the API call devicelist will no longer be supported as of 30 november.
The call will be replaced by getstationsdata. Would you be so kind to update your excellent tool. I’m using this in Fibaro HC2 to control my sunscreens ( have to go up if too much wind), which I will not be able to fo in future.
I tried myself, but the API response structure is different, and I lack LUA knowledge to make the change myself.
Thank you,
WimJanse
Hi boomx,
My outdoor module died on me so at the moment I have only indoor module, rain and window sensor. After outdoor module died I also noticed that script for getting data from rain and wind sensor stop working. I get this error in debugging window:
[DEBUG] 09:10:02: [1;31m2016-11-24 09:10:02.703299 [ fatal] LUA error: /opt/fibaro/scenes/76.lua:133: attempt to concatenate global ‘int_id’ (a nil value)
After analyzing problem I found that you are getting device id after loop which is getting module id’s. After moving this line of code
int_id = data.body.devices[1]._id
before loop it is again working OK. See complete code for getting devices after change:
function getDevices()
getResponseData(‘https://api.netatmo.net/api/devicelist’,’access_token=’..token,
function(data)
fibaro:debug(‘Wind and rain sensor data requested…’)
int_id = data.body.devices[1]._id
for _, v in pairs(data.body.modules) do
if (v.data_type[1] == ‘Rain’) then
rain_id = v._id
fibaro:debug(‘Rain sensor ‘ .. rain_id .. ‘ recognized!’)
–rain_bat = calcBat(v.battery_vp, true)
if rain_id ~= nil then
getSumRain(60 * 60, ‘hour’)
getSumRain(60 * 60 * 24, ‘day’)
getSumRain(60 * 60 * 24 * 7, ‘week’)
getSumRain(60 * 60 * 24 * 30, ‘month’)
end
elseif (v.data_type[1] == ‘Wind’) then
wind_id = v._id
fibaro:debug(‘Windsensor ‘ .. wind_id .. ‘ recognized!’)
getmeasureWind()
end
end
end
)
end
Thank you and keep up good work!