Wiki source code of Retrieve device telemetry
Last modified by Bart Verheecke on 2024/05/06 09:28
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | = Python example = | ||
| 2 | |||
| 3 | Use the following example to retrieve telemetry timeseries data from devices. | ||
| 4 | |||
| 5 | |||
| 6 | {{code language="python"}} | ||
| 7 | from eniris import ApiDriver | ||
| 8 | from datetime import datetime, timezone | ||
| 9 | |||
| 10 | |||
| 11 | def example(): | ||
| 12 | apiUsername = "username" # Your Insights username | ||
| 13 | apiPassword = "password" # Your Insights password | ||
| 14 | |||
| 15 | # Create an API driver | ||
| 16 | driver = ApiDriver(apiUsername, apiPassword) | ||
| 17 | |||
| 18 | # Make a query body | ||
| 19 | startTime = datetime(2023, 10, 11, hour=0, minute=0, second=0, tzinfo=timezone.utc) | ||
| 20 | endTime = datetime(2023, 10, 11, hour=0, minute=2, second=0, tzinfo=timezone.utc) | ||
| 21 | |||
| 22 | # The body of this example is to get data from two devices for the fields 'actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh' | ||
| 23 | # from database "eniris", retention policy "rp_one_m" and measurement "submeteringMetrics" | ||
| 24 | # For the terminology, see https://docs.influxdata.com/influxdb/v1/concepts/key_concepts/ | ||
| 25 | |||
| 26 | # 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. | ||
| 27 | # 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. | ||
| 28 | # The meaning of the fields is documented at https://wiki.eniris.be/wiki/publicinformation/view/Standards/Metrics%20for%20solar%20%26%20batteries/ | ||
| 29 | body = [ | ||
| 30 | |||
| 31 | # Device 1: | ||
| 32 | { | ||
| 33 | "select": ['actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh'], | ||
| 34 | "from": { | ||
| 35 | "database": "beauvent", | ||
| 36 | "retentionPolicy": "rp_one_m", # Retention policy. Possible values: rp_one_s, rp_one_m, rp_ten_m, rp_one_h, rp_six_h | ||
| 37 | "measurement": "submeteringMetrics"}, | ||
| 38 | "where": { | ||
| 39 | # Get all datapoints between two hours ago and the look ahead time | ||
| 40 | "time": [{ "operator": ">=", "value": startTime.timestamp()*1000 }, { "operator": "<=", "value": endTime.timestamp()*1000 }], | ||
| 41 | "tags": {'nodeId': 'nodeIdOfDevice1'} # Tags; at least nodeId must be present, and possibly more tags | ||
| 42 | }, | ||
| 43 | }, | ||
| 44 | |||
| 45 | # Device 2: | ||
| 46 | { | ||
| 47 | "select": ['actualPowerTot_W', 'importedEnergyDeltaTot_Wh', 'exportedEnergyDeltaTot_Wh'], | ||
| 48 | "from": { | ||
| 49 | "database": "beauvent", | ||
| 50 | "retentionPolicy": "rp_one_m", # Retention policy. Possible values: rp_one_s, rp_one_m, rp_ten_m, rp_one_h, rp_six_h | ||
| 51 | "measurement": "submeteringMetrics"}, | ||
| 52 | "where": { | ||
| 53 | # Get all datapoints between two hours ago and the look ahead time | ||
| 54 | "time": [{ "operator": ">=", "value": startTime.timestamp()*1000 }, { "operator": "<=", "value": endTime.timestamp()*1000 }], | ||
| 55 | "tags": {'nodeId': 'nodeIdOfDevice2'} # Tags; at least nodeId must be present, and possibly more tags | ||
| 56 | }, | ||
| 57 | } | ||
| 58 | ] | ||
| 59 | |||
| 60 | try: | ||
| 61 | resp = driver.post('https://api.eniris.be/v1/telemetry/query', json=body) | ||
| 62 | except Exception as e: | ||
| 63 | raise Exception("Got an error while querying Insights: " + str(e)) | ||
| 64 | |||
| 65 | return resp.json() | ||
| 66 | |||
| 67 | # An example of how such a response looks: | ||
| 68 | # [ | ||
| 69 | # { | ||
| 70 | # "statement_id": 0, | ||
| 71 | # "series": [ | ||
| 72 | # { | ||
| 73 | # "name": "submeteringMetrics", | ||
| 74 | # "tags": { | ||
| 75 | # "nodeId": "nodeIdOfDevice1" | ||
| 76 | # }, | ||
| 77 | # "columns": [ | ||
| 78 | # "time", | ||
| 79 | # "actualPowerTot_W", | ||
| 80 | # "importedEnergyDeltaTot_Wh", | ||
| 81 | # "exportedEnergyDeltaTot_Wh" | ||
| 82 | # ], | ||
| 83 | # "values": [ | ||
| 84 | # [ | ||
| 85 | # "2023-10-11T00:00:08.261251072Z", | ||
| 86 | # 284, | ||
| 87 | # 10.475263329722221, | ||
| 88 | # 0 | ||
| 89 | # ], | ||
| 90 | # [ | ||
| 91 | # "2023-10-11T00:01:13.300770048Z", | ||
| 92 | # 280, | ||
| 93 | # 10.097605051388888, | ||
| 94 | # 0 | ||
| 95 | # ] | ||
| 96 | # ] | ||
| 97 | # } | ||
| 98 | # ] | ||
| 99 | # }, | ||
| 100 | # { | ||
| 101 | # "statement_id": 1, | ||
| 102 | # "series": [ | ||
| 103 | # { | ||
| 104 | # "name": "submeteringMetrics", | ||
| 105 | # "tags": { | ||
| 106 | # "nodeId": "nodeIdOfDevice2" | ||
| 107 | # }, | ||
| 108 | # "columns": [ | ||
| 109 | # "time", | ||
| 110 | # "actualPowerTot_W", | ||
| 111 | # "importedEnergyDeltaTot_Wh", | ||
| 112 | # "exportedEnergyDeltaTot_Wh" | ||
| 113 | # ], | ||
| 114 | # "values": [ | ||
| 115 | # [ | ||
| 116 | # "2023-10-11T00:00:08.308108032Z", | ||
| 117 | # 284, | ||
| 118 | # 10.178023648333332, | ||
| 119 | # 0 | ||
| 120 | # ], | ||
| 121 | # [ | ||
| 122 | # "2023-10-11T00:01:13.34544384Z", | ||
| 123 | # 280, | ||
| 124 | # 10.475263329722221, | ||
| 125 | # 0 | ||
| 126 | # ] | ||
| 127 | # ] | ||
| 128 | # } | ||
| 129 | # ] | ||
| 130 | # } | ||
| 131 | # ] | ||
| 132 | |||
| 133 | |||
| 134 | example() | ||
| 135 | {{/code}} |