The bargraph is fully customable in length and colorzones by adjusting some values.
To work this script a textdevice has to be created and a source for the value to be displayed.
Right now in the current script i take the value from a temperature device to see how warm the water in my solar-storage tank is.
Code: Select all
-- Bargraph maker for text device -- RonkA
return {
on = {
timer = { 'every minute' },
},
logging = {
level = domoticz.LOG_ERROR,
marker = 'Bargraph',
},
execute = function(domoticz, triggeredItem)
local bar_length = 30 -- number of segments in bargraph; resize as you wish
local initial_value = 24 -- initial value to start bargraph at
local max_value = 75 -- max value of Bargraph
local bar_length_medium = 50 -- 1st color value yellow
local bar_length_hot = 60 -- 2nd color value red
local text_device = domoticz.devices('Bargraph') -- idx 335 --> the right idx HAS to be set in line 76 !!!
local value_device = domoticz.devices('Boiler gemiddelde temperatuur') -- this device produces the barvalue
local raw_value = value_device.temperature -- because value_device comes from a temperature device I use .temperature
local round_value = domoticz.utils.round(raw_value,0) -- rounding the raw_value to whole numbers
local one_segment = (max_value - initial_value) / bar_length -- calculate value of 1 segment
local green_zone = math.floor((bar_length_medium - initial_value) / one_segment) -- calculate the number of segments in the green zone
local yellow_zone = math.floor((bar_length_hot - initial_value) / one_segment) -- calculate the number of segments in the yellow zone
local red_zone = bar_length -- the remaining segments are for the red zone
local segment_count = math.floor((raw_value - initial_value) / one_segment) -- number of segments to display in bargraph
-- calculate the percentage value round to the nearest whole number
local percentage = (raw_value - initial_value) / (max_value - initial_value) * 100
if percentage < 0 then
percentage = 0
elseif percentage > 100 then
percentage = 100
end
local percentage_display = math.floor(percentage + 0.5)
-- building the separate bar segments
local green_bar = ''
local yellow_bar = ''
local red_bar = ''
if segment_count <= green_zone then
green_bar = '<span style="color: Green;">' .. string.rep('█', segment_count) .. '</span>' ..
'<span style="color: Wheat; background-color: Green;">' .. string.rep('▓', green_zone - segment_count) .. '</span>'
yellow_bar = '<span style="color: Wheat; background-color: yellow;">' .. string.rep('▓', yellow_zone - green_zone) .. '</span>'
red_bar = '<span style="color: Wheat; background-color: red;">' .. string.rep('▓', red_zone - yellow_zone) .. '</span>'
elseif segment_count > green_zone and segment_count <= yellow_zone then
green_bar = '<span style="color: Green;">' .. string.rep('█', green_zone) .. '</span>'
yellow_bar = '<span style="color: Yellow;">' .. string.rep('█', segment_count - green_zone) .. '</span>' ..
'<span style="color: Wheat; background-color: yellow;">' .. string.rep('▓', yellow_zone - segment_count) .. '</span>'
red_bar = '<span style="color: Wheat; background-color: red;">' .. string.rep('▓', red_zone - yellow_zone) .. '</span>'
elseif segment_count > yellow_zone then
green_bar = '<span style="color: Green;">' .. string.rep('█', green_zone) .. '</span>'
yellow_bar = '<span style="color: Yellow;">' .. string.rep('█', yellow_zone - green_zone) .. '</span>'
red_bar = '<span style="color: Red;">' .. string.rep('█', segment_count - yellow_zone) .. '</span>' ..
'<span style="color: Wheat; background-color: red;">' .. string.rep('▓', red_zone - segment_count) .. '</span>'
end
-- building the bar
local bar = '<span style="font-family: Consolas; font-weight: bold; font-size: 14px;">' ..
initial_value .. '°C' .. '▐' .. green_bar .. yellow_bar .. red_bar .. '▌' .. max_value .. '°C\n' ..
string.rep(' ', segment_count + 4) .. '▲\n' ..
string.rep(' ', segment_count) .. round_value .. '°C⁞'.. percentage_display .. '%' .. '</span>'
-- check if the current message is same as the new message
local current_message = text_device.text
if (current_message ~= bar) then
text_device.updateText(bar) -- Update the text device
----------> automatically clear the log of the textdevice; change the ip and portnumber accordingly to your setup..
domoticz.openURL('http://127.17.0.2:80/json.htm?type=command¶m=clearlightlog&idx=335')
else
return -- This will stop the script and no log entry will be made
end
end
}
Enjoy!!