Umschalten des Büro-Lichtrelais über ein Terminal auf meinem Ubuntu-Desktop
Von English mit KI übersetzt ·
Also, das ist halb eine Komfortsache und halb der Wunsch, die API kennenzulernen.Unser Analytics-Büro und die Dock-Beleuchtung daneben werden durch eine ESP32-Relaisplatine geschaltet, die über MQTT an Hermetry meldet. Es gibt ein Board mit einer Schaltkarte, und das funktioniert im Browser problemlos. Ich verbringe aber den ganzen Tag im Terminal unter Ubuntu und möchte ein Tastenkürzel, das die Büroleuchten umschaltet, ohne das Board zu öffnen.Was ich probiert habe: Ich habe das Zugriffstoken des Geräts aus der Credentials-Schublade genommen und an den HTTP-Endpunkt des Geräts gesendet:
curl -X POST "https://<device host>/api/v1/telemetry"
-H "Authorization: Bearer <ACCESS_TOKEN>"
-H "Content-Type: application/json"
-d '{"light": false}'
Es antwortet mit 200, und kurz zeigt die Schaltkarte Off an, aber das Licht bleibt an, und ein paar Sekunden später springt die Karte wieder auf On, sobald der ESP32 erneut meldet. Ich spreche also offensichtlich das falsche Ziel an. Welcher Endpunkt ist der richtige, damit das Gerät tatsächlich etwas tut?
ok so this is half a convenience thing and half me wanting to learn the API.
Our analytics office and the dock lights next to it are switched by an ESP32 relay board that reports to Hermetry over MQTT. There’s a board with a switch card on it and that works fine from the browser. I spend the whole day in a terminal on Ubuntu though and I want a keyboard shortcut that toggles the office lights without opening the board.
What I tried: I took the device’s access token from the Credentials drawer and posted to the device HTTP endpoint:
curl -X POST "https://<device host>/api/v1/telemetry"
-H "Authorization: Bearer <ACCESS_TOKEN>"
-H "Content-Type: application/json"
-d '{"light": false}'
It answers 200, and for a moment the switch card shows Off, but the lights stay on and a few seconds later the card flips back to On when the ESP32 reports again. So I’m clearly talking to the wrong thing. Which endpoint is the right one to make the device actually do something?
Von English mit KI übersetzt ·
Telemetriedaten mit dem Gerätetoken zu senden bedeutet, sich als das Gerät auszugeben. Hermetry speichert lediglich, was „das Gerät“ meldet. Sie benötigen einen Befehl in die entgegengesetzte Richtung.
Posting telemetry with the device token is you pretending to be the device. Hermetry just stores what „the device“ says. You want a command going the other way.
Von English mit KI übersetzt ·
Richtig, das erklärt, warum die Karte zurückspringt. Ist es also /api/v1/rpc? Die HTTP-Seite des Geräts listet genau diesen auf
right, that explains the card flipping back. so is it /api/v1/rpc? the device HTTP page lists that one
Von English mit KI übersetzt ·
Das ist für die Geräteseite, dort wird auf Befehle gewartet. Ich glaube, du brauchst statt des Gerätetokens einen API-Schlüssel. Ich habe vor einiger Zeit etwas Ähnliches mit python requests gemacht, aber ich erinnere mich nicht mehr an den Pfad.
That one is for the device side, it waits for commands. I think you need an API key instead of the device token, I did something like this with python requests a while ago but I don’t remember the path.
Von English mit KI übersetzt ·
Genau das machen wir für die Linienbeleuchtung in unserem Werk, über ein kleines Skript auf dem PC des Supervisors. Es ist die Commands API mit einem API-Schlüssel.Ihre Schaltkarte sendet bereits einen Befehl, und Geräte können nur Befehle senden, die im Commands-Katalog des Gerätoprofils auf der Basic-Stufe stehen. Die Methode existiert also. Um ihren genauen Namen und ihre Parameter zu sehen, fragen Sie die API mit demselben Schlüssel ab, den Sie gleich erstellen: GET /tenant/<tenantId>/api/iot/devices/<deviceId>/rpc-commands listet auf, was eine Steuerung an dieses Gerät senden darf.Erstellen Sie den Schlüssel unter API Keys: Geben Sie ihm die Berechtigung, Basisbefehle zu senden und Befehle zu lesen, tragen Sie die Adresse Ihres Desktops in die IP-Allowlist ein und wählen Sie ein Ablaufdatum. Dann:
#!/bin/sh
# ~/bin/office-lights (chmod 700)
KEY=$(cat ~/.config/hermetry/key)
curl -s -X POST "https://<your host>/tenant/<tenantId>/api/iot/devices/<deviceId>/rpc"
-H "Authorization: Bearer $KEY"
-H "Content-Type: application/json"
-d "{"method":"light.set","params":{"on":$1},"timeoutMs":10000}"
Ein zweiseitiger Befehl mit einem Timeout von 30 Sekunden oder weniger wartet und antwortet mit 200 und dem Status, sodass Sie im Terminal SUCCESSFUL oder den Fehler sehen. Binden Sie office-lights true und office-lights false an zwei Tastenkürzel, und Sie sind fertig. Ein API-Schlüssel kann nur Methoden senden, die im Katalog stehen, was ohnehin genau das ist, was Sie hier wollen.Dokumentation: Commands API.
We do exactly this for the line lights at our plant, from a little script on the supervisor’s PC. It’s the Commands API with an API key.
Your switch card already sends a command, and boards can only send commands that are in the device profile’s Commands catalogue at the Basic tier. So the method exists. To see its exact name and params, ask the API with the same key you’re about to create: GET /tenant/<tenantId>/api/iot/devices/<deviceId>/rpc-commands lists what a control may send to that device.
Create the key under API Keys: give it the permission to send basic commands and to read commands, put your desktop’s address in the IP allowlist, and pick an expiry. Then:
#!/bin/sh
# ~/bin/office-lights (chmod 700)
KEY=$(cat ~/.config/hermetry/key)
curl -s -X POST "https://<your host>/tenant/<tenantId>/api/iot/devices/<deviceId>/rpc"
-H "Authorization: Bearer $KEY"
-H "Content-Type: application/json"
-d "{"method":"light.set","params":{"on":$1},"timeoutMs":10000}"
A two-way command with a timeout of 30 seconds or less waits and answers 200 with the status, so you see SUCCESSFUL or the error in the terminal. Bind office-lights true and office-lights false to two shortcuts and you’re done. An API key can only send methods that are in the catalogue, which is what you want here anyway.
Docs: Commands API.
Von English mit KI übersetzt ·
Schlüssel laufen standardmäßig nach 90 Tagen ab (höchstens ein Jahr), also setz dir irgendwo eine Erinnerung, sonst stirbt dein Shortcut nach drei Monaten stillschweigend. Und halte die Schlüsseldatei bei chmod 600, nicht in deinem Dotfiles-Repo.
Keys expire after 90 days by default (a year at most), so put a reminder somewhere or your shortcut dies quietly in three months. And keep the key file chmod 600, not in your dotfiles repo.
Von English mit KI übersetzt ·
Funktioniert. Mein einziges Problem: Der Katalog enthält kein light.set, unser Integrator hat es relay.set genannt, mit einem Parameter state (0 oder 1). Der rpc-commands-Aufruf hat es sofort angezeigt. Die erste Belegung war Super+L, was natürlich den Bildschirm sperrt; jetzt liegt sie auf Super+F9.
Works. My only snag: the catalogue doesn’t have light.set, our integrator called it relay.set with a param called state (0 or 1). The rpc-commands call showed it straight away. First binding was Super+L which of course locks the screen, it’s on Super+F9 now.
Von English mit KI übersetzt ·
+1, werde mir das für das Garagentor zu Hause merken
+1, saving this for the garage door at home
Von English mit KI übersetzt ·
Schön. Wir haben bei unseren Dock-Lichtern eine Sicherheitsabfrage „Sind Sie sicher?“ hinzugefügt, nachdem jemand sie ausgeschaltet hatte, während ein Fahrer noch rückwärts einfuhr.
Nice. We added an „are you sure“ prompt to ours for the dock lights after someone switched them off with a driver still reversing in.
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

