# @name SolarWatt # @desc Aktuelle Einspeiseleistung eines Shelly Plug M Gen3, mit Icon je nach Leistungsstufe # @author De Douby # @version 1.3 # @config ip text "Shelly IP-Adresse" default="192.168.1.50" # @config every number "Aktualisierung" default=10 min=2 max=60 unit=s # @config thigh number "Schwelle hoch (W)" default=400 min=0 max=10000 # @config tlow number "Schwelle niedrig (W)" default=100 min=0 max=10000 # @config chigh color "Farbe hoch" default=#04FE04 # @config clow color "Farbe niedrig" default=#FF4E1A # @config ichigh text "Icon-ID hoch" default="54156" # @config icmid text "Icon-ID mittel" default="50557" # @config iclow text "Icon-ID niedrig" default="50546" class SolarWatt var url, period var watt, label, color, icon_id var ticks, in_flight def init() self.url = "http://" + store.get("ip") + "/rpc/Switch.GetStatus?id=0" self.period = store.get("every") self.watt = store.get("watt") self.label = self.watt == nil ? nil : self.fmt(self.watt) self.color = 0xFFFFFF self.icon_id = store.get("icmid") self.ticks = 0 self.in_flight = false end def fmt(w) if w >= 1000 return str(round(w / 1000.0, 2)) + "kW" end return str(int(w + 0.5)) + "W" end def on_body(body, status) self.in_flight = false if body == nil return end var m = re.search("\"apower\":([-0-9.]+)", body) if m == nil return end var w = num(m[1]) if w == nil return end if w < 0 w = -w end # Einspeisung liefert negativen Wert -> Betrag nehmen self.watt = w self.label = self.fmt(w) var thigh = store.get("thigh") var tlow = store.get("tlow") if w > thigh self.color = store.get("chigh") self.icon_id = store.get("ichigh") elif w > tlow self.color = 0xFFFFFF self.icon_id = store.get("icmid") else self.color = store.get("clow") self.icon_id = store.get("iclow") end store.set("watt", w) end def loop() if self.ticks <= 0 self.ticks = self.period if !self.in_flight self.in_flight = true http.get(self.url, / b, st -> self.on_body(b, st), {'find': "\"apower\":", 'keep': 24}) end end self.ticks -= 1 end def should_show() return self.watt == nil || self.watt > 0 # bei 0W überspringen, vor erstem Fetch trotzdem zeigen end def draw() clear() if !icon(self.icon_id, 0, 0) rect_fill(0, 0, 8, 8, 0x222222) # Platzhalter, falls Icon nicht installiert ist end if self.label == nil text(9, 6, "...", 0x666666) return end var w = width() - 9 text(9 + (w - text_ink_width(self.label)) / 2, 6, self.label, self.color) end def on_button(btn) if btn == "select" self.ticks = 0 end end end return SolarWatt()