Page 2 of 3

Re: Multi-day weather forecast to control device

Posted: Thursday 06 August 2020 19:50
by waaren
spider85 wrote: Thursday 06 August 2020 19:36 is it possible to get the max temp for 24h (for the next day)?
If it is possible i could make a timing thing for my garden sprinklers.
Yes it's possible but what do you want?
The max temp for the next 24 hours (current hour - same hour tomorrow) or the max temp for the next day (00:00 - 23:59)?

Re: Multi-day weather forecast to control device

Posted: Thursday 06 August 2020 19:55
by spider85
waaren wrote: Thursday 06 August 2020 19:50
spider85 wrote: Thursday 06 August 2020 19:36 is it possible to get the max temp for 24h (for the next day)?
If it is possible i could make a timing thing for my garden sprinklers.
Yes it's possible but what do you want?
The max temp for the next 24 hours (current hour - same hour tomorrow) or the max temp for the next day (00:00 - 23:59)?
Maybe, if possible the max for "today 00:00 - 23:59" and the next day "00:00 - 23:59"
and rain for today and next?

Re: Multi-day weather forecast to control device

Posted: Thursday 06 August 2020 23:53
by waaren
spider85 wrote: Thursday 06 August 2020 19:55 the max for "today 00:00 - 23:59" and the next day "00:00 - 23:59"
and rain for today and next?
Added optional virtual devices for max. temperature today, max temperature tomorrow, mm rain today, mm rain tomorrow.

Code: Select all

--[[
        This script collects rain en temperature forecast data from openweathermap.org
        You need a free API key to access this data. Read howto get this free API key
        at https://openweathermap.org/guide#how

        Next you need to create a uservariable (type string) and enter the openweathermap API key as value
        Then create the devices you want

        Please note that dzVents requires acces to domoticz at a sufficient level to retrieve the location settings
        including the latititude and longitude. It can only get these if the Local Networks (nu username/password)
        include 127.0.0.1 (and ::1 for systems using IPv6)
        Explained in the wiki at:  https://www.domoticz.com/wiki/DzVents:_next_generation_LUA_scripting#Using_dzVents_with_Domoticz
        
        original script posted:  https://www.domoticz.com/forum/viewtopic.php?f=59&t=33424&start=20

        history
        20200720 Start coding - shared on forum
        20200722 Added average and minimum temperature devices
        20200806 Added optional virtual devices for max. temperature today, max temperature tomorrow, mm rain today, mm rain tomorrow.
]]--

local scriptVar = 'openWeatherMap'

