Page 2 of 2

Re: Cloud cover from darksky

Posted: Thursday 27 September 2018 2:00
by wergeld
....And I am an idiot.
Okay, the device name is not the var name used in the LUA script (ie dev_Cloud) it is the value of the var (ie Cloud Layer). Updated my devices to match the string value and not getting the errors now.

My only question then is how to get it to show on dashboard? I added it as a switch but that is just sort of dumb looking. The contents are:
Cloud Layer - percentage value
Forecast Tomorrow - string of text

Re: Cloud cover from darksky

Posted: Wednesday 08 May 2019 21:41
by Toulon7559
Interesting lua-script!

Seems also well suited for application to read the JSON-files from KNMI, but 'just' a small detail to be solved, because the JSON-output files are different.
A JSON-file as output from DarkSky looks like

Code: Select all

{
  "latitude": 52.29626465,
  "longitude": 6.8055501,
  "timezone": "Europe/Amsterdam",
  "currently": {
    "time": 1557341436,
    "summary": "Lichte regen",
    "icon": "rain",
    "precipIntensity": 0.4394,
    "precipProbability": 0.61,
    "precipType": "rain",
    "temperature": 12.28,
    "apparentTemperature": 12.28,
    "dewPoint": 9.92,
    "humidity": 0.86,
    "pressure": 995.11,
    "windSpeed": 5.02,
    "windGust": 10.22,
    "windBearing": 208,
    "cloudCover": 0.72,
    "uvIndex": 0,
    "visibility": 6.74,
    "ozone": 417.53
  },
The output from KNMI for my location looks like

Code: Select all

{
  "liveweer": [
    {
      "plaats": "Hasselo",
      "temp": "10.9",
      "gtemp": "8.4",
      "samenv": "Motregen en regen",
      "lv": "93",
      "windr": "ZO",
      "windms": "4",
      "winds": "3",
      "windk": "7.8",
      "windkmh": "14.4",
      "luchtd": " 996.4",
      "ldmmhg": "747",
      "dauwp": "9",
      "zicht": "7",
      "verw": "Perioden met regen, ook (onweers)buien mogelijk met (zware) windstoten",
      "sup": "05:46",
      "sunder": "21:11",
      "image": "regen",
      "d0weer": "bewolkt",
      "d0tmax": "18",
      "d0tmin": "8",
      "d0windk": "2",
      "d0windknp": "4",
      "d0windms": "2",
      "d0windkmh": "7",
      "d0windr": "Z",
      "d0neerslag": "21",
      "d0zon": "16",
      "d1weer": "halfbewolkt",
      "d1tmax": "15",
      "d1tmin": "6",
      "d1windk": "2",
      "d1windknp": "6",
      "d1windms": "3",
      "d1windkmh": "11",
      "d1windr": "ZW",
      "d1neerslag": "70",
      "d1zon": "40",
      "d2weer": "regen",
      "d2tmax": "14",
      "d2tmin": "8",
      "d2windk": "2",
      "d2windknp": "6",
      "d2windms": "3",
      "d2windkmh": "11",
      "d2windr": "W",
      "d2neerslag": "80",
      "d2zon": "20",
      "alarm": "0"
    }
  ]
}
With the script of aleph0 for read-out of DarkSky as basis, in analogy it means that generally not "currently": , but "liveweer": [ should be the key-value for json:decode()

Just json:decode(liveweer) returns a nil-error.
Have tried various other, slightly expanded layouts, but not successful.
What is the appropriate layout to put that key-info for KNMI in the brackets of json:decode()?

Re: Cloud cover from darksky

Posted: Wednesday 08 May 2019 21:54
by aleph0
Hi !

I didn't try, but it should be something like this :

Code: Select all

result=os.capture('curl -s "url_from_kmni"')
local jsonValeur = json:decode(result)

plaats=jsonValeur.liveweer.plaats
temp=jsonValeur.liveweer.temp
and so on... In the script "result" is just a variable name and it's not related to the "result" data block of the json answer. You can name it as you like

Or maybe

Code: Select all

plaats=jsonValeur.liveweer[1].plaats

Re: Cloud cover from darksky

Posted: Wednesday 08 May 2019 21:58
by Toulon7559
Thanks for the hints!

Your second hint is the working solution!
Data is now being uploaded to Domoticz Virtual Devices, although still 'getting complaints' from the Debugger related to the bottom lines of the script

Code: Select all

    if otherdevices_svalues[dev_Prev] ~= forecast then 
        UpdateDev(dev_Prev,0,forecast)
    end
The debugger is having problems with otherdevices_svalues.

ToDo = further selection of various useful KNMI-values, and cosmetic cleaning (incl. the above debugger-problem).
Related to KNMI-values the local 'round'-function in the DarkSky-script is not required.

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 11:01
by Toulon7559
Below is a derived 'variant-script' for extraction of KNMI-info:
certainly needs some more cosmetic cleaning & tuning to match your application.
Line-numbers inserted for easier reference.

Question1
Not knowing the background-operation of Domoticz, is the icon-translation acceptable&realistic, which starts at line 72?
To be seen in conjunction with Line 095 which uploads to a Virtual Device of type Barometer.
(Dutch) List of possible inputs is at the bottom of the script.
Question2
The upload-format is OK for 'Virtual_Device'-type = 'Barometer'.
Also tried 'Virtual_Device'-type = 'Custom Sensor'.
Under it's svalues see the transferred pressure-value and the icon-number, but no icon-text appearing in Domoticz' Widget.
How to realise icon-presentation in the 'Custom Sensor' widget according to uploaded icon-number?

Code: Select all

-- Script for data-extraction from KNMI, version 00 dated 20190508, (c)2019, Toulon7559
--
--[[ Script to get some values from KNMI API at liveweer.nl,  as described at http://weerlive.nl/delen.php
Source of Original script = http://www.domoticz.com/forum/viewtopic.php?f=61&t=22090&hilit=cloud+cover#p170296
Widgets for Prev and Actl are of type "text", other Widgets are of type '"Customer"  or specific to type.
]]--

-- Line 008 = Settings
-- Set the links to Virtual Devices in Domoticz by label-name
-- local dev_SunUp  ="Zons_Opgang"            -- Sunrise
-- local dev_SunSet ="Zons_Ondergang"         -- Sunset
local dev_Patm ="KNMI_Barometer"                -- Actual air pressure
local dev_Actl ="KNMI_Actueel"             -- Actual weather today
local dev_Prev ="KNMI_Verwachting"         -- Forecast rest of today
local dev_Tmin ="KNMI_Verwachting_Tmin"    -- Forecast Tmin
local dev_Tmax ="KNMI_Verwachting_Tmax"    -- Forecast Tmax

local api_key="abcdefg1234"                -- KNMI secret key <fake!>
local coord="52.29,6.80"                   -- decimal latitude, longitude 

local DEBUG = 1

-- Line 023 = Definition of Local Functions

function UpdateDev(device,nvalue,svalues)
    --Met à jour un device numérique Domoticz
    commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[device]..'|'..tostring(nvalue)..'|'..tostring(svalues)}
end

function os.capture(cmd, raw)
    local f = assert(io.popen(cmd, 'r'))
    local s = assert(f:read('*a'))
    f:close()
    if raw then return s end
    s = string.gsub(s, '^%s+', '')
    s = string.gsub(s, '%s+$', '')
    s = string.gsub(s, '[\n\r]+', ' ')
    return s
end

function round(num, dec)
    if num == 0 then
        return 0
    else
        local mult = 10^(dec or 0)
        return math.floor(num * mult + 0.5) / mult
    end
end

-- Line 050 = Start of Loop
commandArray = {}
time = os.date("*t")
if  ((time.min+2)%10)==0  then -- Run every 10 minutes, plus delay of 2 minutes
-- Line 054 = Querying KNMI
    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
    -- result=os.capture('curl -s "http://weerlive.nl/api/json-data-10min.php?key='..api_key..'&locatie='..coord..'"')
    result=os.capture('curl -s "http://weerlive.nl/api/json-data-10min.php?key=abcdef123&locatie=52.29,6.80"')
    -- if (DEBUG == 1) then print(result)  end
    --   
-- Line 060 = Reading values from json
    local jsonValeur = json:decode(result)
    val_Patm   =jsonValeur.liveweer[1].luchtd
    val_Tact   =jsonValeur.liveweer[1].temp
    val_Tgvl   =jsonValeur.liveweer[1].gtemp
    val_sup    =jsonValeur.liveweer[1].sup
    val_sunder =jsonValeur.liveweer[1].sunder
    val_Tmin   =jsonValeur.liveweer[1].d0tmin -- round(jsonValeur.liveweer.d0tmin,1) [not needed for liveweer.nl]
    val_Tmax   =jsonValeur.liveweer[1].d0tmin -- round(jsonValeur.liveweer.d0tmax,1) [not needed for liveweer.nl]
    icon       =jsonValeur.liveweer[1].image
    summary0   =jsonValeur.liveweer[1].samenv
    summary1   =jsonValeur.liveweer[1].verw   
-- Line 071 =  Converting KNMI icons/images into Domoticz forecast code
    if icon=="zonnig" or icon=="helderenacht"                                 then prev=1 --Sunny
    elseif icon=="regen" or icon=="buien" or icon=="sneeuw" or icon=="hagel"  then prev=6 --Cloudy/Rain
    elseif icon=="wind" or icon=="mist" or icon=="nachtmist"                  then prev=0 --Stable
    elseif icon=="bewolkt"                                                    then prev=2 --Cloudy
    elseif icon=="halfbewolkt" or icon=="wolkennacht"                         then prev=3 --Unstable
    elseif icon=="bliksem" 				                      then prev=4 --Very Unstable?
    else                                                                           prev=5 --Unknown
    end
    actual   =summary0.."<br>Tactueel : "..val_Tact.."°C Tgevoel : "..val_Tgvl.."°C"
    forecast =summary1.."<br>Min  : "..val_Tmin.."°C Max  : "..val_Tmax.."°C"
-- Line 082 =  Check Results 
    if (DEBUG == 1) then
        print("Tijd      : "..time.hour..":"..time.min)
        print("Luchtdruk : "..val_Patm)
        print("Tactueel   : "..val_Tact)
        print("Tgevoel   : "..val_Tgvl)
        print("Tmax      : "..val_Tmax)
        print("Tmin      : "..val_Tmin)
        print("Icon      : "..icon)
        print("Samenvatting : "..actual)
        print("Verwachting  : "..forecast)
    end
-- Line 094 = Upload to Domoticz   
-- Line 095 = Updating domoticz devices
   UpdateDev(dev_Patm,0,val_Patm..";"..prev)
--
--    UpdateDev(dev_Tmin,0,val_Tmin)
--    UpdateDev(dev_Tmax,0,val_Tmax)
-- Line 100 = Upload of text-info to Domoticz   
    if otherdevices_svalues[dev_Actl] ~= actual then 
        UpdateDev(dev_Actl,0,actual)
    end
    if otherdevices_svalues[dev_Prev] ~= forecast then 
        UpdateDev(dev_Prev,0,forecast)
    end
end

return commandArray

--[[ De mogelijke iconen voor weerlive.nl zijn:

zonnig
bliksem
regen
buien
hagel
mist
sneeuw
bewolkt
halfbewolkt
zwaarbewolkt
nachtmist
helderenacht
wolkennacht

Zijn naar analogie met het DarkSky-script verdeeld over de icon-vertaling
]]--]
Lines 056 and 057 are equivalent, to be applied at choice:
line 056 indirectly takes user-info from local definitions in lines 018 and 019,
line 057 directly has the required user-values in the line.

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 13:38
by aleph0
Thanks for sharing :-)

