philippelt / netatmo-api-python

Netatmo connect API python client (for Netatmo information, see https://dev.netatmo.com)
GNU General Public License v3.0
186 stars 118 forks source link

At same home, two stations with same name #53

Closed vheat closed 11 months ago

vheat commented 12 months ago

I'm running Netatmo Weather since 2015 now. By some reason I had to replace my first station by a new one (2017) but kept the data of the first one at Netatmo. Somehow I was able to give the second on the same name like the first one. Now in every interface (web/app) I see two stations with same name where I can chose either. The first one does not show me any recent data, the second one does. This means in the web/app interface there is a way to differentiate (N.B. when I try to rename, I rename always both at the same time, so somewhat frustrated by this). I'm using grafana to record the data myself. This was working fine until the recent change concerning REFRESH_TOKEN. After updating lnetatmo I always select the old one (with no data returned) and the simpleLastData script fails by an error (as selecting the wrong station for default). I'm not able to address the station by station="station_name" as both stations share the same name.

Would it be an option to additionally have the station key "_id" as a further selector? This mac address is unique and is an intrinsic selector identifying the hardware. Thanks

I took the User class to find the different _id strings for the stations.

Further looking around, the lnetatmo is organized around the names, even building the raw_data hides one of the duplicates because station_name is used for key. So a potential change looks to become bigger effort as not only the station_name would have to be replaced by _id , the same applies for all modules which may have same names as well... I wonder how common the issue is, whether it might be more simple to delete my netatmo account fully and rebuild it again.

philippelt commented 12 months ago

I produced a patch to handle station id as well as a station name as parameter value where a station is expected.

Download directly the library version from github, I will not publish a release before some time as these are only minor changes to solve some migrations issues.

philippelt commented 12 months ago

Closed by mistake

vheat commented 12 months ago

lnetatmo.py:294 self.stations = { d['station_name'] : d for d in self.rawData }

If two stations have different name and same station_name above line will only make it for one station as key is the same. At the end there will be just one in the list self.stations. So selection by _id is fine unless station_name is same. All the "name" indexing would need to be redone for "_id" as ids, which are mac addresses, are unique versus name which are user supplied and in some way arbitrary. (I agree, the the failure is at my side, giving the old and the new station the same name by whatever reason it has occured).

philippelt commented 11 months ago

Push a new version to handle duplicate names properly. I am not able to test because I do not want to create such config in my account :smile: Thanks for testing the new property stationIds wich replicate stations but indexed by Ids thus no duplicates are possible.

Using Id in parameter for WeatherStationData or lastData should work now in case of duplicate.

The consequence is that stationbyname and stationbyid may return 2 differents config for a duplicate name, the stationbyname being randomly one of the many stations with the same name (depending of the order of stations in netatmo json response).

vheat commented 11 months ago

Change works for me, testing with simpleLastData. I had to debug because in example the names for the modules "internal" / "external" had to be replaced by my names. Another change I had to do, the WeatherStationData.lastData() now looks to require the station_name/_id. After supplying this and changing to my module names it worked.

For reference I attach the (changed personal data) response body for my setup (same name for two stations)

{'body': {'devices': [{'_id': '70:ee:50:a7:4f:28',
                       'co2_calibrating': False,
                       'dashboard_data': {'AbsolutePressure': 971.4,
                                          'CO2': 603,
                                          'Humidity': 50,
                                          'Noise': 37,
                                          'Pressure': 1020.7,
                                          'Temperature': 25.1,
                                          'date_max_temp': 1689631436,
                                          'date_min_temp': 1689665952,
                                          'max_temp': 25.6,
                                          'min_temp': 24.7,
                                          'pressure_trend': 'down',
                                          'temp_trend': 'stable',
                                          'time_utc': 1689684117},
                       'data_type': ['Temperature',
                                     'CO2',
                                     'Humidity',
                                     'Noise',
                                     'Pressure'],
                       'date_setup': 1489795124,
                       'firmware': 201,
                       'home_id': '5ea8124cf30023ef570a7745',
                       'home_name': 'Location neu',
                       'last_setup': 1489795124,
                       'last_status_store': 1689684118,
                       'last_upgrade': 1489795107,
                       'module_name': 'Innenraum',
                       'modules': [{'_id': '02:00:00:27:94:10',
                                    'battery_percent': 58,
                                    'battery_vp': 5250,
                                    'dashboard_data': {'Humidity': 36,
                                                       'Temperature': 27.5,
                                                       'date_max_temp': 1689683790,
                                                       'date_min_temp': 1689653233,
                                                       'max_temp': 27.7,
                                                       'min_temp': 14,
                                                       'temp_trend': 'up',
                                                       'time_utc': 1689684097},
                                    'data_type': ['Temperature', 'Humidity'],
                                    'firmware': 53,
                                    'last_message': 1689684117,
                                    'last_seen': 1689684097,
                                    'last_setup': 1489795069,
                                    'module_name': 'Aussenraum',
                                    'reachable': True,
                                    'rf_status': 77,
                                    'type': 'NAModule1'},
                                   {'_id': '05:00:00:00:c4:36',
                                    'battery_percent': 72,
                                    'battery_vp': 5382,
                                    'dashboard_data': {'Rain': 0,
                                                       'sum_rain_1': 0,
                                                       'sum_rain_24': 0,
                                                       'time_utc': 1689684110},
                                    'data_type': ['Rain'],
                                    'firmware': 14,
                                    'last_message': 1689684117,
                                    'last_seen': 1689684110,
                                    'last_setup': 1641730261,
                                    'reachable': True,
                                    'rf_status': 67,
                                    'type': 'NAModule3'}],
                       'place': {'altitude': 416,
                                 'city': 'Pusemuckl',
                                 'country': 'DE',
                                 'location': [10.298114914894106,
                                              48.754500000969956],
                                 'timezone': 'Europe/Berlin'},
                       'reachable': True,
                       'station_name': 'Location neu (Innenraum)',
                       'type': 'NAMain',
                       'wifi_status': 64},
                      {'_id': '70:ee:50:12:c4:54',
                       'co2_calibrating': False,
                       'data_type': ['Temperature',
                                     'CO2',
                                     'Humidity',
                                     'Noise',
                                     'Pressure'],
                       'date_setup': 1425692550,
                       'firmware': 111,
                       'home_id': '5ea8124cf20023df570a7745',
                       'home_name': 'Location neu',
                       'last_setup': 1425692550,
                       'last_status_store': 1488319605,
                       'last_upgrade': 1440403352,
                       'module_name': 'Innenraum',
                       'modules': [{'_id': '02:00:00:12:c4:4e',
                                    'battery_percent': 31,
                                    'battery_vp': 4754,
                                    'data_type': ['Temperature', 'Humidity'],
                                    'firmware': 43,
                                    'last_message': 1488319599,
                                    'last_seen': 1488319580,
                                    'last_setup': 1425692514,
                                    'module_name': 'Aussenraum',
                                    'reachable': False,
                                    'rf_status': 73,
                                    'type': 'NAModule1'}],
                       'place': {'altitude': 416,
                                 'city': 'Pusemuckl',
                                 'country': 'DE',
                                 'location': [10.298114914894106,
                                              48.754500000969956],
                                 'timezone': 'Europe/Berlin'},
                       'reachable': False,
                       'station_name': 'Location neu (Innenraum)',
                       'type': 'NAMain',
                       'wifi_status': 58}],
          'user': {'administrative': {'country': 'DE',
                                      'feel_like_algo': 0,
                                      'lang': 'de-DE',
                                      'pressureunit': 0,
                                      'reg_locale': 'de-DE',
                                      'unit': 0,
                                      'windunit': 0},
                   'mail': 'my@mymail.de'}},
 'status': 'ok',
 'time_exec': 0.05098700523376465,
 'time_server': 1689684574}

Side note: With small change, using ID and filtering for requested ID now my grafana-netatmo is back again as well.

philippelt commented 11 months ago

Thanks for signaling the sample code update needed...