return
{
    on = {
        timer =
        {
            'at *:17'
        },

        devices =
        {
            scriptVar .. 'Trigger', -- only used for testing
        },

        httpResponses =
        {
            scriptVar
        }
    },

    logging =
    {
            level = domoticz.LOG_DEBUG, -- switch to domoticz.ERROR when script works as designed
            marker = scriptVar,
    },

    execute = function(dz, item)

       -- ******** enter names of vars and devices below this line

        local openWeatherAPIKey = dz.variables('openWeatherAPIKey').value -- Type string var required

        local currentRain = dz.devices('Current rain')       -- optional virtual Custom Sensor
        local rain48Hours = dz.devices('Rain 48 hours')      -- optional virtual Custom Sensor
        local rain8Days = dz.devices('Rain 8 days')          -- optional virtual Custom Sensor
        local rainToday = dz.devices('Rain today')           -- optional virtual Custom Sensor
        local rainTomorrow = dz.devices('Rain tomorrow')     -- optional virtual Custom Sensor

        local weatherText = dz.devices('WeatherForecast')    -- optional text Sensor

        local currentTemperature = dz.devices('Current Temperature') -- Optional virtual Temperature sensor
        local maxTemperature48Hours = dz.devices('Max Temperature next 48 hours') -- Optional virtual Temperature sensor
        local maxTemperature8Days = dz.devices('Max Temperature next 8 days') -- Optional virtual Temperature sensor
        local minTemperature48Hours = dz.devices('Min Temperature next 48 hours') -- Optional virtual Temperature sensor
        local minTemperature8Days = dz.devices('Min Temperature next 8 days') -- Optional virtual Temperature sensor
        local maxTemperatureToday = dz.devices('Max Temperature today') -- Optional virtual Temperature sensor
        local maxTemperatureTomorrow = dz.devices('Max Temperature tomorrow') -- Optional virtual Temperature sensor
        local avgTemperature48Hours = dz.devices('Average Temperature next 48 hours') -- Optional virtual Temperature sensor
        local avgTemperature8Days = dz.devices('Average Temperature next 8 days') -- Optional virtual Temperature sensor

       -- ******** No changes required below this line

        local openWeatherURL = 'https://api.openweathermap.org/data/2.5/onecall?&units=metric'
        local DAY_SECONDS = 86400
        local endOfToday = os.time{year = dz.time.year, month = dz.time.month, day = dz.time.day, hour = 0} + DAY_SECONDS
        local endOfTomorrow = endOfToday + DAY_SECONDS

        local function getCurrentRain(t)
            if t == nil then return 0 end
            local totalmm = 0
                for key, mm in pairs(t) do
                    totalmm = totalmm + mm
                end
            return totalmm
        end

        local function getDayNightSeconds(t)
            t.daySeconds = t.sunset - t.sunrise
            t.nightSeconds= DAY_SECONDS - t.daySeconds
        end

        local function get8DaysTemperatures(t)
            local maxTemperature = -100
            local minTemperature = 100
            local avgTemperature, avgHelper = 0, 0

            for _, record in ipairs(t) do
                getDayNightSeconds(record)
                if record.temp.max > maxTemperature then maxTemperature = record.temp.max end
                if record.temp.min < minTemperature then minTemperature = record.temp.min end

                avgTemperature = ( record.temp.day * record.daySeconds + record.temp.night * record.nightSeconds ) / DAY_SECONDS
                avgHelper = avgHelper + avgTemperature
            end
            avgTemperature = avgHelper / #t
            return dz.utils.round(maxTemperature,1), dz.utils.round(minTemperature,1), dz.utils.round(avgTemperature,1)
        end

        local function getHoursTemperatures(t)
            local maxTemperature, maxTemperatureToday, maxTemperatureTomorrow = -100, -100, -100
            local minTemperature = 100
            local avgHelper = 0

            for _, record in ipairs(t) do
                if record.dt < endOfToday and record.temp > maxTemperatureToday then maxTemperatureToday = record.temp end
                if record.dt > endOfToday and record.dt < endOfTomorrow and record.temp > maxTemperatureTomorrow then maxTemperatureTomorrow = record.temp end

                if record.temp > maxTemperature then maxTemperature = record.temp end
                if record.temp < minTemperature then minTemperature = record.temp end
                avgHelper = avgHelper + record.temp
             end
            return dz.utils.round(maxTemperature,1),
                   dz.utils.round(minTemperature,1),
                   dz.utils.round((avgHelper / #t),1),
                   dz.utils.round(maxTemperatureToday,1),
                   dz.utils.round(maxTemperatureTomorrow,1)

        end

        local function getHoursRain(t)
            if t == nil then return 0 end
            local totalmm,todaymm, tomorrowmm = 0,0,0
            for key, record in ipairs(t) do
                if type(record) == 'table' and record.rain ~= nil then
                    for id, mm in pairs(record.rain) do
                        todaymm = todaymm + ( ( record.dt < endOfToday and mm ) or 0 )
                        tomorrowmm = tomorrowmm + ( ( record.dt > endOfToday and record.dt < endOfTomorrow and mm ) or 0 )
                        totalmm = totalmm + mm
                    end
                end
            end
            return totalmm, todaymm, tomorrowmm
        end

        local function get8DaysRain(t)
            if t == nil then return 0 end
            local totalmm = 0
            for key, record in ipairs(t) do
                if type(record) == 'table' and record.rain ~= nil then
                        totalmm = totalmm + record.rain
                end
            end
            return totalmm
        end

        local function createResultTable(rt)
            rt.currentRain = getCurrentRain(rt.current.rain)
            rt.rain48Hours, rt.todaymm, rt.tomorrowmm = getHoursRain(rt.hourly)
            rt.rain8Days = get8DaysRain(rt.daily)

            rt.maxTemperature48Hours, rt.minTemperature48Hours,
            rt.avgTemperature48Hours, rt.maxTemperatureToday, rt.maxTemperatureTomorrow  = getHoursTemperatures(rt.hourly)

            rt.maxTemperature8Days, rt.minTemperature8Days, rt.avgTemperature8Days = get8DaysTemperatures(rt.daily)

            rt.maxTemperature8Days = math.max(rt.maxTemperature48Hours, rt.maxTemperature8Days)
            rt.minTemperature8Days = math.min(rt.minTemperature48Hours, rt.minTemperature8Days)
        end

        local function updateTextDevice(t)
            local text =   'range:          rain - max Temp'  .. '\n'
            text = text .. 'current:   ' .. t.currentRain .. ' mm, ' .. t.current.temp .. ' °C' .. '\n'
            text = text .. '48 hours:  ' .. t.rain48Hours .. ' mm, ' .. t.maxTemperature48Hours .. ' °C' .. '\n'
            text = text .. '8 days:    ' .. t.rain8Days .. ' mm, ' .. t.maxTemperature8Days .. ' °C' .. '\n'
            weatherText.updateText(text)
        end

        local function deviceExists(dv)
            if dv == nil then
                return false
            else
                return dz.utils.deviceExists(dv.name)
            end
        end

        local function updateDevice(dv, value)
            if deviceExists(dv) then
                if dv.deviceType == 'Temp' then
                    dv.updateTemperature(value)
                else
                    dv.updateCustomSensor(value)
                end
            end
        end

        local function updateDevices(rt)
            updateDevice(currentRain, rt.currentRain)
            updateDevice(rain48Hours, rt.rain48Hours)
            updateDevice(rain8Days, rt.rain8Days)
            updateDevice(rainTomorrow, rt.tomorrowmm)

            updateDevice(currentTemperature, rt.current.temp)
            updateDevice(maxTemperature48Hours, rt.maxTemperature48Hours)
            updateDevice(maxTemperature8Days, rt.maxTemperature8Days)
            updateDevice(maxTemperatureTomorrow, rt.maxTemperatureTomorrow)
            updateDevice(minTemperature48Hours, rt.minTemperature48Hours)
            updateDevice(minTemperature8Days, rt.minTemperature8Days)
            updateDevice(avgTemperature48Hours, rt.avgTemperature48Hours)
            updateDevice(avgTemperature8Days, rt.avgTemperature8Days)

            if deviceExists(rainToday) and rainToday.lastUpdate.dDate < ( endOfToday - DAY_SECONDS ) then
                updateDevice(rainToday, rt.todaymm)
            elseif deviceExists(rainToday) then
                updateDevice(rainToday, math.max(rt.todaymm, tonumber(rainToday.sValue)))
            end

            if deviceExists(maxTemperatureToday) and maxTemperatureToday.lastUpdate.dDate < ( endOfToday - DAY_SECONDS ) then
                updateDevice(maxTemperatureToday, rt.maxTemperatureToday)
            elseif deviceExists(maxTemperatureToday) then
                updateDevice(maxTemperatureToday, math.max(rt.maxTemperatureToday, maxTemperatureToday.temperature))
            end

            if deviceExists(weatherText) then updateTextDevice(rt) end
        end

        -- main code
        if item.isHTTPResponse then

            if item.ok and item.isJSON then
                local rt = item.json
                createResultTable(rt)
                updateDevices(rt)
            else
                dz.log('There was a problem with the response from ' .. scriptVar, dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
            end
        else -- get the data from openweatherMap
            dz.openURL({
                url = openWeatherURL  ..
                        '&lat=' .. dz.settings.location.latitude ..
                        '&lon='  .. dz.settings.location.longitude ..
                        '&appid=' .. openWeatherAPIKey,

               callback = scriptVar, -- see httpResponses above.
            })
        end
    end
}


Re: Multi-day weather forecast to control device

Posted: Friday 07 August 2020 7:46
by spider85
waaren wrote: Thursday 06 August 2020 23:53
spider85 wrote: Thursday 06 August 2020 19:55 the max for "today 00:00 - 23:59" and the next day "00:00 - 23:59"
and rain for today and next?
Added optional virtual devices for max. temperature today, max temperature tomorrow, mm rain today, mm rain tomorrow.
GREAT!! Superb! your the best!

Thnx

Re: Multi-day weather forecast to control device

Posted: Friday 15 January 2021 10:14
by HyperTeHK
I tried doing it myself but ruined the max tomorrow sensor and reverted to you script.
But would it be possible to add minTemperatureToday and minTemperatureTomorrow or AverageTemperatureTomorrow.


I would like to raise the temperature for my heatpump for the night if It will be too cold for it to function without aid from my gasburner.

Is the maxtemperaturetoday for the entire day or for the remaining hours? If the script is run @18:00 willl it still count the cold morning?

Averagetemperaturetoday would also be usefull for increased running @t higher temperatures (better COP)

Re: Multi-day weather forecast to control device

Posted: Saturday 16 January 2021 8:12
by waaren
HyperTeHK wrote: Friday 15 January 2021 10:14 But would it be possible to add minTemperatureToday and minTemperatureTomorrow or AverageTemperatureTomorrow.
I added minTemperatureToday and minTemperatureTomorrow in below version
Is the maxtemperaturetoday for the entire day or for the remaining hours?
It is a rolling forecast meaning it will only look at the future.
Averagetemperaturetoday would also be useful for increased running @t higher temperatures (better COP)
Because the information is a prediction about the future you could just take ( min + max ) / 2 for this just after midnight.

After this update I will no longer add new functionality to the script because there is an excellent native hardware module available. I will only fix bugs.

Code: Select all

--[[
        This script collects rain en temperature forecast data from openweathermap.org
        You need a free API key to access this data. Read howto get this free API key
        at https://openweathermap.org/guide#how

        Next you need to create a uservariable (type string) and enter the openweathermap API key as value
        Then create the devices you want

        Please note that dzVents requires access to domoticz at a sufficient level to retrieve the location settings
        including the latitude and longitude. It can only get these if the Local Networks (not using username/password)
        include 127.0.0.1 (and ::1 for systems using IPv6)
        Explained in the wiki at:  https://www.domoticz.com/wiki/DzVents:_next_generation_LUA_scripting#Using_dzVents_with_Domoticz

        original script posted:  https://www.domoticz.com/forum/viewtopic.php?f=59&t=33424&start=20

        history
        20200720 Start coding - shared on forum
        20200722 Added average and minimum temperature devices
        20200806 Added optional virtual devices for max. temperature today, max temperature tomorrow, mm rain today, mm rain tomorrow.
        20201109 Defined devices in separate table so the will only be visited when required
        20200116 Add minimum temperature (rest of) today and tomorrow

]]--

local scriptVar = 'openWeatherMap'

return
{
    on = {
        timer =
        {
            'at *:17'
        },

        devices =
        {
            scriptVar .. 'Trigger', -- only used for testing
        },

        httpResponses =
        {
            scriptVar
        }
    },

    logging =
    {
            level = domoticz.LOG_DEBUG, -- switch to domoticz.ERROR when script works as designed
            marker = scriptVar,
    },

    execute = function(dz, item)


        -- ******** enter name of API var and devices below this line
        local openWeatherAPIKey = dz.variables('openWeatherAPIKey').value -- Type string var required

        if item.isHTTPResponse then
            -- ******** enter names of used devices between the quotes below this line
            dv = {}
            dv.currentRain = dz.devices('Current rain')       -- optional virtual Custom Sensor
            dv.rain48Hours = dz.devices('Rain 48 hours')      -- optional virtual Custom Sensor
            dv.rain8Days = dz.devices('Rain 8 days')          -- optional virtual Custom Sensor
            dv.rainToday = dz.devices('Rain today')           -- optional virtual Custom Sensor
            dv.rainTomorrow = dz.devices('Rain tomorrow')     -- optional virtual Custom Sensor

            dv.weatherText = dz.devices('WeatherForecast')    -- optional text Sensor

            dv.currentTemperature = dz.devices('Current Temperature') -- Optional virtual Temperature sensor
            dv.maxTemperature48Hours = dz.devices('Max Temperature next 48 hours') -- Optional virtual Temperature sensor
            dv.maxTemperature8Days = dz.devices('Max Temperature next 8 days') -- Optional virtual Temperature sensor
            dv.minTemperature48Hours = dz.devices('Min Temperature next 48 hours') -- Optional virtual Temperature sensor
            dv.minTemperature8Days = dz.devices('Min Temperature next 8 days') -- Optional virtual Temperature sensor
            dv.maxTemperatureToday = dz.devices('Max Temperature today') -- Optional virtual Temperature sensor
            dv.maxTemperatureTomorrow = dz.devices('Max Temperature tomorrow') -- Optional virtual Temperature sensor
            dv.avgTemperature48Hours = dz.devices('Average Temperature next 48 hours') -- Optional virtual Temperature sensor
            dv.avgTemperature8Days = dz.devices('Average Temperature next 8 days') -- Optional virtual Temperature sensor
            dv.minTemperatureToday = dz.devices('Min Temperature today') -- Optional virtual Temperature sensor
            dv.minTemperatureTomorrow = dz.devices('Min Temperature tomorrow') -- Optional virtual Temperature sensor
        end

       -- ******** No changes required below this line

        local openWeatherURL = 'https://api.openweathermap.org/data/2.5/onecall?&units=metric'
        local DAY_SECONDS = 86400
        local endOfToday = os.time{year = dz.time.year, month = dz.time.month, day = dz.time.day, hour = 0} + DAY_SECONDS
        local endOfTomorrow = endOfToday + DAY_SECONDS

        local function getCurrentRain(t)
            if t == nil then return 0 end
            local totalmm = 0
                for key, mm in pairs(t) do
                    totalmm = totalmm + mm
                end
            return totalmm
        end

        local function getDayNightSeconds(t)
            t.daySeconds = t.sunset - t.sunrise
            t.nightSeconds= DAY_SECONDS - t.daySeconds
        end

        local function get8DaysTemperatures(t)
            local maxTemperature = -100
            local minTemperature = 100
            local avgTemperature, avgHelper = 0, 0

            for _, record in ipairs(t) do
                getDayNightSeconds(record)
                if record.temp.max > maxTemperature then maxTemperature = record.temp.max end
                if record.temp.min < minTemperature then minTemperature = record.temp.min end

                avgTemperature = ( record.temp.day * record.daySeconds + record.temp.night * record.nightSeconds ) / DAY_SECONDS
                avgHelper = avgHelper + avgTemperature
            end
            avgTemperature = avgHelper / #t
            return dz.utils.round(maxTemperature,1), dz.utils.round(minTemperature,1), dz.utils.round(avgTemperature,1)
        end

        local function getHoursTemperatures(t)
            local maxTemperature, maxTemperatureToday, maxTemperatureTomorrow = -100, -100, -100
            local minTemperature, minTemperatureToday, minTemperatureTomorrow = 100, 100, 100
            local avgHelper = 0

            for _, record in ipairs(t.hourly) do
                if record.dt < endOfToday and record.temp > maxTemperatureToday then maxTemperatureToday = record.temp end
                if record.dt < endOfToday and record.temp < minTemperatureToday then minTemperatureToday = record.temp end
                if record.dt > endOfToday and record.dt < endOfTomorrow and record.temp > maxTemperatureTomorrow then maxTemperatureTomorrow = record.temp end
                if record.dt > endOfToday and record.dt < endOfTomorrow and record.temp < minTemperatureTomorrow then minTemperatureTomorrow = record.temp end
                if record.temp > maxTemperature then maxTemperature = record.temp end
                if record.temp < minTemperature then minTemperature = record.temp end
                avgHelper = avgHelper + record.temp
            end

            t.maxTemperature48Hours = dz.utils.round(maxTemperature,1)
            t.minTemperature48Hours = dz.utils.round(minTemperature,1)
            t.avgTemperature48Hours = dz.utils.round((avgHelper / #t.hourly),1)
            t.maxTemperatureToday = dz.utils.round(maxTemperatureToday,1)
            t.maxTemperatureTomorrow = dz.utils.round(maxTemperatureTomorrow,1)
            t.minTemperatureToday = dz.utils.round(minTemperatureToday,1)
            t.minTemperatureTomorrow = dz.utils.round(minTemperatureTomorrow,1)
            return t
        end

        local function getHoursRain(t)
            if t == nil then return 0 end
            local totalmm,todaymm, tomorrowmm = 0,0,0
            for key, record in ipairs(t) do
                if type(record) == 'table' and record.rain ~= nil then
                    for id, mm in pairs(record.rain) do
                        todaymm = todaymm + ( ( record.dt < endOfToday and mm ) or 0 )
                        tomorrowmm = tomorrowmm + ( ( record.dt > endOfToday and record.dt < endOfTomorrow and mm ) or 0 )
                        totalmm = totalmm + mm
                    end
                end
            end
            return totalmm, todaymm, tomorrowmm
        end

        local function get8DaysRain(t)
            if t == nil then return 0 end
            local totalmm = 0
            for key, record in ipairs(t) do
                if type(record) == 'table' and record.rain ~= nil then
                        totalmm = totalmm + record.rain
                end
            end
            return totalmm
        end

        local function createResultTable(rt)
            rt.currentRain = getCurrentRain(rt.current.rain)
            rt.rain48Hours, rt.todaymm, rt.tomorrowmm = getHoursRain(rt.hourly)
            rt.rain8Days = get8DaysRain(rt.daily)

            rt = getHoursTemperatures(rt)

            rt.maxTemperature8Days, rt.minTemperature8Days, rt.avgTemperature8Days = get8DaysTemperatures(rt.daily)

            rt.maxTemperature8Days = math.max(rt.maxTemperature48Hours, rt.maxTemperature8Days)
            rt.minTemperature8Days = math.min(rt.minTemperature48Hours, rt.minTemperature8Days)
        end

        local function updateTextDevice(t)
            local text =   'range:          rain - max Temp'  .. '\n'
            text = text .. 'current:   ' .. t.currentRain .. ' mm, ' .. t.current.temp .. ' °C' .. '\n'
            text = text .. '48 hours:  ' .. t.rain48Hours .. ' mm, ' .. t.maxTemperature48Hours .. ' °C' .. '\n'
            text = text .. '8 days:    ' .. t.rain8Days .. ' mm, ' .. t.maxTemperature8Days .. ' °C' .. '\n'
            dv.weatherText.updateText(text)
        end

        local function deviceExists(dv)
            if dv == nil then
                return false
            else
                return dz.utils.deviceExists(dv.name)
            end
        end

        local function updateDevice(dv, value)
            if deviceExists(dv) then
                if dv.deviceType == 'Temp' then
                    dv.updateTemperature(value)
                else
                    dv.updateCustomSensor(value)
                end
            end
        end

        local function updateDevices(rt)
            updateDevice(dv.currentRain, rt.currentRain)
            updateDevice(dv.rain48Hours, rt.rain48Hours)
            updateDevice(dv.rain8Days, rt.rain8Days)
            updateDevice(dv.rainTomorrow, rt.tomorrowmm)

            updateDevice(dv.currentTemperature, rt.current.temp)
            updateDevice(dv.maxTemperature48Hours, rt.maxTemperature48Hours)
            updateDevice(dv.maxTemperature8Days, rt.maxTemperature8Days)
            updateDevice(dv.maxTemperatureToday, rt.maxTemperatureToday)
            updateDevice(dv.minTemperatureToday, rt.minTemperatureToday)
            updateDevice(dv.maxTemperatureTomorrow, rt.maxTemperatureTomorrow)
            updateDevice(dv.minTemperatureTomorrow, rt.minTemperatureTomorrow)
            updateDevice(dv.minTemperature48Hours, rt.minTemperature48Hours)
            updateDevice(dv.minTemperature8Days, rt.minTemperature8Days)
            updateDevice(dv.avgTemperature48Hours, rt.avgTemperature48Hours)
            updateDevice(dv.avgTemperature8Days, rt.avgTemperature8Days)

            if deviceExists(dv.rainToday) and dv.rainToday.lastUpdate.dDate < ( endOfToday - DAY_SECONDS ) then
                updateDevice(dv.rainToday, rt.todaymm)
            elseif deviceExists(dv.rainToday) then
                updateDevice(dv.rainToday, math.max(rt.todaymm, tonumber(dv.rainToday.sValue)))
            end

            if deviceExists(dv.maxTemperatureToday) and dv.maxTemperatureToday.lastUpdate.dDate < ( endOfToday - DAY_SECONDS ) then
                updateDevice(dv.maxTemperatureToday, rt.maxTemperatureToday)
            elseif deviceExists(dv.maxTemperatureToday) then
                updateDevice(dv.maxTemperatureToday, math.max(rt.maxTemperatureToday, dv.maxTemperatureToday.temperature))
            end

            if deviceExists(dv.weatherText) then updateTextDevice(rt) end
        end

        -- main code
        if item.isHTTPResponse then

            if item.ok and item.isJSON then
                local rt = item.json
                createResultTable(rt)
                updateDevices(rt)

            else
                dz.log('There was a problem with the response from ' .. scriptVar, dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
            end
        else -- get the data from openweatherMap
            dz.openURL({
                url = openWeatherURL  ..
                        '&lat=' .. dz.settings.location.latitude ..
                        '&lon='  .. dz.settings.location.longitude ..
                        '&appid=' .. openWeatherAPIKey,

               callback = scriptVar, -- see httpResponses above.
            })
        end
    end
}


Re: Multi-day weather forecast to control device

Posted: Monday 18 January 2021 11:07
by Jan Jansen
waaren wrote: Saturday 16 January 2021 8:12
After this update I will no longer add new functionality to the script because there is an excellent native hardware module available. I will only fix bugs.
@ Waaren,

Thanks for the addition! However, there is a problem with the average temperature over the next 48 hours, This sensor continues to display the value infinity (no errors displayed in the log).

Regards,
Jan

Re: Multi-day weather forecast to control device

Posted: Monday 18 January 2021 12:22
by waaren
Jan Jansen wrote: Monday 18 January 2021 11:07 there is a problem with the average temperature over the next 48 hours, This sensor continues to display the value infinity
Thx for reporting.

You can fix this by changing line 137 from

Code: Select all

            t.avgTemperature48Hours = dz.utils.round((avgHelper / #t),1)
to

Code: Select all

            t.avgTemperature48Hours = dz.utils.round((avgHelper / #t.hourly),1)

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 13:06
by boggiz
Hi all,
First thank's a lot for this good app.
But I have different result for temperature with OWM API standart and with OneCall
https://www.dropbox.com/s/6qk8ql27y1kih8r/OWM.png?dl=0
On the first line the temperature is 3 times lower than on the following lines.
I don't understand why.
Someone to help me ?

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 13:18
by kiddigital
In the current Beta of Domoticz there is a major upgrade of the Open Weather Map hardware module which uses the OneCall API and has forecast sensors per day and/or hour when enabled.

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 14:49
by boggiz
Unfortunately I am on a NAS: updates are made by Jadhal rarely in Beta.

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 14:58
by waaren
boggiz wrote: Monday 15 March 2021 13:06 But I have different result for temperature with OWM API standart and with OneCall

On the first line the temperature is 3 times lower than on the following lines.
What is OneCall ?
Did you double check your location in the domoticz settings?

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 17:37
by boggiz
Thank you for your response.

In documentation I see 2 methods to Call the OWM API:
api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
or
api.openweathermap.org/data/2.5/onecall?&units=metric

I suppose that devices in the first line (photo) were updated by "Weather" because it's the same parameter I wrote in OWM material (City name = Dourdan, FR)
But in Dz parameters location is my house (1,2km to Dourdan center)

I don't use the 8-day weather forecast devices. Could this be the cause of this temperature difference?

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 18:09
by waaren
boggiz wrote: Monday 15 March 2021 17:37 In documentation I see 2 methods to Call the OWM API:
api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
or
api.openweathermap.org/data/2.5/onecall?&units=metric
Are you using the dzVents script that I posted here or the OWM hardware module @kiddigital created?

Re: Multi-day weather forecast to control device

Posted: Monday 15 March 2021 18:11
by boggiz
I am using your script in DzVents

Re: Multi-day weather forecast to control device

Posted: Tuesday 16 March 2021 0:02
by waaren
boggiz wrote: Monday 15 March 2021 18:11 I am using your script in DzVents
Can you please post all the loglines produced by one executing cycle of the script and the result of the call you make to the API yourself?

Re: Multi-day weather forecast to control device

Posted: Tuesday 16 March 2021 11:19
by boggiz
Hello Wareen,
Thank you for taking care of my problem !

Image
Why I can't put a picture here I'm obliged to give a link ?
https://www.dropbox.com/s/7bwmmro4m775n ... s.png?dl=0

Note: I changed the Script name, and added 4 dz.log() lines in function createResultTable(rt) to see what happens.

JSON 3 minutes later
https://www.dropbox.com/s/pkpas4ut4w5jh ... .json?dl=0

Have a good day
boggiz

Re: Multi-day weather forecast to control device

Posted: Tuesday 16 March 2021 12:34
by waaren
boggiz wrote: Tuesday 16 March 2021 11:19 Why I can't put a picture here I'm obliged to give a link ?
You left out the important part of the log (the line with the actual URL used).

Please don't post text as pictures but just copy / paste the text between </> the code tags here.

Re: Multi-day weather forecast to control device

Posted: Tuesday 16 March 2021 12:45
by boggiz
It is better by code

Code: Select all

 2021-03-16 12:12:00.129 Status: dzVents: Info: ==> OWM_MétéoForecast: ------ Start internal script: DV_MeteoForecast:, trigger: "at *:12"
2021-03-16 12:12:00.130 Status: dzVents: Debug: ==> OWM_MétéoForecast: OpenURL: url = https://api.openweathermap.org/data/2.5/onecall?&units=metric&lat=0&lon=0&appid=365a47xxxxxxxxxxxxxx43864e
2021-03-16 12:12:00.130 Status: dzVents: Debug: ==> OWM_MétéoForecast: OpenURL: method = GET
2021-03-16 12:12:00.130 Status: dzVents: Debug: ==> OWM_MétéoForecast: OpenURL: post data = nil
2021-03-16 12:12:00.131 Status: dzVents: Debug: ==> OWM_MétéoForecast: OpenURL: headers = nil
2021-03-16 12:12:00.131 Status: dzVents: Debug: ==> OWM_MétéoForecast: OpenURL: callback = ==> OWM_MétéoForecast
2021-03-16 12:12:00.131 Status: dzVents: Info: ==> OWM_MétéoForecast: ------ Finished DV_MeteoForecast
2021-03-16 12:12:00.131 Status: EventSystem: Script event triggered: /usr/local/domoticz/dzVents/runtime/dzVents.lua
2021-03-16 12:12:00.571 Status: dzVents: Info: Handling httpResponse-events for: "==> OWM_MétéoForecast"
2021-03-16 12:12:00.571 Status: dzVents: Info: ==> OWM_MétéoForecast: ------ Start internal script: DV_MeteoForecast: HTTPResponse: "==> OWM_MétéoForecast"
2021-03-16 12:12:00.616 Status: dzVents: Debug: ==> OWM_MétéoForecast: Processing device-adapter for Current rain: Custom sensor device adapter 

Re: Multi-day weather forecast to control device

Posted: Tuesday 16 March 2021 12:47
by boggiz
I see the problem !
lat / long = 0