What's the message you got from the debugger (?) about the lines

Code: Select all

    if otherdevices_svalues[dev_Prev] ~= forecast then 
        UpdateDev(dev_Prev,0,forecast)
    end
?

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 17:07
by Toulon7559
@aleph0

The error report is

Code: Select all

lua: ...ticz/scripts/lua/script_time_KNMI_Extract00_RPI3.lua:27: attempt to index global 'otherdevices_idx' (a nil value)
stack traceback:
        ...ticz/scripts/lua/script_time_KNMI_Extract00_RPI3.lua:27: in function 'UpdateDev'
        ...ticz/scripts/lua/script_time_KNMI_Extract00_RPI3.lua:97: in main chunk
        [C]: ?
The reference to scriptline 27 is straightforward.
But it is strange/surprising that the scriptline line is still executed without problems to the results.

Reason that I did not show the error report earlier, is that the next reference to line 97 is a puzzle, because the error report is coming regardless whether that line is empty or filled ........
Also headscratching what the meaning might be of the bottom-line in the error report:
[C]: ?

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 17:12
by aleph0
Thats strange ! Which domoticz version are you running ?

Also, it's been a while since I didn't execute a script from the filesystem, I rather run them from inside domoticz with the event editor. Maybe it has an influence ?

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 17:22
by Toulon7559
Present release of my Domoticz is V4.10717

