Logging Evohome - yet another topic!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Mr_Ess_Jay
    Automated Home Lurker
    • Dec 2017
    • 6

    Logging Evohome - yet another topic!

    Hi all,

    Firstly, let me be clear that I know little about coding, i'm at the beginning of a what seems to be a very steep learning curve so go easy! My background involves hammers and not keyboards!

    I have a Raspberry Pi (3) with the hope of learning how to do some basic coding and the first project I have undertaken is to log all of my temperatures in Evohome. I found the following script from a kind poster on this very forum, entered it into a Python 3 environment and keep coming up with the following error. I understand what it is trying to say but I do not understand how to fix it. Can anyone offer any advice.

    Traceback (most recent call last):
    File "/home/pi/USBHDD/evohome.py", line 18, in <module>
    userinfo = json.loads(response.content)
    File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
    TypeError: the JSON object must be str, not 'bytes'

    Code below:
    Code:
    # Script to monitor and read temperatures from Honeywell EvoHome Web API
    
    # Load required libraries
    import requests
    import json
    import datetime
    import time
    
    # Ser your login details in the 2 fields below
    USERNAME = '########@gmail.com'
    PASSWORD = '########'
    
    # Initial JSON POST to the website to return your userdata
    url = 'https://rs.alarmnet.com/TotalConnectComfort/WebAPI/api/Session'
    postdata = {'Username':USERNAME, 'Password':PASSWORD, 'ApplicationId':'91db1612-73fd-4500-91b2-e63b069b185c'}
    headers = {'content-type':'application/json'}
    response = requests.post(url,data=json.dumps(postdata),headers=headers)
    userinfo = json.loads(response.content)
    
    # Extract the sessionId and your userid from the response
    userid = userinfo['userInfo']['userID']
    sessionId = userinfo['sessionId']
    
    # Next, using your userid, get all the data back about your site
    # Print out the headers for our temperatures, this let's us input to .csv file easier for charts
    url = 'https://rs.alarmnet.com/TotalConnectComfort/WebAPI/api/locations?userId=%s&allData=True' % userid
    headers['sessionId'] = sessionId
    response = requests.get(url,data=json.dumps(postdata),headers=headers)
    fullData = json.loads(response.content)[0]
    (print) ('Evotemps', 'Evohometemps.csv')
    for device in fullData['devices']:
        print (device['name'],',',)
    print
    time.sleep (5)
    
    # Infinite loop every 5 minutes output temperatures
    while True:
    # Next, using your userid, get all the data back about your site
        url = 'https://rs.alarmnet.com/TotalConnectComfort/WebAPI/api/locations?userId=%s&allData=True' % userid
    headers['sessionId'] = sessionId
    response = requests.get(url,data=json.dumps(postdata),headers =headers)
    fullData = json.loads(response.content)[0]
    
    # Get current time and then print all thermostat readings out
    from datetime import datetime
    print ,datetime.datetime.now().strftime('%H:%M:%S'),',',
    for device in fullData['devices']:
        print (device['thermostat']['indoorTemperature'],',',)
    print
    time.sleep(300)
    Can anyone help? I've tried adding .decode('utf-8') to the end of userinfo = json.loads(response.content) but no luck either.
  • DBMandrake
    Automated Home Legend
    • Sep 2014
    • 2361

    #2
    Instead of trying to reinvent the wheel making the raw http API calls directly in your script, you're better off using watchforstock's evohome client python bindings:

    Python client to access the Evohome web service. Contribute to watchforstock/evohome-client development by creating an account on GitHub.


    All of the minutia of how to send queries to the server and parse the responses is handled for you in the library allowing you to focus on what you really want - retrieving data or sending commands.

    Here is an example of just how easy it is to use, polling for and printing all zone temperature information using the V1 API, which provides high resolution temperature readings:

    Code:
    #!/usr/bin/python
    
    from evohomeclient import EvohomeClient
    
    client = EvohomeClient('email', 'password')
    
    for device in client.temperatures():
        print device
    I'd recommend you use the V2 API for everything except retrieving measured zone temperatures, which have higher resolution using the V1 API above. watchforstock's library supports both API's.
    Last edited by DBMandrake; 30 December 2017, 10:28 PM.

    Comment

    • Karrimor
      Automated Home Jr Member
      • Oct 2016
      • 24

      #3
      As DBMandrake mentioned, you are better off using the existing Evohome Python modules.

      As you are using Python 3.x, you will need to make a small adjustment to the Python 2.x code. The print statement has now been replaced with a print() function in Python 3.0:

      Code:
      from evohomeclient import EvohomeClient
      
      client = EvohomeClient('email', 'password')
      
      for device in client.temperatures():
         [B]print(device)[/B]
      Calling the client.temperatures() function returns a dictionary for each device, which includes the sensor ID, the name of the sensor, the type of sensor and the current temperature reading:

      Code:
      {'thermostat': 'EMEA_ZONE', 'id': 2039413, 'name': 'Bedroom 1', 'temp': 18.68, 'setpoint': 15.5}
      {'thermostat': 'EMEA_ZONE', 'id': 2039413, 'name': 'Kitchen', 'temp': 15.97, 'setpoint': 12.0}
      ...
      Last edited by Karrimor; 1 January 2018, 01:46 AM.

      Comment

      • HenGus
        Automated Home Legend
        • May 2014
        • 1001

        #4
        Happy New Year to all. Out-of-interest, can this script be run on a Synology NAS? If so, can anyone recommend a Python guide for dummies (or in my case, oldies)?

        Comment

        • bruce_miranda
          Automated Home Legend
          • Jul 2014
          • 2309

          #5
          I works absolutely fine on a Synology. I ran it for quite a while until I moved to a QNAP. Runs there too.

          Comment

          • Mr_Ess_Jay
            Automated Home Lurker
            • Dec 2017
            • 6

            #6
            Thank you all for your help, I really do appreciate it!

            I was hoping to use this as a project to get me into coding at a basic level but I'm content looking at the code and understanding what it is doing and when as a close second!


            Cheers all!

            Comment

            Working...
            X