Skip to content

Commit 90c0d4b

Browse files
Meterstatistieken doorgeven via API #820
1 parent 8012f57 commit 90c0d4b

File tree

20 files changed

+1289
-656
lines changed

20 files changed

+1289
-656
lines changed

docs/api.rst

+211-27
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,39 @@ Besides allowing the API to listen for requests, you will also need send your AP
2626
The API key can be found on the same page as in the screenshot above.
2727
The application generates one for you initially, but feel free to alter the API key when required.
2828

29-
You should pass it in the header of every API call. The header should be defined as ``X-AUTHKEY`` with value ``<key>``. See below for an example.
30-
31-
Alternatively, since ``v2.1.0``, you can use ``Authorization`` with value ``Token <key>``.
29+
You should pass it in the header of every API call. See below for an example.
30+
Since ``v2.1.0``, you should not longer use ``X-AUTHKEY``.
31+
Use ``Authorization`` with value ``Token <key>`` instead.
3232

3333
Examples
3434
~~~~~~~~
3535

3636
Using ``cURL``::
3737

38-
curl http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading \
38+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
39+
curl http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading \
3940
-d 'telegram=xxxxx' \
40-
-H 'X-AUTHKEY: <YOUR-API-KEY>'
41+
-H 'Authorization: Token YOUR-API-KEY'
4142
42-
# Or use
43+
# Or use
4344

44-
curl http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading \
45+
curl http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading \
4546
-d 'telegram=xxxxx' \
46-
-H 'Authorization: Token <YOUR-API-KEY>'
47+
-H 'Authorization: Token YOUR-API-KEY'
4748
4849
Using ``requests``::
4950

50-
requests.post(
51+
requests.post(
5152
'http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading',
52-
headers={'X-AUTHKEY': '<YOUR-API-KEY>'},
53+
headers={'Authorization': 'Token YOUR-API-KEY'},
5354
data={'telegram': 'xxxxx'},
54-
)
55+
)
5556
56-
# Or use
57+
# Or use
5758