The KNMI script-timing has to be set that you not exceed the limit of 300 calls/day:
with interval at 10 minutes it comfortably becomes 24*6 = 144 calls/day.
More frequent in fact has no meaning, because the source info is refreshed at an interval of 10 minutes.

In this case, for my testruns I temporarily commented the if and end-line of the time-loop (=> running script at highest speed of Domoticz, which in time would violate the limit mentioned above)
and also manually ran the command from Putty's CLI:
lua <full filepath&filename>

THAT CLI results in an error report, that ;-) is the purpose of testing, but it is strange (IMHO) that still the script is executed ......

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 19:08
by aleph0
Ah ok ; then in that case you miss some exports done by domoticz before launching the script. otherdevices_idx is one of this exported tables ; but you shall have many more in that case : otherdevices_svalues, devicechanged, ...

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 19:54
by Toulon7559
;-) Just thinking ahead .....

Textstrings such as 'summary' and 'forecast' are filled with variable textual contents.
For webpage-application with such variable textual contents, it may be handy to have a txt-file or html-file.

From other scripts derived this example working on the extracted KNMI-info.
Simple extension at the end of the KNMI-script.

Code: Select all

Check interval-time and make txt-file
date = os.date("*t")
if  ((time.min+2)%10)==0  then -- Run every 10 minutes, plus delay of 2 minutes
   file = io.open("/home/pi/KNMI_Info.txt", "w+")
   -- Opens a file named KNMI_Info.txt (stored under the designated sub-folder of Domoticz)
   -- in append mode
   -- write lines (as required) to opened file
    file:write("= Meteo, KNMI informatie = \n")
    file:write("  Datum = " .. time.day .. "." .. time.month .. "." .. time.year .. "\n")
    file:write("  Tijd  = " .. time.hour .. ":" .. time.min ..  "\n")
    file:write(" Actueel  = "  .. summary0  .. "\n")
    file:write(" Actueel  =  Tactueel : "..val_Tact.."°C, Tgevoel : "..val_Tgvl.."°C" .. "\n")
    file:write(" Verwacht = " .. summary1  .. "\n")
    file:write(" Verwacht =  Tmin  : "..val_Tmin.."°C, Tmax  : "..val_Tmax.."°C" .. "\n")
    file:close() -- closes the open file

    -- Next line is required to set permissions for open&copy&shift of the file 
    os.execute("chmod a+rw /home/pi/KNMI_Info.txt")
