LUA Copilot, experiment..

Moderator: leecollings

Post Reply
Droll
Posts: 43
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

LUA Copilot, experiment..

Post by Droll »

Did a little experiment on coding LUA, got no real nolage on this lagnue, some in bacic, minor.

Started with some keywords in word (just as a notepad for myself), some pieces on code I knew was working.
The goal was to read a TXT file
Find the line that corresponded with today’s date, if the month (DD.MM.YYYY) was todays mouth, then search for today’s date.
If today’s date was found, display the second text.
Separate the text and use it as a variable, filter out the "//"
But each word should have a specific colour.
To make it more complex, it should also give you a notice the day before, in a different colour.
And of course show you 3 different icons, for 3 different states.

It's sort of a "GarbageCalendar (amateur) lua script"
Requires
  • A text file, made up in Excel, saved as Unicode TXT file, transferred to the LUA script folder.
06.01.2025 Matavfall/Papiravfall///
13.01.2025 //Plastavfall/Glass&Metal/
20.01.2025 Matavfall////Restavfall
03.02.2025 Matavfall/Papiravfall///
10.02.2025 //Plastavfall//
17.02.2025 Matavfall////Restavfall
03.03.2025 Matavfall//// Matavfall
10.03.2025 //Plastavfall//
xxxxx
  • A LUA script

Code: Select all

local file_path = "/xx/xx/domoticz/scripts/lua/Soir.txt"-- edit
local ttidx = otherdevices_idx['Soir']--edit

-- Get today's date
local today = os.date("*t")
local today_mm = string.format("%02d", today.month)
local today_dd = string.format("%02d", today.day)
local yesterday = os.date("*t", os.time() - 24*60*60)
local yesterday_mm = string.format("%02d", yesterday.month)
local yesterday_dd = string.format("%02d", yesterday.day)
local icon_waste = 136--edit
local icon_no_waste = 135--edit
local icon_waste_ready = 137--edit
local web = "http://192.168.xx.xxx:8080"--edit
local idx_display = 1099--edit
local display = "Soir"--edit

-- Function to map waste types to colors
local function getColor(wasteType)
    local colors = {
        ["Plastavfall"] = "purple",
        ["Glass&Metal"] = "orange",
        ["Matavfall"] = "green",
        ["Restavfall"] = "black",
        ["Papiravfall"] = "blue",
    }
    return colors[wasteType] or "black"
end

-- Function to make HTTP request
local function makeHttpRequest(customImage)
    os.execute('curl "' .. web .. '/json.htm?type=setused&used=true&name=' .. display .. '&description=&idx=' .. idx_display .. '&switchtype=0&customimage=' .. customImage .. '"')
end

-- Initialize commandArray
commandArray = {}

-- Read the file
local file = io.open(file_path, "r")
if file then
    local found_today = false
    local found_yesterday = false
    local output = ""

    for line in file:lines() do
        local date, waste = line:match("(%d%d.%d%d.%d%d%d%d)%s+(.-)%s")
        if date then
            local dd, mm = date:match("(%d%d).(%d%d)")

            if mm == today_mm and dd == today_dd then
                found_today = true
                local wasteTypes = {}
                for wasteType in waste:gmatch("(%w+&?%w*)") do
                    table.insert(wasteTypes, wasteType)
                end

                -- Print today's waste types with background colors
                output = output .. "<div style='color:red; margin: 0; padding: 0;'><ins>Waste today:</ins></div>"
                for _, wasteType in ipairs(wasteTypes) do
                    local color = getColor(wasteType)
                    output = output .. "<div style='background-color:" .. color .. "; margin: 0; padding: 0;'><ins>" .. wasteType .. "</ins></div>"
                end
                makeHttpRequest(icon_waste_ready)
            elseif mm == yesterday_mm and dd == yesterday_dd then
                found_yesterday = true
                local wasteTypes = {}
                for wasteType in waste:gmatch("(%w+&?%w*)") do
                    table.insert(wasteTypes, wasteType)
                end

                -- Print yesterday's waste types with normal colors
                output = output .. "<div style='color:orange; margin: 0; padding: 0;'><ins>Waste tomorrow:</ins></div>"
                for _, wasteType in ipairs(wasteTypes) do
                    local color = getColor(wasteType)
                    output = output .. "<div style='color:" .. color .. "; margin: 0; padding: 0;'><ins>" .. wasteType .. "</ins></div>"
                 end
                makeHttpRequest(icon_waste)
            end
        else
            print("Warning: Line format is incorrect: " .. line)
        end
    end

    if not found today and not found yesterday then
        output = "<div style='color:green; margin: 0; padding: 0;'><ins>No waste</ins></div>"
        makeHttpRequest(icon_no_waste)
    end

    commandArray['UpdateDevice'] = ttidx .. "|0|" .. output
    file:close()
else
    print("Error: Could not open file " .. file_path)
end

return commandArray
  • And 3 icon files, just made up in a hurry
domoticz_custom_icon_Søppel_tom.zip
(4.41 KiB) Downloaded 25 times
domoticz_custom_icon_soir_soppel.zip
(5.28 KiB) Downloaded 28 times
domoticz_custom_icon_soir_no_soppel.zip
(12.09 KiB) Downloaded 28 times
Image
No waste
Image
The day before
Image
Today

I know there’s a lot to do with the design, the goal was just to see what's possible to do with simple tools.

The code is not perfect.
Might be better options in DeVent, but I got better working with LUA, maybe more articles online?(for Copilot to read)
And for the TXT file, it takes 5 >10 minuttes to edit once a year..

Arne Kjetil
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest