really thanks for the support, now looking at an example I'm trying to understand how the scripts work. I tried to make a change to your script but the shelly doesn't react...
I only modified the btmMap by removing everything and putting only one command to control the shelly which would then be in localhost.
This is what I read in the console:
shelly_notification:208 Evento da input:0: {"component":"input:0", "id":0, "event":"btn_down", "ts":1698075533.11} 17:38:53
shelly_notification:208 Evento da input:0: {"component":"input:0", "id":0, "event":"long_push", "ts":1698075534.11} 17:38:54
shelly_ejs_rpc.cpp:41 Shelly.call HTTP.GET {"url":"http://192.168.1.253/relay/0?turn=toggle"} 17:38:56
shelly_notification:208 Evento da input:0: {"component":"input:0", "id":0, "event":"btn_up", "ts":1698075536.36} 17:38:56
shos_rpc_inst.c:230 HTTP.GET tramite loopback 17:38:56
shelly_http_client.:306 0x3ffe53d0: HTTP GET http://192.168.1.253/relay/0?turn=toggle 17:38:56
shelly_notification:163 Cambio di stato dell'interruttore:0: {"id":0,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1698075539,"total":0.000}} 17:39:00
shelly_http_client.:606 0x3ffe53d0: finito; byte 0, codice 0, redir 0/3, autenticazione 0, stato DEADLINE_EXCEEDED: timeout 17:39:12
Errore: Call(), -104 -104: Timeout
thank you for your time
//config
let cid = 0; //Local input id
let btnMap = { //parameter-> pushTime:-> push duration, multiPush:-> push count, shellyCall=Shelly.call()=[Method,parameter,CallBack,userData]
test1: {pushTime: 2, shellyCall: ['HTTP.GET',{url:"http://192.168.1.253/relay/0?turn=toggle"}] },
}; //You can name entries inside the btnMap whatever you want, name Restriction: without Space!, unique entries only!, maximum--> 40 entries!
let multiPush_delay = 0.6; //Duration betwinn short multi pushes in Seconds, when reached it will reset the push counter
let shortPush_Timeframe = 0.6; //Timeframe betwinn btn_down and btn_up in Seconds, when reached it will stop counting pushes
let debug = false; //Debug output
function CustomBtn(d){
let result = Efilter(d,{filterValue:['btn_down','btn_up']});
if(!result || d.component !== 'input:'+cid) return; //Exit if usless event
try{
if(result.btn_down){ oldTS_down = d.now; //Saving push start time
if(tH1){Timer.clear(tH1); tH1=0;} //Reset multiPush Timer
let diffT = d.now-oldTS_up; //Duration betwinn short push
if(diffT >= multiPush_delay) pushCounter = 0; //Reset push counter
}
if(result.btn_up){ oldTS_up = d.now; //Saving push stop time
let diffT = d.now-oldTS_down; //Push duration
if(!oldTS_down) return; //Exit if no oldTS_down
if(diffT < shortPush_Timeframe) pushCounter++; //Increase Push counter
if(diffT >= shortPush_Timeframe) pushCounter = 0; //Reset Push counter
if(debug) print('Debug: Btn pushed for ',diffT,' Seconds, push count: ',pushCounter||'not_a_short_Push');
try{Object.keys(btnMap).forEach(Read_btnMap);}catch(e){print('Error: Read_btnMap(),',e);} //Parse to Array
if(tList.length > 0){ //Push duration logic
try{tList = Sort(tList);}catch(e){print('Error: Sort(tList),',e);} //sorting pushTime, high --> low
for(let btn of tList){ //Compare real Push Duration with btnMap pushTime
if(diffT > btn[0]){
if(debug) print('Debug: found match pushTime, >',btn[0]);
if(debug) print('Debug: calling: ',btn[1]);
Call(btn[1][0],btn[1][1],btn[1][2],btn[1][3]);
break;
}
}
tList = []; //Clear Push duration list
}
if(mList.length > 0 && pushCounter){ //Push counter logic
try{mList = Sort(mList,-1);}catch(e){print('Error: Sort(mList),',e);} //sorting multiPush, high --> low
for(let btn of mList){ //Compare Push counter with btnMap multiPush
if(btn[0] && pushCounter === btn[0]){
if(!tH1) tH1 = Timer.set(1000*multiPush_delay,false,function(){tH1=0; Check_multiPush(btn);})
break;
}
}
mList = []; //Clear Push counter list
}
}
}catch(e){print('Error: CustomBtn(),',e);}
}
function Read_btnMap(btn){ //Read btnMap and parse to Array
let t=btnMap[btn].pushTime, m=btnMap[btn].multiPush, c=btnMap[btn].shellyCall;
if((!m && !t) || !c){print('Error: btnMap, missing Parameter: ',m || t,',',c); return;}
if(t && c) tList.push([t,c]);
if(m && c) mList.push([m,c]);
}
function Sort(arr, inv) { //Sorting array, high-->low or inv=-1 low-->high
let sA = [];
while (arr.length > 0) { //array loop
let maxI = 0; //start at index 0
for (let i = 1; i < arr.length; i++) { //search for max Value
if (inv && arr[i][0] < arr[maxI][0]) maxI = i;
if (!inv && arr[i][0] > arr[maxI][0]) maxI = i;
}
sA.push(arr.splice(maxI, 1)[0]); //add new max Value
}
return sA;
}
function Check_multiPush(btn){
if(debug) print('Debug: found match multiPush, =',btn[0]);
if(debug) print('Debug: calling: ',btn[1]);
Call(btn[1][0],btn[1][1],btn[1][2],btn[1][3]);
}
function Main(){ //Main Code
SwitchM(cid,'detached','button',debug); //Check,change Realy/input mode
Shelly.addEventHandler(CustomBtn); //Add EventHandler with Asyn CallBack CustomBtn()
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
function Str(d){ //Upgrade JSON.stringify
if(d === null || d === undefined)return null;
if(typeof d === 'string')return d;
return JSON.stringify(d);}
function Efilter(d, p) { //Event Filter, d=eventdata, p={debug:true, noInfo:true, inData:true, filterKey:[], filterValue:[]}->optional_parameter
try{
fR = {};
if (p.noInfo) {fR = d; d = {}; d.info = fR; fR = {};} if (p.inData && d.info.data) { d.info = d.info.data; delete d.info.data; }
if (p.filterKey) for (f of p.filterKey) for (k in d.info) if (f === k) fR[k] = d.info[k];
if (p.filterValue) for (f of p.filterValue) for (v of d.info) if (Str(v) && f === v) fR[Str(v)] = v;
if (p.debug) print('\nDebug: EventData-> ', d, '\n\nDebug: Result-> ', fR, '\n');
if (Str(fR) === '{}') {return null;} return fR;}catch(e){print('Error: Efilter(), ',e);}}
function SwitchM(cID,rM,iT,debug){ //Change rM->RelayMode, iT->InputMode, cID=0->Relay_ID and/or Input_ID,
try{ //More Info, rM='detached'->detached/flip/momentary/follow,iT='switch'->button/switch
let r = 0;
if(Config('switch',cID).initial_state === 'match_input') Call('Switch.SetConfig',{id:cID,config:{initial_state:'restore_last'}});
if(rM && Config('switch',cID).in_mode !== rM){Call('Switch.SetConfig',{id:cID,config:{in_mode: rM}});if(!r){r='';} r+='rM, ';}
if(iT && Config('input',cID).type !== iT){Call('Input.SetConfig',{id:cID,config:{type: iT}});if(!r){r='';} r+='iT, ';}
if(debug){r='Debug: tryed to change-> '+r+' Oldconfig: rM-> '+Config('switch',cID).in_mode+', iT-> '+Config('input',cID).type; print(r);}
return r;}catch(e){print('Error: SwitchM(), ',e);}}
function ErrorChk(r,e,m,d){ //Call() Error Check d=UserCallBack(r), or d={CB:UserCallBack(r), uD:userData}, or d='xy'->for_debug_output
try{
aC--; if(aC < 0) aC = 0;
if(e){print('Error: Call(), ',e,m); return;};
if(d.CB && d.uD) d.CB(r,d.uD); if(d.CB && !d.uD) d.CB(r);
if(!d.CB && d.uD) print('Debug: Call() finished->',d.uD);
if(Str(r.code) && r.code !== 200) throw new Error(Str(r));
}catch(e){print('Error: ErrorChk(), ',e);}}
function Call(m,p,CB,uD){ //Upgrade Shelly.call, m='Switch.Set'->call_Methode, p={id:0,on:true}->Call_Parameter,
try{ //More Info, d=UserCallBack(r), or d={CB:UserCallBack(r), uD:userData}, or d='xy'->for_debug_output
let d={};
if(typeof CB === 'function') d.CB = CB; if(Str(uD)) d.uD = uD;
if(aC < callLimit){aC++; Shelly.call(m, p, ErrorChk, d); return 0;}else{print('Error: Call(), to many activ Calls');}
}catch(e){print('Error: Call(), ',e); return 1;}}
function Setup(){ //Wating 2sek, to avoid a Shelly FW Bug
if(Main !== undefined && !tH9){tH9 = Timer.set(2000,false,function(){tH9 = null; print('Status: started Script _[', scriptN,']_');
if(callLimit > 5){callLimit=5;} try{Main();}catch(e){print('Error: Main(), ',e); Setup();};});}}
var tH9= null, callLimit = 5, aC=0, callList= []; //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.6-Alpha, Shelly FW >1.2!
let tH1=oldTS_down=oldTS_up=pushCounter= 0, mList=[] ,tList= []; //Global variabel
Setup();
Alles anzeigen