From 2f67682256f888d3317ffaeb95d04510a9341929 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 7 Sep 2025 12:44:14 +0200 Subject: [PATCH] Add Devices online count --- tasmota/berry/examples/statedata.be | 100 +++++++++++++--------------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/tasmota/berry/examples/statedata.be b/tasmota/berry/examples/statedata.be index bf51cd828..0fcce9851 100644 --- a/tasmota/berry/examples/statedata.be +++ b/tasmota/berry/examples/statedata.be @@ -49,36 +49,27 @@ class mqttdata_cls var state = json.load(data) if state # Valid JSON state message - var sub_option = 1 var ipaddress = "" # Not used var uptime = state['Uptime'] # 129T10:52:41 if state.find('Hostname') - sub_option = 2 topic = state['Hostname'] # wemos7 ipaddress = state['IPAddress'] # 192.168.2.123 end var last_seen = tasmota.rtc('local') - var line = format("%s,%s,%s,%d,%d", topic, ipaddress, uptime, last_seen, sub_option) + var line = format("%s,%s,%s,%d", topic, ipaddress, uptime, last_seen) - if 1 == self.line_option - self.list_buffer.push(line) # Add state as last entry - if self.list_buffer.size() > self.line_cnt # Max number of lines in buffer - self.list_buffer.remove(0) # Remove first entry - end - elif 2 == self.line_option - if self.list_buffer.size() - var i = 0 - var list_size = size(self.list_buffer) - while i < list_size # Use while loop as counter is decremented - if 0 == string.find(self.list_buffer[i], topic) - self.list_buffer.remove(i) # Remove current state - list_size -= 1 # Continue for duplicates - end - i += 1 + if self.list_buffer.size() + var list_index = 0 + var list_size = size(self.list_buffer) + while list_index < list_size # Use while loop as counter is decremented + if 0 == string.find(self.list_buffer[list_index], topic) + self.list_buffer.remove(list_index) # Remove current state + list_size -= 1 # Continue for duplicates end + list_index += 1 end - self.list_buffer.push(line) # Add state as last entry - end + end + self.list_buffer.push(line) # Add state as last entry end end @@ -116,60 +107,61 @@ class mqttdata_cls def web_sensor() if self.list_buffer.size() - var msg = "" + var time_window = tasmota.rtc('local') - self.line_teleperiod + var list_index = 0 + var list_size = size(self.list_buffer) + while list_index < list_size + var splits = string.split(self.list_buffer[list_index], ",") + var last_seen = int(splits[3]) + if time_window > last_seen # Remove offline devices + self.list_buffer.remove(list_index) + list_size -= 1 + end + list_index += 1 + end + if !list_size return end # If list became empty bail out if 2 == self.line_option - # Sort list var less = /a,b -> a < b - self.sort(self.list_buffer, less) + self.sort(self.list_buffer, less) # Sort list by topic and/or hostname end - var stx = false # If list_buffer is empty due to removes show nothing - var time_window = tasmota.rtc('local') - self.line_teleperiod - var i = 0 - var j = size(self.list_buffer) - while i < j - var splits = string.split(self.list_buffer[i], ",") - var last_seen = int(splits[3]) - - if time_window > last_seen # Remove offline devices - self.list_buffer.remove(i) - j -= 1 - continue - end - + list_index = 0 + if 1 == self.line_option + list_index = list_size - self.line_cnt # Offset in list using self.line_cnt + if list_index < 0 list_index = 0 end + end + var msg = format("{t}") # Terminate two column table and open new table:
| Devices online | %d |
|---|