shelly-tools / shelly-script-examples

Shelly mJS Scripting examples
GNU Affero General Public License v3.0
73 stars 21 forks source link

Shelly Group Switch Script Loop #20

Open Kai9555 opened 7 months ago

Kai9555 commented 7 months ago

Hallo zusammen, nachdem ich alle Daten der Hue Bridge in das Script eingetragen habe, kann ich die Hue Gruppe steuern (An und Aus). Jedoch ist aus irgend einem Grund, den ich bis jetzt noch nicht finden konnte, das Script im Loop. Die Gruppe schalten sich etwa alle 60s an und und dann nach etwa 60s wieder aus.

Das Log im Shelly aus 2min Aufzeichnung sieht so aus:

08:44:00 shelly_notification:163 Status change of switch:3: {"id":3,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000}} 08:45:00 shelly_notification:163 Status change of switch:2: {"id":2,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000}} 08:45:00 shelly_ejs_rpc.cpp:41 Shelly.call http.request {"method":"GET","url":"http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1"} 08:45:00 shelly_notification:163 Status change of switch:1: {"id":1,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000}} 08:45:00 shelly_notification:163 Status change of switch:0: {"id":0,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":43.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113900,"total":0.000}} 08:45:00 shos_rpc_inst.c:230 http.request via loopback 08:45:00 shelly_http_client.:306 0x3ffe6aa8: HTTP GET http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1 08:45:00 shelly_http_client.:606 0x3ffe6aa8: Finished; bytes 956, code 200, redir 0/3, auth 0, status OK 08:45:00 shelly_ejs_rpc.cpp:41 Shelly.call http.request {"method":"PUT","url":"http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1/action","body":"{\"on\": false}"} 08:45:00 shos_rpc_inst.c:230 http.request via loopback 08:45:00 shelly_http_client.:306 0x3ffe372c: HTTP PUT http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1/action 08:45:00 shelly_http_client.:606 0x3ffe372c: Finished; bytes 729, code 200, redir 0/3, auth 0, status OK 08:45:00 shelly_notification:163 Status change of switch:2: {"id":2,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000}} 08:46:00 shelly_ejs_rpc.cpp:41 Shelly.call http.request {"method":"GET","url":"http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1"} 08:46:00 shelly_notification:163 Status change of switch:3: {"id":3,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000}} 08:46:00 shelly_notification:163 Status change of switch:0: {"id":0,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":43.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000}} 08:46:00 shelly_notification:163 Status change of switch:1: {"id":1,"aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000},"ret_aenergy":{"by_minute":[0.000,0.000,0.000],"minute_ts":1714113960,"total":0.000}} 08:46:00 shos_rpc_inst.c:230 http.request via loopback 08:46:00 shelly_http_client.:306 0x3ffe2e78: HTTP GET http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1 08:46:00 shelly_http_client.:606 0x3ffe2e78: Finished; bytes 959, code 200, redir 0/3, auth 0, status OK 08:46:00 shelly_ejs_rpc.cpp:41 Shelly.call http.request {"method":"PUT","url":"http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1/action","body":"{\"on\": true}"} 08:46:00 shos_rpc_inst.c:230 http.request via loopback 08:46:00 shelly_http_client.:306 0x3ffe6d7c: HTTP PUT http://192.168.2.37/api/muAoGOH73kTnl6bcDc4tiGbqPLTPwdjcsD7DBozY/groups/1/action 08:46:00 shelly_http_client.:606 0x3ffe6d7c: Finished; bytes 728, code 200, redir 0/3, auth 0, status OK

Hat jemand eine Idee dazu?

Kai9555 commented 6 months ago

Wenns jemanden interessiert, ich habe es selber gelöst. Hier mein Script:

// CONFIG START
let CONFIG = {
    ip: '192.168.2.XX', //Hue Bridge IP
    user: 'muAoGOH73kTnl6bcxxxxxxxxxxxxxxx', //Hue Bridge API user
    group: '6', // Hue Group ID
    input1: 3, // Shelly switch ID
};
// CONFIG END

Shelly.addEventHandler(
    function (event, user_data) {

        if (typeof event.info.state !== 'undefined') {
          if (event.info.id === CONFIG.input1) {
                // Get the current light state
                Shelly.call(
                    "http.request", {
                    method: "GET",
                    url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/groups/' + CONFIG.group,
                },
                    function (res, error_code, error_message, ud) {
                        let st = JSON.parse(res.body);
                        if (st.state.all_on === true) {
                            Toggle("false");
                        } else {
                            Toggle("true");

                        }
                    },
                    null
                );
            } else {
                return true;
            }
        } else {
            return true;
        }
    },
);

function Toggle(state) {
    let b = '{"on": ' + state + '}';

    Shelly.call(
        "http.request", {
        method: "PUT",
        url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/groups/' + CONFIG.group + '/action',
        body: b
    },
        function (r, e, m) {
        },
        null
    );
}