Last active
March 25, 2025 14:59
-
-
Save mgerhardy/7c5555c24116175be224bc7a65ee7064 to your computer and use it in GitHub Desktop.
Fritz Box Dect Steckdose abfragen und in die Influx schieben
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # apt-get install python3-fritzconnection | |
| import logging | |
| import requests | |
| from fritzconnection import FritzConnection | |
| from fritzconnection.core.exceptions import FritzArrayIndexError | |
| logger = logging.getLogger("metrics") | |
| fc = FritzConnection(address="http://yourfritzbox", user="metrics", password="xxx") | |
| index = 0 | |
| url = f"http://influxdb:8086/write?db=vzlogger&precision=ms" | |
| def metric(name, value, deviceName=None): | |
| if deviceName: | |
| deviceName = deviceName.replace(" ", "_") | |
| else: | |
| deviceName = "unknown" | |
| data = f"power,device={deviceName} {name}={value}" | |
| logger.debug("Sending data: %s", data) | |
| headers = { | |
| "User-Agent": "fritz.py" | |
| } | |
| resp = requests.post(url, data=data, headers=headers) | |
| logger.debug("Response: %s", resp.text) | |
| if resp.status_code != 204: | |
| logger.error("Failed to send data: %s", resp.text) | |
| while True: | |
| try: | |
| info = fc.call_action("X_AVM-DE_Homeauto1", "GetGenericDeviceInfos", NewIndex=index) | |
| except FritzArrayIndexError: | |
| logger.debug("Got IndexError for index %d, stopping", index) | |
| break | |
| deviceName = info["NewDeviceName"] | |
| if (info["NewMultimeterIsEnabled"] == "ENABLED" and info["NewMultimeterIsValid"] == "VALID"): | |
| power = info["NewMultimeterPower"] / 100.0 #W | |
| metric("power", power, deviceName=deviceName) | |
| energy = info["NewMultimeterEnergy"] #Wh | |
| metric("energy", energy, deviceName=deviceName) | |
| index += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Unit] | |
| Description=Gather power metrics | |
| [Service] | |
| Type=oneshot | |
| ExecStart=fritz.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Unit] | |
| Description=Gather power metrics every 5 seconds | |
| [Timer] | |
| OnBootSec=5s | |
| OnUnitActiveSec=5s | |
| AccuracySec=1s | |
| Unit=fritz.service | |
| [Install] | |
| WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment