Python Forum
Help gathering Temperature from API response
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help gathering Temperature from API response
#1
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 !
Reply
#2
print(response_data['data']['telemetries'][0]['temperature'])
Output:
46.1466
road102 likes this post
Reply
#3
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'])
ndc85430 likes this post
Reply
#4
Yes! this works

Thank you deanhystad
Reply
#5
sweet
Reply
#6
sweet
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,867 Apr-16-2021, 07:49 PM
Last Post: apollo
  monitoring the temperature of the CPU with Python apollo 2 8,787 Apr-13-2021, 05:39 PM
Last Post: apollo
  print CPU temperature samuelbachorik 12 6,123 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  Read temperature once mada72 2 2,797 Apr-28-2019, 07:18 PM
Last Post: mada72
  Trivial novice question: information gathering tools in Python Drone4four 1 2,262 Nov-10-2018, 01:41 AM
Last Post: Larz60+
  Message Passing library for HPC and progressive gathering Dunatotatos 1 2,081 Aug-26-2018, 04:44 AM
Last Post: Dunatotatos
  Importing a temperature converting module RedSkeleton007 2 8,056 Nov-12-2017, 01:20 PM
Last Post: sparkz_alot
  Temperature Code help? 20AJ0931 2 6,560 Apr-08-2017, 12:35 AM
Last Post: snippsat
  Temperature value code 20AJ0931 10 13,522 Mar-19-2017, 11:05 PM
Last Post: 20AJ0931

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020