# pyglowmarkt
## Introduction
Python API to the Bright/Glowmarkt/Hildebrand API for energy consumption.
There is a python API and a command-line script.
For instructions on use with Home Assistant, see
https://github.com/danmed/Glow2MQTT
## Install
```
pip3 install pyglowmarkt
```
## API example usage
### Connect
You need an account from https://glowmarkt.com/
```
from glowmarkt import *
cli = BrightClient("myusername@example.org", "MyP4ssword!")
```
### Discover virtual entities and resources
A virtual entity is e.g. your Glowmarkt device or SMETS2 smart meter.
A virtual entity has multiple resource e.g.
- Electricity consumption
- Electricity cost
- Gas consumption
- Gas cost
```
ents = cli.get_virtual_entities()
for ent in ents:
print("Entity:", ent.name)
for res in ent.get_resources():
print(" %s:" % res.name)
```
### Meter readings over a period of time
Assuming we've got a resource from the discovery above...
`get_readings` returns a list. Each element of the list is a
`[timestamp, value]` pair which will be a `KWH` or `Pence` object.
Use `value.value` to fetch the floating point value, or `str(value)` to
represent as a string with the kWh/pence unit.
```
# Get time now, and 4 hours ago, this is the reading window
now = datetime.datetime.now()
t_from = now - datetime.timedelta(hours=4)
t_to = now
# Results will be summarised at one hour readings
period = "PT1H"
# Round times to start of period boundary
t_from = resource.round(t_from, period)
t_to = resource.round(t_to, period)
rdgs = resource.get_readings(t_from, t_to, period)
for r in rdgs:
print(" %s: %s" % (
r[0].astimezone().replace(tzinfo=None),
r[1]
))
```
Timezones are managed according to t_from and t_to. If you want to use GMT
timezone, make sure t_from and t_to are set to use that timezone.
### Tariff
```
t = res.get_tariff()
print(" Tariff: rate=%.1f standing=%.1f" % (
t.current_rates.rate,
t.current_rates.standing_charge,
))
```
### Not implemented / tested
The API provides the means to get the current value of a resource (the last
data point acquired) and the meter reading (the cumulative value, the number
you would see if you go and look at the meter.
I can't get these to work, maybe not implemented, or maybe only work with
Glowmarkt hardware (I'm testing with a SMETS2 meter).
## Command line
### `glowmarkt-dump`
Accesses the bright account and dumps out each resource's readings, and
tariff information, human readable.
```
usage: glowmarkt-dump [-h] --username USERNAME --password PASSWORD
[--minutes MINUTES] [--period PERIOD]
optional arguments:
-h, --help show this help message and exit
--username USERNAME, -u USERNAME
Bright account username
--password PASSWORD, -p PASSWORD
Bright account password
--minutes MINUTES, -m MINUTES
Number of minutes to look back
--period PERIOD, -d PERIOD
Summary period (default: PT1H)
```
e.g.
```
$ glowmarkt-dump -u 'username@example.org' -p 'p4ssw0rd' -m 240 -d PT1H
Entity: DCC Sourced
electricity consumption:
2021-06-28 16:00:00: 0.000000 kWh
2021-06-28 17:00:00: 0.506000 kWh
2021-06-28 18:00:00: 2.355000 kWh
2021-06-28 19:00:00: 0.282000 kWh
2021-06-28 20:00:00: 0.000000 kWh
current: Not implemented.
meter reading: Not implemented.
Tariff: rate=16.3 standing=28.8
electricity cost:
2021-06-28 16:00:00: 0.000000 p
2021-06-28 17:00:00: 8.257920 p
2021-06-28 18:00:00: 38.433600 p
2021-06-28 19:00:00: 4.602240 p
2021-06-28 20:00:00: 0.000000 p
current: Not implemented.
meter reading: Not implemented.
Tariff: rate=16.3 standing=28.8
```
### `glowmarkt-csv`
Accesses the readings for all resources with a particular classifier
and writes out readings in CSV format. Would be used with e.g.
- `electricity.consumption`
- `electricity.consumption.cost`
- `gas.consumption`
- `gas.consumption.cost`
```
usage: glowmarkt-csv [-h] --username USERNAME --password PASSWORD
[--classifier CLASSIFIER] [--minutes MINUTES]
[--period PERIOD] [--no-header]
optional arguments:
-h, --help show this help message and exit
--username USERNAME, -u USERNAME
Bright account username
--password PASSWORD, -p PASSWORD
Bright account password
--classifier CLASSIFIER, -c CLASSIFIER
Resource classifier to use (default:
electricity.consumption)
--minutes MINUTES, -m MINUTES
Number of minutes to look back
--period PERIOD, -d PERIOD
Summary period (default: PT1H)
--no-header, -n Suppress CSV header
```
e.g.
```
$ scripts/glowmarkt-csv -u 'username@example.org' -p 'p4ssw0rd' \
-m 240 -d PT30M -c electricity.consumption.cost
entity,resource,time,value,unit
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T06:30:00,2.59488,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T07:00:00,1.82784,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T07:30:00,2.1216,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T08:00:00,2.31744,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T08:30:00,11.21184,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T09:00:00,3.1008,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T09:30:00,0,pence
DCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T10:00:00,0,pence
```
### `glowmarkt-today`
Shows cumulative consumption for today (since midnight local time).
Accesses the readings for all resources with a particular classifier
and writes out readings in CSV format. Would be used with e.g.
- `electricity.consumption`
- `electricity.consumption.cost`
- `gas.consumption`
- `gas.consumption.cost`
```
usage: glowmarkt-today [-h] --username USERNAME --password PASSWORD
[--classifier CLASSIFIER]
optional arguments:
-h, --help show this help message and exit
--username USERNAME, -u USERNAME
Bright account username
--password PASSWORD, -p PASSWORD
Bright account password
--classifier CLASSIFIER, -c CLASSIFIER
Resource classifier to use (default:
electricity.consumption)
```
e.g.
```
$ scripts/glowmarkt-today -u 'username@example.org' -p 'p4ssw0rd' \
-c electricity.consumption
3.998
```
### `glowmarkt-mqtt`
Gets cumulative consumption for today (since midnight local time) and pushes
the value to an MQTT topic.
Accesses the readings for all resources with a particular classifier
and writes out readings in CSV format. Would be used with e.g.
- `electricity.consumption`
- `electricity.consumption.cost`
- `gas.consumption`
- `gas.consumption.cost`
```
usage: glowmarkt-mqtt [-h] --username USERNAME --password PASSWORD
[--classifier CLASSIFIER]
[--mqtt-hostname MQTT_HOSTNAME]
[--mqtt-username MQTT_USERNAME]
[--mqtt-password MQTT_PASSWORD] [--topic TOPIC]
optional arguments:
-h, --help show this help message and exit
--username USERNAME, -u USERNAME
Bright account username
--password PASSWORD, -p PASSWORD
Bright account password
--classifier CLASSIFIER, -c CLASSIFIER
Resource classifier to use (default:
electricity.consumption)
--mqtt-hostname MQTT_HOSTNAME, --host MQTT_HOSTNAME
MQTT hostname (default: localhost)
--mqtt-username MQTT_USERNAME, -U MQTT_USERNAME
MQTT username
--mqtt-password MQTT_PASSWORD, -P MQTT_PASSWORD
MQTT password
--topic TOPIC, -t TOPIC
MQTT topic to publish to (default:
glowmarkt/consumption)
```
Simple test:
- Assume Mosquitto is running (or just run `mosquitto`).
- Run a subscriber: `mosquitto_sub -t glowmarkt/consumption`
- Publish a reading: `glowmarkt-mqtt -u USER -p PASSWORD`
Raw data
{
"_id": null,
"home_page": "https://github.com/cybermaggedon/pyglowmarkt",
"name": "pyglowmarkt",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Cybermaggedon",
"author_email": "mark@cyberapocalypse.co.uk",
"download_url": "https://files.pythonhosted.org/packages/f8/03/dea0943d5edd687e61b17ef31c4cf2735274092b7c9eb5e10ba073a9c38b/pyglowmarkt-0.5.6.tar.gz",
"platform": null,
"description": "\n# pyglowmarkt\n\n## Introduction\n\nPython API to the Bright/Glowmarkt/Hildebrand API for energy consumption.\nThere is a python API and a command-line script.\n\nFor instructions on use with Home Assistant, see\nhttps://github.com/danmed/Glow2MQTT\n\n## Install\n\n```\npip3 install pyglowmarkt\n```\n\n## API example usage\n\n### Connect\n\nYou need an account from https://glowmarkt.com/\n\n```\nfrom glowmarkt import *\n\ncli = BrightClient(\"myusername@example.org\", \"MyP4ssword!\")\n\n```\n\n### Discover virtual entities and resources\n\nA virtual entity is e.g. your Glowmarkt device or SMETS2 smart meter.\nA virtual entity has multiple resource e.g.\n- Electricity consumption\n- Electricity cost\n- Gas consumption\n- Gas cost\n\n```\nents = cli.get_virtual_entities()\n\nfor ent in ents:\n print(\"Entity:\", ent.name)\n for res in ent.get_resources():\n print(\" %s:\" % res.name)\n```\n\n### Meter readings over a period of time\n\nAssuming we've got a resource from the discovery above...\n\n`get_readings` returns a list. Each element of the list is a\n`[timestamp, value]` pair which will be a `KWH` or `Pence` object.\nUse `value.value` to fetch the floating point value, or `str(value)` to\nrepresent as a string with the kWh/pence unit.\n```\n\n# Get time now, and 4 hours ago, this is the reading window\nnow = datetime.datetime.now()\nt_from = now - datetime.timedelta(hours=4)\nt_to = now\n\n# Results will be summarised at one hour readings\nperiod = \"PT1H\"\n\n# Round times to start of period boundary\nt_from = resource.round(t_from, period)\nt_to = resource.round(t_to, period)\n\nrdgs = resource.get_readings(t_from, t_to, period)\nfor r in rdgs:\n print(\" %s: %s\" % (\n r[0].astimezone().replace(tzinfo=None),\n r[1]\n ))\n```\n\nTimezones are managed according to t_from and t_to. If you want to use GMT\ntimezone, make sure t_from and t_to are set to use that timezone.\n\n### Tariff\n\n```\nt = res.get_tariff()\nprint(\" Tariff: rate=%.1f standing=%.1f\" % (\n t.current_rates.rate,\n t.current_rates.standing_charge,\n))\n```\n\n### Not implemented / tested\n\nThe API provides the means to get the current value of a resource (the last\ndata point acquired) and the meter reading (the cumulative value, the number\nyou would see if you go and look at the meter.\n\nI can't get these to work, maybe not implemented, or maybe only work with\nGlowmarkt hardware (I'm testing with a SMETS2 meter).\n\n## Command line\n\n### `glowmarkt-dump`\n\nAccesses the bright account and dumps out each resource's readings, and\ntariff information, human readable.\n\n```\nusage: glowmarkt-dump [-h] --username USERNAME --password PASSWORD\n [--minutes MINUTES] [--period PERIOD]\n\noptional arguments:\n -h, --help show this help message and exit\n --username USERNAME, -u USERNAME\n Bright account username\n --password PASSWORD, -p PASSWORD\n Bright account password\n --minutes MINUTES, -m MINUTES\n Number of minutes to look back\n --period PERIOD, -d PERIOD\n Summary period (default: PT1H)\n```\n\ne.g.\n\n```\n$ glowmarkt-dump -u 'username@example.org' -p 'p4ssw0rd' -m 240 -d PT1H\nEntity: DCC Sourced\n electricity consumption:\n 2021-06-28 16:00:00: 0.000000 kWh\n 2021-06-28 17:00:00: 0.506000 kWh\n 2021-06-28 18:00:00: 2.355000 kWh\n 2021-06-28 19:00:00: 0.282000 kWh\n 2021-06-28 20:00:00: 0.000000 kWh\n current: Not implemented.\n meter reading: Not implemented.\n Tariff: rate=16.3 standing=28.8\n electricity cost:\n 2021-06-28 16:00:00: 0.000000 p\n 2021-06-28 17:00:00: 8.257920 p\n 2021-06-28 18:00:00: 38.433600 p\n 2021-06-28 19:00:00: 4.602240 p\n 2021-06-28 20:00:00: 0.000000 p\n current: Not implemented.\n meter reading: Not implemented.\n Tariff: rate=16.3 standing=28.8\n```\n\n### `glowmarkt-csv`\n\nAccesses the readings for all resources with a particular classifier\nand writes out readings in CSV format. Would be used with e.g.\n- `electricity.consumption`\n- `electricity.consumption.cost`\n- `gas.consumption`\n- `gas.consumption.cost`\n\n```\nusage: glowmarkt-csv [-h] --username USERNAME --password PASSWORD\n [--classifier CLASSIFIER] [--minutes MINUTES]\n [--period PERIOD] [--no-header]\n\noptional arguments:\n -h, --help show this help message and exit\n --username USERNAME, -u USERNAME\n Bright account username\n --password PASSWORD, -p PASSWORD\n Bright account password\n --classifier CLASSIFIER, -c CLASSIFIER\n Resource classifier to use (default:\n electricity.consumption)\n --minutes MINUTES, -m MINUTES\n Number of minutes to look back\n --period PERIOD, -d PERIOD\n Summary period (default: PT1H)\n --no-header, -n Suppress CSV header\n```\n\ne.g.\n\n```\n$ scripts/glowmarkt-csv -u 'username@example.org' -p 'p4ssw0rd' \\\n -m 240 -d PT30M -c electricity.consumption.cost\nentity,resource,time,value,unit\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T06:30:00,2.59488,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T07:00:00,1.82784,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T07:30:00,2.1216,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T08:00:00,2.31744,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T08:30:00,11.21184,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T09:00:00,3.1008,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T09:30:00,0,pence\nDCC Sourced,121d3e6d-ccea-4b46-8b37-798d5cd880b3,2021-06-29T10:00:00,0,pence\n```\n\n### `glowmarkt-today`\n\nShows cumulative consumption for today (since midnight local time).\n\nAccesses the readings for all resources with a particular classifier\nand writes out readings in CSV format. Would be used with e.g.\n- `electricity.consumption`\n- `electricity.consumption.cost`\n- `gas.consumption`\n- `gas.consumption.cost`\n\n```\nusage: glowmarkt-today [-h] --username USERNAME --password PASSWORD\n [--classifier CLASSIFIER]\n\noptional arguments:\n -h, --help show this help message and exit\n --username USERNAME, -u USERNAME\n Bright account username\n --password PASSWORD, -p PASSWORD\n Bright account password\n --classifier CLASSIFIER, -c CLASSIFIER\n Resource classifier to use (default:\n electricity.consumption)\n```\n\ne.g.\n\n```\n$ scripts/glowmarkt-today -u 'username@example.org' -p 'p4ssw0rd' \\\n -c electricity.consumption\n3.998\n```\n\n### `glowmarkt-mqtt`\n\nGets cumulative consumption for today (since midnight local time) and pushes\nthe value to an MQTT topic.\n\nAccesses the readings for all resources with a particular classifier\nand writes out readings in CSV format. Would be used with e.g.\n- `electricity.consumption`\n- `electricity.consumption.cost`\n- `gas.consumption`\n- `gas.consumption.cost`\n\n```\nusage: glowmarkt-mqtt [-h] --username USERNAME --password PASSWORD\n [--classifier CLASSIFIER]\n [--mqtt-hostname MQTT_HOSTNAME]\n [--mqtt-username MQTT_USERNAME]\n [--mqtt-password MQTT_PASSWORD] [--topic TOPIC]\n\noptional arguments:\n -h, --help show this help message and exit\n --username USERNAME, -u USERNAME\n Bright account username\n --password PASSWORD, -p PASSWORD\n Bright account password\n --classifier CLASSIFIER, -c CLASSIFIER\n Resource classifier to use (default:\n electricity.consumption)\n --mqtt-hostname MQTT_HOSTNAME, --host MQTT_HOSTNAME\n MQTT hostname (default: localhost)\n --mqtt-username MQTT_USERNAME, -U MQTT_USERNAME\n MQTT username\n --mqtt-password MQTT_PASSWORD, -P MQTT_PASSWORD\n MQTT password\n --topic TOPIC, -t TOPIC\n MQTT topic to publish to (default:\n glowmarkt/consumption)\n```\n\nSimple test:\n- Assume Mosquitto is running (or just run `mosquitto`).\n- Run a subscriber: `mosquitto_sub -t glowmarkt/consumption`\n- Publish a reading: `glowmarkt-mqtt -u USER -p PASSWORD`\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Python API for accessing Hildebrand/Glowmarkt/Bright API to smart meter data",
"version": "0.5.6",
"project_urls": {
"Download": "https://github.com/cybermaggedon/pyglowmarkt/archive/refs/tags/v0.5.6.tar.gz",
"Homepage": "https://github.com/cybermaggedon/pyglowmarkt"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f803dea0943d5edd687e61b17ef31c4cf2735274092b7c9eb5e10ba073a9c38b",
"md5": "2aec0a863a3e8dad198eb1ecc7d1380b",
"sha256": "c05be915f55e847571d565959a57347b8daa8bffd1d617c36c9e08857f1d03eb"
},
"downloads": -1,
"filename": "pyglowmarkt-0.5.6.tar.gz",
"has_sig": false,
"md5_digest": "2aec0a863a3e8dad198eb1ecc7d1380b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 12170,
"upload_time": "2023-05-13T09:09:05",
"upload_time_iso_8601": "2023-05-13T09:09:05.276918Z",
"url": "https://files.pythonhosted.org/packages/f8/03/dea0943d5edd687e61b17ef31c4cf2735274092b7c9eb5e10ba073a9c38b/pyglowmarkt-0.5.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-13 09:09:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cybermaggedon",
"github_project": "pyglowmarkt",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pyglowmarkt"
}