On a lot a standard weather stations the phase of the moon can be seen and it looks me a good idea that it would be nice to have this implemented also in Domoticz

Moderators: leecollings, remb0
I believe he'd like the data from: http://www.wunderground.com/weather/api ... onomy&MR=1gizmocuz wrote:Please give more details
URL, screenshot, API, usage, .....
Not sure, but found this: http://www.almanac.com/content/planting ... g-calendargizmocuz wrote:So something like a moon rise/set ?
I still do not get its usage ? Why is this important ? And can't the sun set/rise be used ?
Code: Select all
-- Variables to customize ------------------------------------------------
local city = "London" -- Your city for Wunderground API
local countryCode = "UK" -- Your country code for Wunderground API
local idxmoonpercentage ='214' -- Your virtual moon percentage illuminated Device ID
local idxmoonage ='213' -- Your virtual moon age Device ID
local idxmoonphase ='210' -- Your virtual phase moon Device ID
local idxmoonrise='211' -- Your virtual moon rise variable ID
local idxmoonset='212' -- Your virtual moon set variable ID
local wuAPIkey = "" -- Your Weather Underground API Key
local DEBUG = 0 -- 0 , 1 for domoticz log
-------------------------------------------------------------------------
commandArray = {}
time = os.date("*t")
if ((time.min % 60)==0) then -- Run every hour. Check the wundergroud API limitation before changing this
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
-- API Wunderground
local file=assert(io.popen('curl http://api.wunderground.com/api/'..wuAPIkey..'/astronomy/q/'..countryCode..'/'..city..'.json'))
local raw = file:read('*all')
file:close()
s = (string.gsub(raw, "%c+", ""))
if( DEBUG == 1) then print('Moon'..s) end
-- moonrise
moonriseHour, moonriseMinute = string.match(s, [["moonrise": {"hour":"(%d+)","minute":"(%d+)"]])
print("Moonrise:\t"..moonriseHour..":"..moonriseMinute)
-- moonset
moonsetHour, moonsetMinute = string.match(s, [["moonset": {"hour":"(%d+)","minute":"(%d+)"]])
print("Moonset:\t"..moonsetHour..":"..moonsetMinute)
-- percentage of moon illuminated
percentIlluminated = string.match(s, [["percentIlluminated":"(%d+)"]])
print("Percent:\t"..percentIlluminated.."%")
-- age of moon since last new moon
age = string.match(s, [["ageOfMoon":"(%d+)"]])
print("Age:\t\t"..age)
-- Phase of the moon
moonPhase = string.match(s, [["phaseofMoon":"(.-)"]])
print("Phase:\t"..moonPhase)
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}
end
return commandArray
Code: Select all
daily: {
data: [
{
time: 1465941600,
summary: "Light rain throughout the day.",
icon: "rain",
sunriseTime: 1465963534,
sunsetTime: 1466020087,
moonPhase: 0.34,
precipIntensity: 0.0141,
precipIntensityMax: 0.0317,
precipIntensityMaxTime: 1466017200,...
Code: Select all
-- MoonPhase script
-- Original by: alexsh1 in https://www.domoticz.com/forum/viewtopic.php?f=31&t=107
-- Adapted by wizjos 26-10-2016
-- Requirements:
-- 1 uservariable "MoonphaseCheck"
-- 1 dummy sensor (type percentage) for moonpercentage
-- 3 dummy sensors (type text) for moonphase, moonrise and moonset
-- 2 dummy sensors (type custom) for moonage and moonphase icon
-- 8 custom icon sets (http://domoticz-icon.aurelien-loyer.fr/) for the moonphase icons
-- note: Only the first (or 'on') icon of a custom iconset is used, so you need 8 different sets of custom icons
-- The icons are referenced by their internal number.
-- Variables to customize ------------------------------------------------
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 CITY HERE>" -- Your city for Wunderground API
local countryCode = "NL" -- 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 idxmoonphaseicon = "133" -- Your virtual moon phase icon variable ID
local wuAPIkey = "<YOUR API KEY HERE>" -- Your Weather Underground API Key
local DOMO_IP = "<YOUR DOMOTICZ IP HERE>" -- Domoticz ip address
local DOMO_PORT = "8084" -- 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
message ="Moonphase - Collecting data from: "..url
if (file_exists(tempfilename)==false) then
message=message.."because /var/tmp/phase.tmp does not exist."
end
if (may_update(checkvar,checktime)==true) then
message=message.." it's time."
end
print(message)
print("Difference: "..timedifference(uservariables_lastupdate[checkvar]).." seconds")
end
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
commandArray['Variable:'..checkvar]=time.hour..":"..time.min
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+)"]])
print("Moonrise:\t"..moonriseHour..":"..moonriseMinute)
-- moonset
moonsetHour, moonsetMinute = string.match(s, [["moonset": {"hour":"(%d+)","minute":"(%d+)"]])
print("Moonset:\t"..moonsetHour..":"..moonsetMinute)
-- percentage of moon illuminated
percentIlluminated = string.match(s, [["percentIlluminated":"(%d+)"]])
print("Percent:\t"..percentIlluminated.."%")
-- age of moon since last new moon
age = string.match(s, [["ageOfMoon":"(%d+)"]])
print("Age:\t\t"..age)
-- Phase of the moon
-- set the moonPhaseIcon to your appropriate icon number (8 x)
moonPhase = string.match(s, [["phaseofMoon":"(.-)"]])
if moonPhase=="New" then
moonPhaseIdx = 1
moonPhaseIcon = 103
moonPhase = "Nieuwe maan"
end
if moonPhase=="Waxing Crescent" then
moonPhaseIdx = 2
moonPhaseIcon = 104
moonPhase = "Wassende halve maan"
end
if moonPhase=="First Quarter" then
moonPhaseIdx = 3
moonPhaseIcon = 105
moonPhase = "Eerste kwartier"
end
if moonPhase=="Waxing Gibbous" then
moonPhaseIdx = 4
moonPhaseIcon = 106
moonPhase = "Wassende maan"
end
if moonPhase=="Full" then
moonPhaseIdx = 5
moonPhaseIcon = 107
moonPhase = "Volle maan"
end
if moonPhase=="Waning Gibbous" then
moonPhaseIdx = 6
moonPhaseIcon = 108
moonPhase = "Afnemende maan"
end
if moonPhase=="Last Quarter" then
moonPhaseIdx = 7
moonPhaseIcon = 109
moonPhase = "Laatste kwartier"
end
if moonPhase=="Waning Crescent" then
moonPhaseIdx = 8
moonPhaseIcon = 110
moonPhase = "Afnemende halve maan"
end
-- Because there is no api command available to set a custom icon a JSON call is made
-- This requires the following parameters:
-- type = setused (not to be changed)
-- idx = idxmoonphaseicon (not to be changed here)
-- name = Maanfase (This is the Name of the control. You need to pass this value. This name will be displayd in the Domoticz GUI.
-- description = (left empty: no need for a description in the Domoticz GUI)
-- switchtype = 0 (not to be changed)
-- customimage = moonPhaseIcon (not to be changed here)
-- devoptions = 1;%20 (This is the 'axis-label'. Because only a number is sent a space (%20) is given as axis-label, resulting in no axis-label in Domoticz GUI (not to be changed)
-- used = true (not to be changed)
os.execute('curl -s -i -H "Accept: application/json" "http://"'..DOMO_IP..'":"'..DOMO_PORT..'"/json.htm?type=setused&idx="'..idxmoonphaseicon..'"&name=Maanfase&description=&switchtype=0&customimage="'..moonPhaseIcon..'"&devoptions=1;%20&used=true"')
print("Phase:\t"..moonPhase)
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}
commandArray[6] = {['UpdateDevice'] = idxmoonphaseicon.."|0|"..moonPhaseIdx}
else
if debug then print("MoonPhase - Update not allowed: Difference is "..timedifference(uservariables_lastupdate[checkvar]).." seconds") end
end
return commandArray
The city nameDerik wrote:mmm try this get working...
Do i need the city name
Or the station id?
Code: Select all
http://api.wunderground.com/api/<YourwuAPIkey>/astronomy/q/NL/Nijmegen.json
I'd love to, but I'm not completely sure about what exactly your question/problem is. When looking at your screenshot you're adding a uservariable. I use this variable to determine when the last check (= property lastupdated of this uservariable) was done. At this time plus 3600 seconds ore more the check/update is performed again. The uservariable displays a time, but this is purely cosmetic. The check is performed against the time of the last update.Derik wrote: And the variable:
Do not understand this function?
Please explane som more...
I was not aware of this site. Thanks for mentioning! I'll post my icon-sets a soon as I've sanitized things...Derik wrote:Nice icon..
please share:
https://drive.google.com/drive/u/0/fold ... WdHV1Qxbm8
[email protected]
I can confirm it isalexsh1 wrote:Good work @ wizjos!
Is your moon icon changing according to the moon phase? Very nice - now I'd like to implement it too
Users browsing this forum: Google [Bot] and 1 guest