Help gathering Temperature from API response - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help gathering Temperature from API response (/thread-38987.html) |
Help gathering Temperature from API response - road102 - Dec-16-2022 Hello I want to extract the temperature from my solar panels but the format of the API response is giving me headaches !! pi@raspberrypi:~ $ python3 Python 3.7.3 (default, Oct 31 2022, 14:04:00) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import http.client, urllib, requests >>> response=requests.get("https://monitoringapi.solaredge.com/equipment/XXX/YYY/data?startTime=2022-12-16%2012:00:00&endTime=2022-12-16%2012:05:00&api_key=ZZZ") >>> response_data= response.json() >>> response_data {'data': {'count': 1, 'telemetries': [{'date': '2022-12-16 12:03:50', 'totalActivePower': 4118.73, 'dcVoltage': 386.966, 'groundFaultResistance': 11000.0, 'powerLimit': 100.0, 'totalEnergy': 973109.0, 'temperature': 46.1466, 'inverterMode': 'MPPT', 'operationMode': 0, 'vL1ToN': 121.436, 'vL2ToN': 121.364, 'L1Data': {'acCurrent': 16.9855, 'acVoltage': 242.799, 'acFrequency': 59.9962, 'apparentPower': 4125.01, 'activePower': 4118.73, 'reactivePower': 227.599, 'cosPhi': 1.0}}]}} >>> response_data['data']['count'] 1 So far all is good, I am able to store the API response and get the first value. But retrieving the temperature is the problem >>> response_data['data']['count']['telemetries']['date']['temperature'] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not subscriptable Any ideas on how to get the Temperature?? Thanks ! RE: Help gathering Temperature from API response - Axel_Erfurt - Dec-16-2022 print(response_data['data']['telemetries'][0]['temperature'])
RE: Help gathering Temperature from API response - deanhystad - Dec-16-2022 Does it help to arrange response_data like this? { 'data': { 'count': 1, 'telemetries': [ { 'date': '2022-12-16 12:03:50', 'totalActivePower': 4118.73, 'dcVoltage': 386.966, 'groundFaultResistance': 11000.0, 'powerLimit': 100.0, 'totalEnergy': 973109.0, 'temperature': 46.1466, 'inverterMode': 'MPPT', 'operationMode': 0, 'vL1ToN': 121.436, 'vL2ToN': 121.364, 'L1Data': { 'acCurrent': 16.9855, 'acVoltage': 242.799, 'acFrequency': 59.9962, 'apparentPower': 4125.01, 'activePower': 4118.73, 'reactivePower': 227.599, 'cosPhi': 1.0} } ] } }'telemetries' and 'count' are keys in the same dictionary. The value for 'count' is 1, and the value for 'telemetries' is a list of dictionaries that must contain telemetry data. This prints date and time from each dictionary of telemetry data. for telemetry in response_data['data']['telemetries']: print(telemetry['date'], telemetry['temperature']) RE: Help gathering Temperature from API response - road102 - Dec-16-2022 Yes! this works Thank you deanhystad RE: Help gathering Temperature from API response - road102 - Dec-16-2022 sweet RE: Help gathering Temperature from API response - road102 - Dec-16-2022 sweet |