diff --git a/switch.js b/switch.js index 79bcfe3..6395ff3 100644 --- a/switch.js +++ b/switch.js @@ -1,15 +1,3 @@ -function initialize() { - Shelly.call("Switch.GetStatus", - { id: CONFIG.switchId }, - function (res, error_code, error_msg, ud) { - print("Initializing... current switch state is:", res.output) - switchIsOpen = res.output; - runLoop(); - }, - null - ); -} - function runLoop() { let alertTimer = Timer.set( CONFIG.pollingInterval, @@ -19,7 +7,7 @@ function runLoop() { { method: "POST", url: CONFIG.pollingUrl, body: '{"801":{"170":null}}', timeout: 10 }, function (res, error_code, error_msg, ud) { print("errors-http-post:", error_code, error_msg); - if (error_code === 0) { // do this so that during timeout no crash + if (error_code === 0) { let parsedBody = JSON.parse(res.body); let production = parsedBody["801"]["170"]["101"]; let consumption = parsedBody["801"]["170"]["110"]; @@ -69,6 +57,7 @@ let CONFIG = { chargingConsumption: 2100, }; -let switchIsOpen; +let switchIsOpen = Shelly.getComponentStatus("switch:0").output; +print("Initialized switchIsOpen with:", switchIsOpen); -initialize(); +runLoop();