Shelly Scripting

Mit ** gekennzeichnete Links auf dieser Seite sind Affiliatelinks.

Shelly Scripting
Shelly Scripting
  • Matthias Kleine
  • 26.09.2021
  • Grundlagen

Vor wenigen Stunden hat Allterco Robotics (das Unternehmen hinter der Marke Shelly) das neue Script-Feature freigegeben. In diesem Video schauen wir uns gemeinsam an, was man damit anstellen kann. Durch den sehr viel leistungsfähigeren ESP32 in den Pro- und Plus-Komponenten, bleiben freie Ressourcen für eigene Logik übrig. Diese kann mit mJs (einem Subset von JavaScript) umgesetzt werden.

Über dieses Feature kann man zum Beispiel die Geräte über HTTP miteinander vernetzen und so zum Beispiel mit einem Shelly Pro 4 PM einen “einfachen” Shelly 2.5 ansteuern. Das gleiche gilt natürlich auch für andere Shelly-Geräte. Mehr dazu im Video!

Video

Hausbau-Kurs

Code-Beispiele

let greeting = "Hello world!";
print(greeting);
let CONFIG = {
    accuWeatherAPIKEY: "...",
    weatherForecastEndpoint: "http://dataservice.accuweather.com/forecasts/v1/daily/1day/",
    weatherCurrentEndpoint: "http://dataservice.accuweather.com/currentconditions/v1/",
    location: 170358,
    coverControlTimer: 15 * 60 * 1000,
    coverControlDevice: "http://192.168.205.244/"
};

function getWeatherURLForLocation(location) {
    return CONFIG.weatherCurrentEndpoint +
        JSON.stringify(CONFIG.location) +
        "?apikey=" +
        CONFIG.accuWeatherAPIKEY +
        "&details=true";
}

function cover(position) {
    print("Cover will ", position);
    /*
    Shelly.call(
        "http.get",
        { url: CONFIG.coverControlDevice + "roller/0?go=" + position },
        function (response, error_code, error_message, ud) {
            print(JSON.stringify(response));
        },
        null
    );
    */
}

function CoverControlLocation() {
    Shelly.call(
        "http.get",
        { url: getWeatherURLForLocation() },
        function (response, error_code, error_message) {
            let weatherData = JSON.parse(response.body);
            if (weatherData[0].CloudCover > 60) {
                cover("open");
            }
            if (weatherData[0].CloudCover < 40) {
                cover("close");
            }
            print("Wolken: ", weatherData[0].CloudCover, "%");
        }
    );
}

Timer.set(
    CONFIG.coverControlTimer,
    true,
    function (ud) {
        CoverControlLocation();
    },
    null
);
Shelly.addEventHandler(
    function (event, ud) {
        print(JSON.stringify(event));
    },
    null
);
let switch0Timer = null;

function swOff(idx) {
    print('switching off ', idx);

    Shelly.call(
        'switch.set',
        { id: idx, on: false },
        function (result, error_code, error_message, ud) {
            print('switched off ', idx);
        }
    );
}

Shelly.addEventHandler(
    function (event, ud) {
        if (typeof event.component !== 'undefined' && typeof event.info.output !== 'undefined') {
            if (event.component === 'switch:0') {
                if (switch0Timer) {
                    print('clearing timeout');

                    Timer.clear(switch0Timer);
                }

                if (event.info.output) { // Just start if ouput is 'true'
                    print('starting timeout');

                    switch0Timer = Timer.set(5 * 1000, // Switch off after 5 Seconds
                        false,
                        swOff,
                        0
                    );
                }
            }
        }
    },
    null
);
Du willst mehr?

Smart-Home-Trainings von A-Z

Steig noch tiefer in die Themen ein und meistere Deine Projekte! Über 13.000 Teilnehmer konnten sich schon von der Qualität der Online-Kurse überzeugen.

ioBroker-Master-Kurs

ioBroker-Master-Kurs

Mehr Infos
Hausbau-Kurs

Hausbau mit KNX

Mehr Infos
Lox-Kurs

Lox-Kurs

Mehr Infos
Node-RED-Master-Kurs

Node-RED-Master-Kurs

Mehr Infos