end

The resulting file KNMI_Info.txt can be found in directory /home/pi
If you need it elsewhere, an upload is required in that direction.

Re: Cloud cover from darksky

Posted: Friday 10 May 2019 23:01
by FireWizard
Hi

DarkSky Cloud Cover is now included in Domoticz beta V 4.10730.
However, you may expect it under the Weather Tab, but you will find it under Utility.

Regards

Re: Cloud cover from darksky

Posted: Saturday 11 May 2019 8:51
by Toulon7559
Meteo organisations often provide cloudlayer/-cover expressed according to the octa-scale of 0 to 9.
E.g. this script applies such input (from Ogimet):
0=no cloud , 1-8= cloudy from 1 to 8 max , 9 =Fog , / = no data

Means that the number given by DarkSky must be translated.

;-) Is it an option to add the octa-value to Domoticz' svalue and widget?
Cloudcover a candidate for extra, specific API-JSON-call?

Re: Cloud cover from darksky

Posted: Saturday 11 May 2019 9:03
by aleph0
Dunno for the new widget from darksky plugin. From my script simply multiply cloud cover by 0.08 before storing it in a dedicated octa device. Eventually round it too if you don't want intermediates values

Envoyé de mon moto g(6) en utilisant Tapatalk


Re: Cloud cover from darksky

Posted: Monday 20 May 2019 17:10
by Toulon7559
Alternative solution in aleph0's script

Code: Select all

    val_Cloud=round((jsonValeur.currently.cloudCover)*8,0) -- integer Octa scaling!