Add Devices online count

This commit is contained in:
Theo Arends 2025-09-07 12:44:14 +02:00
parent 27532f8d81
commit 2f67682256

View File

@ -49,36 +49,27 @@ class mqttdata_cls
var state = json.load(data) var state = json.load(data)
if state # Valid JSON state message if state # Valid JSON state message
var sub_option = 1
var ipaddress = "" # Not used var ipaddress = "" # Not used
var uptime = state['Uptime'] # 129T10:52:41 var uptime = state['Uptime'] # 129T10:52:41
if state.find('Hostname') if state.find('Hostname')
sub_option = 2
topic = state['Hostname'] # wemos7 topic = state['Hostname'] # wemos7
ipaddress = state['IPAddress'] # 192.168.2.123 ipaddress = state['IPAddress'] # 192.168.2.123
end end
var last_seen = tasmota.rtc('local') 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 if self.list_buffer.size()
self.list_buffer.push(line) # Add state as last entry var list_index = 0
if self.list_buffer.size() > self.line_cnt # Max number of lines in buffer var list_size = size(self.list_buffer)
self.list_buffer.remove(0) # Remove first entry while list_index < list_size # Use while loop as counter is decremented
end if 0 == string.find(self.list_buffer[list_index], topic)
elif 2 == self.line_option self.list_buffer.remove(list_index) # Remove current state
if self.list_buffer.size() list_size -= 1 # Continue for duplicates
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
end end
list_index += 1
end 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
end end
@ -116,60 +107,61 @@ class mqttdata_cls
def web_sensor() def web_sensor()
if self.list_buffer.size() 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 if 2 == self.line_option
# Sort list
var less = /a,b -> a < b 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 end
var stx = false # If list_buffer is empty due to removes show nothing list_index = 0
var time_window = tasmota.rtc('local') - self.line_teleperiod if 1 == self.line_option
var i = 0 list_index = list_size - self.line_cnt # Offset in list using self.line_cnt
var j = size(self.list_buffer) if list_index < 0 list_index = 0 end
while i < j end
var splits = string.split(self.list_buffer[i], ",") var msg = format("</table>{t}") # Terminate two column table and open new table: <table style='width:100%'>
var last_seen = int(splits[3]) while list_index < list_size
var splits = string.split(self.list_buffer[list_index], ",")
if time_window > last_seen # Remove offline devices
self.list_buffer.remove(i)
j -= 1
continue
end
var topic = splits[0] # topic or hostname var topic = splits[0] # topic or hostname
var ipaddress = splits[1] var ipaddress = splits[1]
var uptime = splits[2] var uptime = splits[2]
var sub_option = int(splits[4]) var last_seen = int(splits[3])
if !stx
stx = true
msg = format("</table>{t}") # Terminate two column table and open new table
end
# msg += format("<tr style='font-size:%d%%'>", 90 - (self.line_duration * 10)) # msg += format("<tr style='font-size:%d%%'>", 90 - (self.line_duration * 10))
msg += "<tr style='font-size:80%'>" msg += "<tr style='font-size:80%'>"
if 1 == sub_option if ipaddress
msg += format("<td>%s</td><td>&nbsp</td><td align='right'>%s</td>", msg += format("<td><a target=_blank href='http://%s.'>%s</a></td><td><a target=_blank href='http://%s'>%s</a></td>",
topic, uptime) topic, topic, ipaddress, ipaddress)
elif 2 == sub_option else
msg += format("<td><a target=_blank href='http://%s.'>%s</a></td><td><a target=_blank href='http://%s'>%s</a></td><td align='right'>%s</td>", msg += format("<td>%s</td><td>&nbsp</td>", topic)
topic, topic, ipaddress, ipaddress, uptime)
end end
msg += format("<td align='right'>%s</td>", uptime)
if self.line_duration if self.line_duration
msg += format("<td style='font-size:90%%'>&#x1F557;%s</td>", # Clock msg += format("<td style='font-size:90%%'>&#x1F557;%s</td>", # Clock
self.dhm(last_seen)) self.dhm(last_seen))
end end
msg += "</tr>" msg += "</tr>"
i += 1 list_index += 1
end
if stx
msg += "</table>{t}" # Terminate three column table and open new table
tasmota.web_send(msg) # Do not use tasmota.web_send_decimal() which will replace IPAddress dots
tasmota.web_send_decimal("") # Force horizontal line
end end
msg += "</table>{t}" # Terminate three/four column table and open new table: <table style='width:100%'>
msg += format("{s}Devices online{m}%d{e}", list_size) # <tr><th>Devices online</th><td style='width:20px;white-space:nowrap'>%d</td></tr>
tasmota.web_send(msg) # Do not use tasmota.web_send_decimal() which will replace IPAddress dots
tasmota.web_send_decimal("") # Force horizontal line
end end
end end
end end
mqttdata = mqttdata_cls() mqttdata = mqttdata_cls()