Nach weiteren Experimenten habe ich einen naheliegenden und auf einfache Weise funktionierenden Weg gefunden, BLU Daten unmittelbar nach deren Eintreffen zu verarbeiten. Das folgende Skript sendet diese Daten als MQTT Nachricht weiter. Es kann leicht für andere funktionale Aufgaben angepasst werden. Darin ist nach wie vor die Funktion bths() zuständig.
Zu diesem Skript sind weder anzulegende Aktionen noch ein Schedule Job erforderlich.
Meine Idee, die Einheit im Sensornamen unterzubringen nutze ich weiterhin. Stattdessen kann man die Object Id Tabelle in der BTHome Dokumentation im Skript einbauen und jeweils nach der importierten Komponente obj_id darin suchen lassen. Mir missfällt eine solche Sucherei, weshalb ich einen anderen Weg verwende. Für ein sofort einsetzbares Skript ohne zusätzliche bthomesensor Benamung - diese ist immerhin anwendungsorientiert - mag die Tabelle vorteilhaft sein.
Das Skript
Man entschuldige bitte die vielen auskommentierten print Ausgaben. Sie dienen während des Experimentierens der Kontrolle und können entfernt werden.
// created by eiche, 2025-05-24
// This script, running on a Shelly generation 3 or higher, sends MQTT messages with data from all BTHome sensor values of a BTHome device.
// This happens immediately after the script receives a status message from a BTHome device. Nothing further needs to be done.
// The function bths() requires a composition of sensor name and optional unit in the component name.
// Sensor name and unit must be separated by a separator containing the variable Sep, e.g. a colon.
// In function bths you may do something else with the sensor data, e.g. use the "HTTP.Get" method with an URL.
// But that method can be in an action and does not need a script.
// Have fun with it anyway!
// A small configuration
let
Topic = "versuch/bthome", // the MQTT topic
Sep = ':'; // the seperator symbol - it may be a string instead of one character
// end of configuration
// Get BTHome sensor data an send those via MQTT.
function bths(id) {
//print(id);
Shelly.call("bthomesensor.getstatus", {id:id}, function(res, errc, errm) {
if(errc) {console.log(errc, errm); return;}
//print(JSON.stringify(res));
let Sensor = {id:res.id, value:res.value, ts:res.last_updated_ts, name:"", unit:""};
Shelly.call("bthomesensor.getconfig", {id:id}, function(res, errc, errm, s) {
if(errc) {console.log(errc, errm); return;}
//print(JSON.stringify(res));
let n = res.name.split(Sep);
s.name = n[0];
if(n.length>1) s.unit = n[1];
let p = JSON.stringify(s);
//print(p);
MQTT.publish(Topic, p); // or do something else with object s respective Sensor
}, Sensor);
});
}
function bthStatus(st) {
if(st.name!=="bthomedevice") return;
//print(JSON.stringify(st));
Shelly.call("BTHomeDevice.GetKnownObjects", {id:st.id}, function(res, errc, errm) {
if(errc) {console.log(errc, errm); return;}
//print(JSON.stringify(res));
for(let i=res.objects.length-1; i>=0; --i) {
//print(res.objects[i].component);
if(res.objects[i].component!==null) bths(res.objects[i].component.split(':')[1]);
}
});
}
Shelly.addStatusHandler(bthStatus);
Alles anzeigen