58-
requests.post(
59+
requests.post(
5960
'http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading',
60-
headers={'Authorization': 'Token <YOUR-API-KEY>'},
61+
headers={'Authorization': 'Token YOUR-API-KEY'},
6162
data={'telegram': 'xxxxx'},
6263
)
6364

@@ -127,7 +128,7 @@ Example
127128
# Register telegram by simply sending it to the application with a POST request.
128129
response = requests.post(
129130
'http://YOUR-DSMR-URL/api/v1/datalogger/dsmrreading',
130-
headers={'X-AUTHKEY': 'YOUR-API-KEY'},
131+
headers={'Authorization': 'Token YOUR-API-KEY'},
131132
data={'telegram': telegram_string},
132133
)
133134

@@ -229,10 +230,11 @@ Example
229230

230231
Using **cURL** (commandline)::
231232

233+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
232234
# Please note that the plus symbol "+" has been replaced by "%2B" here, to make it work for cURL.
233235
curl http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading \
234236
-d 'timestamp=2017-04-15T00:00:00%2B02&electricity_currently_delivered=1.5&electricity_currently_returned=0.025&electricity_delivered_1=2000&electricity_delivered_2=3000&electricity_returned_1=0&electricity_returned_2=0' \
235-
-H 'X-AUTHKEY: YOUR-API-KEY' | python -m json.tool
237+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
236238

237239

238240
Using **requests** (Python)::
@@ -242,7 +244,7 @@ Using **requests** (Python)::
242244

243245
response = requests.post(
244246
'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading',
245-
headers={'X-AUTHKEY': 'YOUR-API-KEY'},
247+
headers={'Authorization': 'Token YOUR-API-KEY'},
246248
data={
247249
'electricity_currently_delivered': 1.500,
248250
'electricity_currently_returned': 0.025,
@@ -321,8 +323,9 @@ This demonstrates how to fetch all readings stored, without using any of the par
321323

322324
Using **cURL** (commandline)::
323325

324-
curl 'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading' \
325-
-H 'X-AUTHKEY: YOUR-API-KEY' | python -m json.tool
326+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
327+
curl 'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading' \
328+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
326329

327330

328331
Using **requests** (Python)::
@@ -332,7 +335,7 @@ Using **requests** (Python)::
332335

333336
response = requests.get(
334337
'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading',
335-
headers={'X-AUTHKEY': 'YOUR-API-KEY'},
338+
headers={'Authorization': 'Token YOUR-API-KEY'},
336339
)
337340

338341
if response.status_code != 200:
@@ -380,8 +383,9 @@ This demonstrates how to fetch the latest reading stored. Therefor we request al
380383

381384
Using **cURL** (commandline)::
382385

386+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
383387
curl 'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading?ordering=-timestamp&limit=1' \
384-
-H 'X-AUTHKEY: YOUR-API-KEY' | python -m json.tool
388+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
385389

386390

387391
Using **requests** (Python)::
@@ -391,7 +395,7 @@ Using **requests** (Python)::
391395

392396
response = requests.get(
393397
'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading?ordering=-timestamp&limit=1',
394-
headers={'X-AUTHKEY': 'YOUR-API-KEY'},
398+
headers={'Authorization': 'Token YOUR-API-KEY'},
395399
)
396400

397401
if response.status_code != 200:
@@ -437,9 +441,10 @@ This demonstrates how to fetch all readings within a month. We limit the search
437441

438442
Using **cURL** (commandline)::
439443

444+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
440445
# Note that the whitespace in the timestamps has been converted to '%20' for cURL.
441446
curl 'http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading?timestamp__gte=2017-02-01%2000:00:00&timestamp__lte=2017-03-01%2000:00:00' \
442-
-H 'X-AUTHKEY: YOUR-API-KEY' | python -m json.tool
447+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
443448

444449

445450
Using **requests** (Python)::
@@ -449,7 +454,7 @@ Using **requests** (Python)::
449454

450455
response = requests.get(
451456
'http://YOUR-DSMR-URL/api/v2/statistics/day?timestamp__gte=2017-02-01 00:00:00&timestamp__lte=2017-03-01 00:00:00',
452-
headers={'X-AUTHKEY': 'YOUR-API-KEY'},
457+
headers={'Authorization': 'Token YOUR-API-KEY'},
453458
)
454459

455460
if response.status_code != 200:
@@ -460,7 +465,7 @@ Using **requests** (Python)::
460465
461466
**Result**::
462467

463-
# This should present you a set of all readings in the month we selected.
468+
# This should present you a set of all readings in the month selected.
464469
{
465470
"count": 240968,
466471
"next": "http://YOUR-DSMR-URL/api/v2/datalogger/dsmrreading?limit=25&offset=25&timestamp__gte=2017-02-01+00%3A00%3A00&timestamp__lte=2017-03-01+00%3A00%3A00",
@@ -495,7 +500,187 @@ Using **requests** (Python)::
495500

496501

497502
----
498-
503+
504+
505+
``GET`` - ``/datalogger/meter-statistics``
506+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
507+
508+
Retrieve meter statistics.
509+
510+
.. note::
511+
512+
This endpoint was added in DSMR-reader ``v3.1``
513+
514+
515+
URI
516+
~~~
517+
Full path: ``/api/v2/datalogger/meter-statistics``
518+
519+
520+
Parameters
521+
~~~~~~~~~~
522+
None.
523+
524+
525+
Response
526+
~~~~~~~~
527+
``HTTP 200`` on success. Body contains the data in JSON format. Any other status code on failure.
528+
529+
530+
Example
531+
~~~~~~~
532+
533+
Using **cURL** (commandline)::
534+
535+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
536+
curl http://YOUR-DSMR-URL/api/v2/datalogger/meter-statistics \
537+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
538+
539+
540+
**Result**::
541+
542+
{
543+
"id": 1,
544+
"timestamp": "2020-01-15T20:13:40+01:00",
545+
"dsmr_version": "40",
546+
"electricity_tariff": 1,
547+
"power_failure_count": 3,
548+
"long_power_failure_count": 0,
549+
"voltage_sag_count_l1": 1,
550+
"voltage_sag_count_l2": 2,
551+
"voltage_sag_count_l3": 3,
552+
"voltage_swell_count_l1": 0,
553+
"voltage_swell_count_l2": 0,
554+
"voltage_swell_count_l3": 0,
555+
"rejected_telegrams": 99,
556+
"latest_telegram": "/XMX5LGBBFFB123456789\r\n\r\n1-3:0.2.8(40)\r\n0-0:1.0.0(200115201340W)\r\n0-0:96.1.1(12345678901234567890123456789000)\r\n1-0:1.8.1(007952.261*kWh)\r\n1-0:2.8.1(000000.000*kWh)\r\n1-0:1.8.2(004771.357*kWh)\r\n1-0:2.8.2(000000.000*kWh)\r\n0-0:96.14.0(0001)\r\n1-0:1.7.0(02.507*kW)\r\n1-0:2.7.0(00.000*kW)\r\n0-0:96.7.21(00003)\r\n0-0:96.7.9(00000)\r\n1-0:99.97.0(0)(0-0:96.7.19)\r\n1-0:32.32.0(00001)\r\n1-0:52.32.0(00002)\r\n1-0:72.32.0(00003)\r\n1-0:32.36.0(00000)\r\n1-0:52.36.0(00000)\r\n1-0:72.36.0(00000)\r\n0-0:96.13.1()\r\n0-0:96.13.0()\r\n1-0:32.7.0(225.0*V)\r\n1-0:52.7.0(232.1*V)\r\n1-0:72.7.0(233.2*V)\r\n1-0:31.7.0(000*A)\r\n1-0:51.7.0(000*A)\r\n1-0:71.7.0(001*A)\r\n1-0:21.7.0(01.407*kW)\r\n1-0:41.7.0(00.765*kW)\r\n1-0:61.7.0(00.334*kW)\r\n1-0:22.7.0(00.000*kW)\r\n1-0:42.7.0(00.000*kW)\r\n1-0:62.7.0(00.000*kW)\r\n!013B"
557+
}
558+
559+
560+
.. warning::
561+
562+
Please note that all timestamps **returned** are in **UTC (CET -1 / CEST -2)**. This is indicated as well by the timestamps ending with a 'Z' (Zulu timezone).
563+
564+
565+
----
566+
567+
568+
``PATCH`` - ``/datalogger/meter-statistics``
569+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
570+
571+
Manually updates any meter statistics fields.
572+
573+
.. note::
574+
575+
This endpoint was added in DSMR-reader ``v3.1``
576+
577+
578+
.. warning::
579+
580+
You should **not use this if you're using the v1 datalogger to push your telegrams**, as they will collide and overwrite each other's data!
581+
582+
583+
URI
584+
~~~
585+
Full path: ``/api/v2/datalogger/meter-statistics``
586+
587+
588+
Parameters
589+
~~~~~~~~~~
590+
Since this is a ``PATCH`` operation, **all parameters are optional**.
591+
592+
- ``timestamp`` (*datetime*) - Timestamp indicating the last update. The builtin datalogger uses the timestamp of the telegram for this.
593+
- ``dsmr_version`` (*string*) - DSMR protocol version string. Should be something like ``42`` (4.2) or ``50`` (5.0)
594+
- ``power_failure_count`` (*int*) - Number of power failures in any phase
595+
- ``long_power_failure_count`` (*int*) - Number of long power failures in any phase
596+
- ``voltage_sag_count_l1`` (*int*) - Number of voltage sags/dips in phase L1
597+
- ``voltage_sag_count_l2`` (*int*) - Number of voltage sags/dips in phase L2 (polyphase meters only)
598+
- ``voltage_sag_count_l3`` (*int*) - Number of voltage sags/dips in phase L3 (polyphase meters only)
599+
- ``voltage_swell_count_l1`` (*int*) - Number of voltage swells in phase L1
600+
- ``voltage_swell_count_l2`` (*int*) - Number of voltage swells in phase L2 (polyphase meters only)
601+
- ``voltage_swell_count_l3`` (*int*) - Number of voltage swells in phase L3 (polyphase meters only)
602+
- ``rejected_telegrams`` (*int*) - Number of rejected telegrams due to invalid CRC checksum
603+
- ``latest_telegram`` (*string*) - The latest telegram succesfully read
604+
605+
All parameters, except for ``timestamp`` and ``rejected_telegrams`` are **nullable**. Send an empty value to make them ``null``.
606+
607+
.. note::
608+
609+
**datetime format** = ``YYYY-MM-DDThh:mm[:ss][+HH:MM|-HH:MM|Z]``, i.e.: ``2017-01-01T12:00:00+01`` (CET), ``2017-04-15T12:00:00+02`` (CEST) or ``2017-04-15T100:00:00Z`` (UTC).
610+
611+
612+
Response
613+
~~~~~~~~
614+
``HTTP 200`` on success. Body contains the updated data in JSON format. Any other status code on failure.
615+
616+
617+
Example
618+
~~~~~~~
619+
**Data** to update::
620+
621+
timestamp: 2020-01-15T12:34:56+01
622+
dsmr_version: '50'
623+
power_failure_count: 1
624+
voltage_sag_count_l1: 5
625+
voltage_swell_count_l1: 6
626+
latest_telegram: null
627+
628+
629+
Using **cURL** (commandline)::
630+
631+
# Don't forget to replace 'YOUR-DSMR-URL' and 'YOUR-API-KEY' with your own values.
632+
# Please note that the plus symbol "+" has been replaced by "%2B" here, to make it work for cURL.
633+
curl --request PATCH http://YOUR-DSMR-URL/api/v2/datalogger/meter-statistics \
634+
-d 'timestamp=2020-01-15T12:34:56%2B01&dsmr_version=50&power_failure_count=1&voltage_sag_count_l1=5&voltage_swell_count_l1=6&latest_telegram=' \
635+
-H 'Authorization: Token YOUR-API-KEY' | python -m json.tool
636+
637+
638+
Using **requests** (Python)::
639+
640+
import requests
641+
import json
642+
643+
response = requests.patch(
644+
'http://YOUR-DSMR-URL/api/v2/datalogger/meter-statistics',
645+
headers={'Authorization': 'Token YOUR-API-KEY'},
646+
data={
647+
'timestamp': '2020-01-15T12:34:56+01'
648+
'dsmr_version': '50'
649+
'power_failure_count': 1
650+
'voltage_sag_count_l1': 5
651+
'voltage_swell_count_l1': 6
652+
'latest_telegram': None
653+
}
654+
)
655+
656+
if response.status_code != 200:
657+
print('Error: {}'.format(response.text))
658+
else:
659+
print('Updated: {}'.format(json.loads(response.text)))
660+
661+
662+
**Result**::
663+
664+
{
665+
"id": 1,
666+
"timestamp": "2020-01-15T12:34:56+01:00",
667+
"dsmr_version": "50",
668+
"electricity_tariff": 1,
669+
"power_failure_count": 1,
670+
"long_power_failure_count": 0,
671+
"voltage_sag_count_l1": 5,
672+
"voltage_sag_count_l2": 2,
673+
"voltage_sag_count_l3": 3,
674+
"voltage_swell_count_l1": 6,
675+
"voltage_swell_count_l2": 0,
676+
"voltage_swell_count_l3": 0,
677+
"rejected_telegrams": 99,
678+
"latest_telegram": null
679+
}
680+
681+
682+
----
683+
499684

500685
``GET`` - ``consumption/electricity``
501686
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -916,4 +1101,3 @@ Example
9161101
"days_since": 1
9171102
}
9181103
}
919-

docs/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ v3.1.0 - 2020-01-xx
77
- [`#836 <https://github.com/dennissiemensma/dsmr-reader/issues/836>`_] Correct background of inactive icons in Archive - by JeanMiK
88
- [`#828 <https://github.com/dennissiemensma/dsmr-reader/issues/828>`_] Status page displays disabled capabilities
99
- [`#833 <https://github.com/dennissiemensma/dsmr-reader/issues/833>`_] Mqtt verbindt niet opnieuw na herstart mosquitto
10+
- [`#820 <https://github.com/dennissiemensma/dsmr-reader/issues/820>`_] Meterstatistieken doorgeven via API
1011

1112

1213
----

docs/locale/nl/LC_MESSAGES/api.mo

963 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)