Ein Skript, das jede BLU Button, Blu Motion oder Blu Door/Window Aktion in Shelly Events umwandelt.
Um die erzeugten BLU-Events zu nutzen, reicht es einfach, die Daten über einen Event-Handler Callback im Shelly-Script aufzurufen. Dies ermöglicht die Integration von Blu-Events in bestehende eventbasierte Skripts mit minimalem Aufwand. Im Anwendungsbeispiel, weiter unten, wird gezeigt, wie man mit wenig Code die BLU-Events nutzen kann, um eine spezifische URL bei einem bestimmten Blu Event aufzurufen.
Blu Events:
alle Blu Shellys: ---> "alive", "single_push", "hold_push", "pairing_push", "unknown_push"
Blu Door/Window: ---> "open", "closed"
Blu Motion: ---> "no_motion", "motion_detected"
Blu Button: ---> "wake_up", "double_push", "triple_push", "long_push"
Ein "unknown_state" Event ist ebenfalls möglich, wenn neue BTHome-IDs von Shelly Blu-Geräten übertragen werden und das Skript diese keinen Datenpunkt zuordnen kann. Bitte notiere die Hex-ID, die das Skript dir in dem Fall übermittelt, und teile sie mir mit, wenn du ein solches Event entdeckst. Ich werde dann versuchen die neuen IDs/Datenpunkte, so bald wie möglich, ins Skript zu integrieren.
Für eine Übersicht aller Event Daten hier klicken ---> [Event Preview] <--- oder manuell zum Beitrag #35 scrollen.
Das "Blu_Events Script" erzeugt "Blu Events", im Grunde lokale digitale Echtzeit-Benachrichtigungen auf dem Shelly. Um diese Events zu erstellen, belauscht das Script alle Bluetooth-Geräte in der Nähe, filtert die "Blu Shelly" Kommunikation heraus, entpackt die Daten in ein brauchbares Format und sendet sie dann als Event/Nachricht lokal auf dem Shelly, auf dem das Script ausgeführt wird.
Dadurch können dann auch andere Scripts auf dem Shelly mit diesen Nachrichten/Events arbeiten.
Ich hoffe, das Skript ist für einige von euch nützlich und falls ihr Fragen oder Anmerkungen habt, lasst es mich gerne wissen.
Update(08.12.2023): Upgrade Toolbox auf v2.6-Alpha.
Update(22.01.2024): v2.1 Hofix
Update(01.02.2024): v2.3.1 Hotfix
(Versionsupdate Links sind nur für registrierte Forum Benutzer zugänglich)
Status: getestet unter Shelly FW v1.2.0
(Aufgrund eines FW 1.2.0 Bugs werden einige print() Ausgaben nicht in der Shelly Script Console angezeigt) Mehr dazu hier
Shelly Script:
Blu_Events v2.4
//Config
let get_All= false; //Get all data available and ship it in a Event, default false, should only be used with eco mode false.
let battery_Fix= true; //Ignore, drop, 0% battery BTHome packages on Reboot Event, true/false
let debug= false; //Show debug msg in log, true/false, only use it for a short period of time, as it slows down your Shelly!
//========= Blu_Events v2.4 =========
let activeScan= true; //Active or Passiv Bluetooth Scan, only used when BT Gateway false!
//notUsed-->let _cid = "0ba9"; //Allterco, Company ID(MFD)
let devID1= "SBBT"; //Blu Button1, deviceID, --> SBBT-002C
let devID2= "SBDW"; //Blu Door/Window, deviceID --> SBDW-002C
let devID3= 'SBMO'; //Blu Motion, deviceID, --> SBMO-003Z
let uuid= "fcd2"; //BTHome, Service ID --> UUID(16bit)
let bluMap= {//Device Parameter, you can find the full BTH Device List at 'https://bthome.io/format'
//bthObjectID:[Property,Datatype,Factor/Unit],
'0xf0':['device_type_id','uint8','split2'], //All Blu Devices, special case [default --> uint16]
'0xf1':['firmware_version','uint8','split4'], //All Blu Devices, special case [default--> uint32]
'0xf2':['extra_data','uint8','split3'], //All Blu Devices, special case [default--> uint24] --> maybe FW version?
'0x00':['pid','uint8'], //All Blu Devices
'0x01':['battery','uint8','%'], //All Blu Devices
'0x3a':['button','uint8'], //All Blu Devices
'0x05':['illuminance','uint24',0.01], //Blu Motion & D/W
'0x45':['temperature','sint16',0.1], //Blu Motion & H&T
//notUsed-->'0x1a':['door','uint8'], //Blu D/W
//notUsed-->'0x20':['moisture','uint8'], //Unknown
'0x41':['distance','uint16',0.1], //BLU Sonic
'0x03':['humidity','uint16',0.01], //BLU H&T
'0x2d':['window','uint8'], //Blu D/W
'0x3f':['rotation','int16',0.1], //Blu D/W
'0x21':['motion','uint8'], //Blu Motion
};
function CreateEvent(obj){ //Create Blu data and send Blu Events
try{
//Somehow filter for device_type, with out local names
obj.gen= 'GBLE'; obj.device_type= 'Unknown-Type';
if(typeof obj.button === 'number' && !obj.illuminance || obj.device_type_id === '2.1') obj.device_type= 'Button';
if(typeof obj.illuminance === 'number' && !obj.motion || obj.device_type_id === '2.2') obj.device_type= 'Door-Window';
if(typeof obj.motion === 'number' || obj.device_type_id === '2.5') obj.device_type= 'Motion';
//notUsed-->obj.device_type || obj.device_type_id ? obj.gen= 'GBLE': obj.device_type= 'Unknown-Type';
//Create device_states
switch(obj.device_type){
case 'Button':
obj.device_state= obj.input;
break;
case 'Door-Window':
obj.window === 0 ? obj.device_state= obj.input||'closed': obj.device_state= obj.input||'open';
break;
case 'Motion':
obj.motion === 0 ? obj.device_state= obj.button_input||'no_motion': obj.device_state= obj.input||'motion_detected';
break;
default:
obj.device_state= 'unknown_state';
}
if(obj.input) delete obj.input;
if(obj.firmware_version){
obj.device_state= 'alive';
if(battery_Fix && obj.battery === 0) delete obj.battery; //Workaround a Blu FW_Update Battery Bug
if(battery_Fix && get_All && obj.battery_string) delete obj.battery_string; //Workaround a Blu FW_Update Battery Bug
}
if(debug) print('\nDebug: Blu_data:\n',obj);
Shelly.emitEvent(''+obj.device_state, obj); //Sending Event
if(debug) print('Debug: sending Event __[',obj.device_state,']__');
}catch(e){ErrorMsg(e,'SendEvent()',debug);}
}
function CheckInput(bI){ //Check for Blu Button Events
try{
if(typeof bI !== 'number') return null;
let buttonMap= ['wake_up', 'single_push', 'double_push', 'triple_push', 'long_push', 'pairing_push', 'default_reset_push'];
if(bI > 6 && bI !== 254) bI= 'unknown_push';
if(bI < 7) bI= buttonMap[bI];
if(bI === 254) bI= 'hold_push';
return bI;
}catch(e){ErrorMsg(e,'CheckInput()',debug);}
}
function DeviceName(name){ //Check for locale Device Name
try{
if(!name) return 'Hidden-Device';
if(Cut(name,devID1)) return 'Blu-Button1';
if(Cut(name,devID2)) return 'Blu-Door-Window';
if(Cut(name,devID3)) return 'Blu-Motion';
return 'Unknown-Device--> '+ name;
}catch(e){ErrorMsg(e,'DeviceName()',debug);}
}
function Unpack(d,m,r,l){ //Create BTHome obj and Unpack BTHome data
try{
//Setup declaring variabel and functions
if(typeof d !== "string" || d.length < 3) return null;
var obj= {mac: m, rssi: r, device_name: DeviceName(l)}, spC= 0, tmp= []; //Add extra data
function Int_To_uInt(int, bytes){
let mask= 1 << (bytes - 1);
if(int & mask) return int-(1 << bytes);
return int;
}
//Unpack Info BTHome Byte
byte= d.at(0); //Getting first Byte as dezimal
if(byte & 0x01) obj.encryption= true; //Getting encryption;
if(obj.encryption) throw new Error('BThome Service Data encripted, encription is not supported!');
if(get_All){
obj.bthome_version= byte >> 5; //Getting BTHome Version
(byte & 0x02) ? obj.interval= 'irregular': obj.interval= 'regular'; //Get transmission interval
if(obj.bthome_version !== 2) throw new Error('Wrong BThome Version: found v.'+obj.bthome_version+' only v.2 supported!');
}else{delete obj.encryption; if(obj.device_name === "Hidden-Device") delete obj.device_name;} //Reduce data
d= d.slice(1); //Delete useless Info byte
//Unpack BThome Values
for(let value of d){ //Search for matching BTHome hex ID
if(d.length < 1) break;
if(!spC) byte= btoh(d[0]); //Getting BTHome object ID
let bluData= bluMap['0x'+byte]; //Getting blu Data
if(bluData === undefined){ //Debug handling
print('Error: Unknown BThome Data--> HexID: 0x',byte,', you can add more id from the full objID list--> https://bthome.io/format');
obj.new_BTH_HexID= byte;
obj.new_Data= btoa(d);
obj.info= 'Please send the newBTH_HexID and newData to this script developer so that they can integrate it.';
break;
}
if(!spC) d= d.slice(1); //Delete usless bth ID byte
// Merge value bytes
let max= Number(Cut(bluData[1],'int','int'))/8; //Getting max Bytes out of dataType
if(d.length < max) throw new Error('Wrong DataType, '+d.length+' Bytes, payload to big for DataType: '+bluData[1]+' max->'+max);
if(max === 1) value= d.at(0);
else if(max === 2) value= (d.at(1) << 8) | d.at(0);
else if(max === 3) value= (d.at(2) << 16) | (d.at(1) << 8) | d.at(0);
//notUsed-->else if(max === 4) value= (d.at(3) << 24) | (d.at(2) << 16) | (d.at(1) << 8) | d.at(0);
d= d.slice(max); //Delete useless value Bytes
if(!Cut(bluData[1],'u','u')) value= Int_To_uInt(value,max*8); //Convert int to uint
if(value === undefined) break; //Exit value loop
//Adding String/Unit to value
let is_Str= typeof bluData[2] === 'string';
if (typeof bluData[2] === 'number') value= value*bluData[2]; //Adding factor
if(is_Str && !spC && Cut(bluData[2],'split')) spC= Number(Cut(bluData[2],'split','split'))+1; //Get split counter
if(spC){
tmp.push(value); //Saving value for special case
spC--; //reduce split counter
}else{
obj[bluData[0]]= value; //Saving value
}
if(is_Str && !spC && get_All) value= ''+value+bluData[2]; //Adding unit
if(is_Str && !spC && get_All) obj[bluData[0]+'_string']= value; //Saving String Value
if(spC === 1){
value= [];
for(let i in tmp){
value[(tmp.length-1)-i]= tmp[i]; //Reverse Array
}
tmp= []; //Clear useless data
obj[bluData[0]]= value.join('.'); //Join FW version into String
spC= 0; //Exit special case
}
}
return obj;
}catch(e){ErrorMsg(e,'Unpack()',debug);}
}
var old_pid= [-1,-1,-1,-1], old_mac= [-1,-1,-1,-1], iX= 0;
function ScanCB(e,r) { //BT Scan Loop
try{
if(e !== 2 || !r) return; //Exit if empty scan
if(!r.service_data || !r.service_data[uuid]) return; //Exit if not BTHome data
let obj= Unpack(r.service_data[uuid],r.addr,r.rssi,r.local_name); //Create BTHome Obj & Unpack BTHome Data
if(!obj) throw new Error('Failed to unpack service_data --> _[ '+btoa(r.service_data[uuid])+' ]_');
//Anti Double msg Logic
if((obj.pid === old_pid[0] && obj.mac === old_mac[0])||(obj.pid === old_pid[1] && obj.mac === old_mac[1])||
(obj.pid === old_pid[2] && obj.mac === old_mac[2])||(obj.pid === old_pid[3] && obj.mac === old_mac[3])
) return;//Exit if double msg
if(debug) print('Debug: Anti Double, saved Data:',iX,'\n',old_pid,'\n',old_mac);
if(iX >= old_pid.length) iX= 0;
old_pid[iX]= obj.pid; old_mac[iX]= obj.mac;
iX++ //increas counter;
if(!get_All && obj.pid) delete obj.pid; //reduce data
if(debug){ //Debug output
r.service_data[uuid]= btoa(r.service_data[uuid]);
r.advData= btoa(r.advData);
print('\nDebug: BT_data:\n',r,'\nDebug: BTHome_data:\n', obj);}
r= undefined, e= undefined; //Delete useless data
obj.input= CheckInput(obj.button);
if(!obj.input) delete obj.input; //Delete useless data
CreateEvent(obj); //Creating Event out of bthObj
}catch(e){ErrorMsg(e,'ScanCB()',debug);}
}
function Main(){ //Main syncron Script Code
BLE.Scanner.Start({duration_ms: -1, active: activeScan}, ScanCB); //Sub to passiv BT Scanner, start a new scan if no scanner is runnning
BLE.Scanner.isRunning() ? print('Status: BLE.Scanner is scanning in Background.'): print('Status: BLE.Scanner cannot be initiated, check your Bluetooth settings!');
}
//Toolbox v1.0(cut), a universal Toolbox for Shelly scripts
function Cut(f,k,o,i){ //Upgrade slice f=fullData, k=key-> where to cut, o=offset->offset behind key, i=invertCut
try{
let s= f.indexOf(k); if(s === -1) return null; if(o) s= s+o.length || s+o; if(i) return f.slice(0,s);
return f.slice(s);}catch(e){ErrorMsg(e,'Cut()');}}
function Setup(){ //Wating 2sek, to avoid a Shelly FW Bug
try{
if(Main && !tH9){tH9= Timer.set(2000,0,function(){print('\nStatus: started Script _[', scriptN,']_');
if(callLimit > 4){callLimit= 4;} try{Main();}catch(e){ErrorMsg(e,'Main()'); tH9= 0; Setup();}});}}catch(e){ErrorMsg(e,'Setup()');}}
function ErrorMsg(e,s,deBug){ //Toolbox formatted Error Msg
try{
let i=0; if(Cut(e.message, '-104: Timed out')) i= 'wrong URL or device may be offline';
if(Cut(e.message, 'calls in progress')) i= 'reduce _[ callLimit ]_ by 1 and try again, its a global variabel at the end of the toolbox';
if(s === 'Main()' || deBug) i= e.stack; if(Cut(e.message, '"Main" is not')) i= 'define a Main() function before using Setup()';
print('Error:',s || "",'---> ',e.type,e.message); if(i) print('Info: maybe -->',i);}catch(e){print('Error: ErrorMsg() --->',JSON.stringify(e));}}
var tH8= 0, tH9= 0, aC= 0, cCache= [], nCall= [], callLimit= 4, cacheLimit= 40, cSp= 0.1; //Toolbox global variable
var Status= Shelly.getComponentStatus, Config= Shelly.getComponentConfig; //Renamed native function
var info= Shelly.getDeviceInfo(), scriptID= Shelly.getCurrentScriptId(), scriptN= Config('script',scriptID).name; //Pseudo const, variabel
//Toolbox v1.0(cut), Shelly FW >1.0.8
Setup();
Alles anzeigen
Einfaches Event-Handler Beispiel: ---> Shelly.addEventHandler(function(d){print(d);}); ---Ausgabe---> "{ alle Event Daten }"
Das Anwendungsbeispiel wurde irgendwann bei einem Update abgeschnitten, vorerst verweise ich dafür auf das Easy_Call Script:
Shelly Script Easy_Call _[v1.2]_ (Blu Shellys lokal nutzen)
{ ohne Cloud oder Internet Anbindung }
Die Verknüpfung muss in der actionMap { eine Art Tabelle } unter Config eingetragen werden, es sind auch Callbacks möglich, worüber man mehrere Geräte auf einmal, mit einem Event schalten kann. (lokale Szenen)
Das Script gibt beim starten für 15 Minuten alle Blu Shellys in der Nähe im Script Log aus, so…