Because I am kind of addicted to dzVents for the interaction between domoticz and the outside world, I tried to combine these two in the attached script.
Explanation is inside the script. Tested with the default python version (2.7) on my debian stretch PI3 system with domoticz V4.9788 and dzVents 2.4.7 but expect it to work with older / newer versions as well.
Have fun !
Code: Select all
--[[ pythonHoliday.lua
get Holiday string or "None" based on current date.
Before the dzVents script can work the python holidays module needs to be imported on your system.
Script is tested on dzVents 2.4.7 but will most likely also work on earlier versions
This script use the result string from the python call to determine how to set a user-variable and
to update a text device but you can make other choices
]]--
return {
on = { timer = {"at 00:01"}}, -- Once a day right after midnight
logging = { level = domoticz.LOG_ERROR, -- change to LOG_DEBUG when needed
marker = "pythonHoliday" },
execute = function(dz, _) -- dz is short for domoticz; _ because we don't need trigger object
-- Enter your settings below this line
local country = "NL" -- other country abbreviations to be found at https://pypi.org/project/holidays/
local holidayVarName = "Holiday" -- This var should be defined as type string
local holidayTextDeviceIDX = 992 -- This virtual text device should be be defined
local none = "geen feestdag." -- Localized text
-- local testDate = "2018-12-25" -- Use christmas as a test; uncomment to use this date
-- Enter your settings above this line
function quote(str) -- Add double quotes around string
return "\""..str.."\""
end
local mydate = testDate or os.date('%F') -- yyyy-MM-DD
local shellCommand = "echo 'import holidays;holiday=holidays." .. -- compose shellCommand
country ..
"(); print (holiday.get(" ..
quote(mydate) ..
"))' | python "
local filePointer = io.popen(shellCommand, 'r') -- execute command and output to filePointer
local resultString = filePointer:read('*a'):gsub("\n", "") -- readvar and remove line break
filePointer:close()
dz.log("Return from python call: " .. mydate .. " " .. resultString,dz.LOG_DEBUG)
-- Just for lay-out purposes
if resultString == "None" then
resultString = none
end
local fullText = os.date(" %A %d %B, %Y"):gsub(" 0"," ") .. " is " .. resultString
if testDate then
fullText = mydate .. " is " .. resultString
end
-- Update textdevice and uservariable
dz.devices(holidayTextDeviceIDX).updateText(fullText)
dz.variables(holidayVarName).set(fullText)
end
}