English     Dutch     French     German     Spanish

Retrieve device telemetry

Last modified by Bart Verheecke on 2024/05/06 09:28

Python example

Use the following example to retrieve telemetry timeseries data from devices.

from eniris import ApiDriver
from datetime import datetime, timezone


def example():
    apiUsername = "username"    # Your Insights username
    apiPassword = "password"    # Your Insights password

   # Create an API driver
    driver = ApiDriver(apiUsername, apiPassword)
   
   # Make a query body
    startTime = datetime(2023, 10, 11, hour=0, minute=0, second=0, tzinfo=timezone.utc)
    endTime = datetime(2023, 10, 11, hour=0, minute=2, second=0, tzinfo=timezone.utc)
   
   # The body of this example is to get data from two devices for the fields 'actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh'
   # from database "eniris", retention policy "rp_one_m" and measurement "submeteringMetrics"
   # For the terminology, see https://docs.influxdata.com/influxdb/v1/concepts/key_concepts/
   
   # For each device you can know which database, retention policy, tags, measurements and fields exist by looking at the nodeInfluxSeries in the metadata object of the device.
   # The devices you have access to can be retrieved using the https://api.eniris.be/v1/devices GET call. In the smart grid controller you can also look to the debugger tab.
   # The meaning of the fields is documented at https://wiki.eniris.be/wiki/publicinformation/view/Standards/Metrics%20for%20solar%20%26%20batteries/
    body = [
       
       # Device 1:
        {
           "select": ['actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh'],
           "from": {
               "database": "beauvent",
               "retentionPolicy": "rp_one_m",           # Retention policy. Possible values: rp_one_s, rp_one_m, rp_ten_m, rp_one_h, rp_six_h
               "measurement": "submeteringMetrics"},
           "where": {
               # Get all datapoints between two hours ago and the look ahead time
               "time": [{ "operator": ">=", "value": startTime.timestamp()*1000 }, { "operator": "<=", "value": endTime.timestamp()*1000 }],
               "tags": {'nodeId': 'nodeIdOfDevice1'}                # Tags; at least nodeId must be present, and possibly more tags
            },
        },
       
       # Device 2:
        {
           "select": ['actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh'],
           "from": {
               "database": "beauvent",
               "retentionPolicy": "rp_one_m",           # Retention policy. Possible values: rp_one_s, rp_one_m, rp_ten_m, rp_one_h, rp_six_h
               "measurement": "submeteringMetrics"},
           "where": {
               # Get all datapoints between two hours ago and the look ahead time
               "time": [{ "operator": ">=", "value": startTime.timestamp()*1000 }, { "operator": "<=", "value": endTime.timestamp()*1000 }],
               "tags": {'nodeId': 'nodeIdOfDevice2'}                # Tags; at least nodeId must be present, and possibly more tags
            },
        }
    ]

   try:
        resp = driver.post('https://api.eniris.be/v1/telemetry/query', json=body)
   except Exception as e:
       raise Exception("Got an error while querying Insights: " + str(e))
   
   return resp.json()
   
   # An example of how such a response looks:
   # [
   #     {
   #         "statement_id": 0,
   #         "series": [
   #             {
   #                 "name": "submeteringMetrics",
   #                 "tags": {
   #                     "nodeId": "nodeIdOfDevice1"
   #                 },
   #                 "columns": [
   #                     "time",
   #                     "actualPowerTot_W",
   #                     "importedEnergyDeltaTot_Wh",
   #                     "exportedEnergyDeltaTot_Wh"
   #                 ],
   #                 "values": [
   #                     [
   #                         "2023-10-11T00:00:08.261251072Z",
   #                         284,
   #                         10.475263329722221,
   #                         0
   #                     ],
   #                     [
   #                         "2023-10-11T00:01:13.300770048Z",
   #                         280,
   #                         10.097605051388888,
   #                         0
   #                     ]
   #                 ]
   #             }
   #         ]
   #     },
   #     {
   #         "statement_id": 1,
   #         "series": [
   #             {
   #                 "name": "submeteringMetrics",
   #                 "tags": {
   #                     "nodeId": "nodeIdOfDevice2"
   #                 },
   #                 "columns": [
   #                     "time",
   #                     "actualPowerTot_W",
   #                     "importedEnergyDeltaTot_Wh",
   #                     "exportedEnergyDeltaTot_Wh"
   #                 ],
   #                 "values": [
   #                     [
   #                         "2023-10-11T00:00:08.308108032Z",
   #                         284,
   #                         10.178023648333332,
   #                         0
   #                     ],
   #                     [
   #                         "2023-10-11T00:01:13.34544384Z",
   #                         280,
   #                         10.475263329722221,
   #                         0
   #                     ]
   #                 ]
   #             }
   #         ]
   #     }
   # ]
   

example()
 

Applications

(c) Eniris, 2024