Re: Dashticz v2.0, custom positioning and multiple screens
Posted: Thursday 27 April 2017 17:10
After replace with this script, everything updated again and no errors 

wizjos wrote:OK I think I've got the moonphase lua script working again. I made the mistake to assume lua has a function 'round' when porting it from js to lua.
But now I think I've 'floored' the problem![]()
New Moonphase LUA script:Please confirm...Code: Select all
-- Variables to customize ------------------------------------------------ local moonpicture = "MoonPicture" -- name of the uservar to write the name of the moonphase picture to local checkvar = "MoonphaseCheck" -- name of the uservar to check if update is allowed local checktime = 3600 -- check allowed every x seconds 3600 = 60 min. Check the wundergroud API limitation before changing this local city = "<your town>" -- Your city for Wunderground API local countryCode = "<YOUR COUNTRY CODE>" -- Your country code for Wunderground API local idxmoonpercentage ='125' -- Your virtual moon percentage illuminated Device ID local idxmoonage ='131' -- Your virtual moon age Device ID local idxmoonphase ='132' -- Your virtual moon phase Device ID local idxmoonrise='124' -- Your virtual moon rise variable ID local idxmoonset='127' -- Your virtual moon set variable ID local wuAPIkey = "<your key>" -- Your Weather Underground API Key local DOMO_IP = "<your domo ip>" -- Domoticz ip address local DOMO_PORT = "<your domo port>" -- Domoticz port local tempfilename = '/var/tmp/phase.tmp' -- can be anywhere writeable local debug=false -- false, true for domoticz log ------------------------------------------------------------------------- function file_exists(path) -- function to check if a file exists local file = io.open(path, "rb") if file then file:close() end return file ~= nil end function timedifference(s) -- function to determine the difference in seconds between the current time and a given one year = string.sub(s, 1, 4) month = string.sub(s, 6, 7) day = string.sub(s, 9, 10) hour = string.sub(s, 12, 13) minutes = string.sub(s, 15, 16) seconds = string.sub(s, 18, 19) t1 = os.time() t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds} difference = os.difftime (t1, t2) return difference end function may_update(device, timeelapsed) -- function to check whether an update is allowed return uservariables_lastupdate[device] == nil or timedifference(uservariables_lastupdate[device]) >= timeelapsed end commandArray = {} time = os.date("*t") url='http://api.wunderground.com/api/'..wuAPIkey..'/astronomy/q/'..countryCode..'/'..city..'.json' if (may_update(checkvar,checktime)==true) or (file_exists(tempfilename)==false) then -- read API Wunderground if debug then print("Moonphase - Collecting data from: "..url) end os.execute('curl -s '..url..' > '..tempfilename) -- NOTE: if the command above doens't work in your situation try -- read = os.execute('curl -s -o '..tempfilename..' "'..url..'"') -- instead! Thanks to EdKo66 file = io.open(tempfilename, "r") s= file:read("*a") s = (string.gsub(s, "%c+", "")) file:close() -- moonrise moonriseHour, moonriseMinute = string.match(s, [["moonrise": {"hour":"(%d+)","minute":"(%d+)"]]) if debug then print("Moonrise:\t"..moonriseHour..":"..moonriseMinute) end -- moonset moonsetHour, moonsetMinute = string.match(s, [["moonset": {"hour":"(%d+)","minute":"(%d+)"]]) if debug then print("Moonset:\t"..moonsetHour..":"..moonsetMinute) end -- percentage of moon illuminated percentIlluminated = string.match(s, [["percentIlluminated":"(%d+)"]]) if debug then print("Percent:\t"..percentIlluminated.."%") end -- age of moon since last new moon age = string.match(s, [["ageOfMoon":"(%d+)"]]) if debug then print("Age:\t\t"..age) end -- Phase of the moon -- set the moonPhaseIcon to your appropriate icon number (8 x) moonPhase = string.match(s, [["phaseofMoon":"(.-)"]]) if moonPhase=="New Moon" then moonPhase = "Nieuwe maan" is_waxing = true end if moonPhase=="Waxing Crescent" then moonPhase = "Wassende halve maan" is_waxing = true end if moonPhase=="First Quarter" then moonPhase = "Eerste kwartier" is_waxing = true end if moonPhase=="Waxing Gibbous" then moonPhase = "Wassende maan" is_waxing = true end if moonPhase=="Full" then moonPhase = "Volle maan" is_waxing = false end if moonPhase=="Waning Gibbous" then moonPhase = "Afnemende maan" is_waxing = false end if moonPhase=="Last Quarter" then moonPhase = "Laatste kwartier" is_waxing = false end if moonPhase=="Waning Crescent" then moonPhase = "Afnemende halve maan" is_waxing = false end -- calculate moonphase picture if percentIlluminated == '0' then n='50' else if waardecheck==false then picnumber=math.floor(math.abs(percentIlluminated-100)/2) n=tostring(picnumber) if n.length==1 then n='0'+n end else picnumber=math.floor(50+(percentIlluminated/2)) if picnumber == 100 then picnumber=99 end end n=tostring(picnumber) end picture='moon.'..n..'.png' if debug then print('Picture number: '..n..' '..'Picture name: '..picture) end commandArray['Variable:'..checkvar]=moonPhase commandArray['Variable:'..moonpicture]=picture commandArray[1] = {['UpdateDevice'] = idxmoonphase.."|0|"..moonPhase} commandArray[2] = {['UpdateDevice'] = idxmoonrise.."|0|"..moonriseHour..":"..moonriseMinute} commandArray[3] = {['UpdateDevice'] = idxmoonset.."|0|"..moonsetHour..":"..moonsetMinute} commandArray[4] = {['UpdateDevice'] = idxmoonage.."|0|"..age} commandArray[5] = {['UpdateDevice'] = idxmoonpercentage.."|0|"..percentIlluminated} else if debug then print("MoonPhase - Update not allowed: Difference is "..timedifference(uservariables_lastupdate[checkvar]).." seconds") end end return commandArray
Regards
Wizjos