Extract Max and min temperature from a temperature device

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
acaonweb
Posts: 92
Joined: Thursday 23 March 2017 14:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Extract Max and min temperature from a temperature device

Post by acaonweb »

Hi, there is a way to extract max and min temperature in a day using dzevents in native way i mean using data are stored by domoticz?
Having a look to the day temperature graph, i can see the max and min are already calculated/extracted

So maybe is possible to extract them from database...

Thanx in advance 😄

Fabrizio
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract Max and min temperature from a temperature device

Post by waaren »

acaonweb wrote: Sunday 05 May 2019 10:03 Hi, there is a way to extract max and min temperature in a day using dzevents in native way i mean using data are stored by domoticz?
This could be a way to do it.

Code: Select all

local scriptVar = "minMaxTemp"

return {
    on      =   {   
                    timer = { "every 12 minutes" },
                    httpResponses = { scriptVar .. "*" }
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   scriptVar,
                },
                    
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        temperatureDevice = dz.devices(xxxx)          -- Replace with ID of temperatureDevice you want to track
        minMax = dz.devices(yyyy)                     -- Create as virtual text sensor
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function getGraphData(id)
            local  url = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=temp&range=month&idx=" .. id 
            dz.openURL({ url = url,
                         method = "GET",
                         callback = scriptVar .. "_" .. id})
        end
        
        local function getMinMax(rt)
            for idx, v in ipairs(rt) do 
                if rt[idx].d == dz.time.rawDate then
                    return dz.utils.round(rt[idx].tm,1) , dz.utils.round( rt[idx].te,1 )  -- min, max
                end
            end
	end    
        
        local function formatText(min,max)
            return (    os.date("%A ") .. dz.time.day .. " " .. os.date("%B ") .. " " .. dz.time.year .. "\n" .. 
                        "min. temp: " .. min .. " °C\n" ..
                        "max. temp: " .. max .. " °C") -- full-Dayname  day full-Monthname yyyy
        end
        
        if not item.isHTTPResponse then
            getGraphData(temperatureDevice.idx)
        elseif item.ok then                                      -- statusCode == 2xx
            minMax.updateText(formatText(getMinMax(item.json.result)))
        else
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
acaonweb
Posts: 92
Joined: Thursday 23 March 2017 14:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Extract Max and min temperature from a temperature device

Post by acaonweb »

Many Thanx,
i'll have a look deeply to the code ;)

Fabrizio
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest