Data download via API

mario.frei

I would like to download the data from my Netatmo Home Coach and Netatmo Weather station using Python.
 
All Python libraries for Netatmo seem to be outdated, broken, missing examples or have other issues.
I further struggle with the API documentation provided (https://dev.netatmo.com/apidocumentation/oauth). I manged to create an app. But I couldn't get it working on my own. 
I am comfortable with Python and have accessed sensor APIs from multiple other brands without issues. It is significantly more difficult with Netatmo devices. 
Hence, I was wondering if I am missing something? Am I using the right documentation?
Am I using the right documentation? 

3

Comments

13 comments

  • Comment author
    Leslie Community moderator

    Hello Mario,

    In a nutshell, here is the process to follow : 

    • you must send an /authorize request by using authorization_code method (https://dev.netatmo.com/apidocumentation/oauth#authorization-code) in a web browser. It's mandatory to get explicit user's consent
    • after accepting scopes, you are redirected to the redirect_uri you defined and will find a "code" value as URL parameter
    • then, perform your /token request by using this code value and all other needed information. You will retrieve your first pair of access/refresh_token values
    • Finally, execute the refresh_token process at access_token expiration (3 hours) to maintain connectivity
    • You can now automate the refresh_token process and use a valid access_token value in each of your calls to the API

     

    Have a good day,

    Leslie - Community Manager

    0
  • Comment author
    budslab13

    Hi Leslie,

    Thank you for the quick response.

    Is there always a human interaction required to read out my own sensor data?
    If the fresh token expires, does a human again need to authorize access?

    1
  • Comment author
    Leslie Community moderator

    Hi again,

    Human interaction is only mandatory for the /authorize request in order to get the user's consent. You can then automate the retrieval of the code value and the next steps

    Indeed, if for any reason the refresh_token value is not correct or correctly used to refresh the access_token, you will have to redo the whole process. But please note that the refresh_token doesn't have an expiration date

    Have a good day,

    Leslie - Community Manager

    0
  • Comment author
    mario.frei

    Hi Leslie,
    Thank you again very much for your quick response. 
    It helped to clarify a misunderstanding on my side. I managed to get it working. 

    It appears that the 'scale' parameter for the '/getmeasure/ endpoint is limited to {30min, 1hour, 3hours, 1day, 1week, 1month}.

    Is there a way to get data with a higher sampling rate from the API?

    0
  • Comment author
    Leslie Community moderator

    Hello Mario,

    Indeed, there are the only available scales via the /getmeasure call 

    As far as I remember (I'm pretty sure but to be confirmed) data coming from the /gethomecoachsdata endpoint are updated every 10 minutes. It's the minimum scale we propose via API for this device

    Have a good day,

    Leslie - Community Manager

    0
  • Comment author
    connelly.bill

    Hi Mario,

    I'm in the same position as you. I'm experienced in python and working with other APIs, but I keep getting errors too. Would you be able to share a little code snippet for how you got it working? Thanks in advance

    0
  • Comment author
    mario.frei
    • Edited

    Hi Bill,
    Below are the code snippets that "works". This works for a few days and then I have to manually create new access and refresh tokens on https://dev.netatmo.com/ and update them in the code.
    I couldn't figure out how to make it work for longer without human intervention.

    Credentials

    # Source: https://dev.netatmo.com/apidocumentation/oauth
    # Notes:
    # - Register an app on https://dev.netatmo.com/ (Top right, 'My apps')
    # - Device: Home coach

    # User credentials
    EMAIL = ''
    PASSWORD = ''

    # App credentials
    CLIENT_ID = '' # Sometimes called app ID, looks like: '5989eA5B1AF3d8fc015d4215'
    CLIENT_SECRET = '' # looks like: 'BHtNLOTNSsbQFSqpCoGsQkOCjZJrothMwW'

    # These tokens are generated in the 'app' created on dev.netatmo.com (scope: read_homecoach)
    netatmo_access_token = '' # looks like: 'cde723283f7ab2d2786fb1f1|506be379de09b2ff5d3e25e56ebb8cdf'
    netatmo_refresh_token = ''# looks like: 'cde723283f7ab2d2786fb1f1|9977bb61decf0ed99db97b096e66fe77'

    SCOPE = 'read_homecoach'
    MAC = '' # MAC address of the device looks like: '21:ff:31:69:2d:19'

    Fetch data

    date_start = datetime.now()-timedelta(days=1)
    date_start = int(date_start.replace(tzinfo=None).timestamp())
    date_end = int(datetime.now().timestamp())

    URL = 'https://api.netatmo.com/api/getmeasure'

    # Create the payload for API call
    params={'device_id': MAC,
            'module_id': MAC,
            'scale': '30min',
            'type': 'temperature,humidity,co2,pressure,noise',
            'date_begin': date_start,
            'date_end': date_end,
            'limit': '1024',
            'optimize': 'true',
            'real_time': 'true'}
    print(params)

    # Create the header for API call
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {netatmo_access_token}"
    }

    # Make API call
    response = requests.get(url=URL, params=params, headers=headers)
    print(response.content)

    # Parse response data
    body = response.json()['body'][0]

    values = dict()
    values['temperature'] = [val[0] for val in body['value']]
    values['humidity'] = [val[1] for val in body['value']]
    values['co2'] = [val[2] for val in body['value']]
    values['pressure'] = [val[3] for val in body['value']]
    values['noise'] = [val[4] for val in body['value']]

    # Add timestamps
    datetime_start = datetime.fromtimestamp(body['beg_time'])

    # Parse sampling interval
    step_time = 1 # This default value should be overwritten, if there is only one sample
    if 'step_time' in payload:
        step_time = payload['step_time']

    # Create timestamps
    values['timestamp'] = [datetime_start + timedelta(seconds=i*step_time) for i in range(0, len(values['temperature']))]

    # Create dataframe
    df = pd.DataFrame.from_dict(values)
    df = df.set_index('timestamp')
    df.head()

    Refresh token

    # Create payload
    URL = 'https://api.netatmo.com/oauth2/token'
    payload={'grant_type': 'refresh_token',
             'refresh_token': netatmo_refresh_token,
             'client_id': CLIENT_ID,
           'client_secret': CLIENT_SECRET}

    # Create headers
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }

    # Make API call
    response = requests.post(url=URL, data=payload, headers=headers)
    print(response.content)

    # Parse response data
    netatmo_access_token = response.json()['access_token']
    print(response.status_code)
     
    0
  • Comment author
    connelly.bill
    • Edited

    Thanks so much.

     

    > This works for a few days and then I have to manually create new access and refresh token

     

    Yes. I am also getting this problem. This is very frustrating. I get the Netatmo wants to keep our data safe, but I feel like if I want to access my own data, there should be a simpler approach. 

    2
  • Comment author
    mario.frei

    I agree. 
    I've bought a few more sensors from other reputable brands and it took me less than an hour to interface their API's and read out the data. And for those the setup is now working for weeks and months without any issues. 

    2
  • Comment author
    wjk

    Can you tell me some other brands that work? I'm getting fed up with Netatmo and their lack of decent support and constant fiddling with their APIs.

    1
  • Comment author
    mario.frei

    I've been very happy with the following devices:

    - Airgradient: https://www.airgradient.com/indoor/
    - Ubibot AQS1: https://www.ubibot.com/ubibot-aqs1/
    - Atmo Cube: https://atmotube.com/atmocube

    Setting up the APIs took about one hour each, and it has worked reliably for several months.

    0
  • Comment author
    wjk

    Amazing, thanks, will try one of those.

    1
  • Comment author
    darrell.hayward

    I agree entirely - I am sick to the teeth with this auth. It used to work so well. I am giving up with the product and going elsewhere.

    1

Please sign in to leave a comment.