Page 1 of 1

Quote of the Day Module

Posted: Thursday 19 October 2017 16:25
by emme
Willing to add some ironic behavior to my notification and as a Murphy's Law fellow, I've created a quote of the day module that can be used wherever you want...
It is extremely simple!

It's made by a file with a simple phrases db and random generated quote and your script where to put it....

the module contains the follow:

Code: Select all

local quote = {};

	function quote.getQuote()
		local quotes = {
			"\nAnything that can go wrong will go wrong ",
            "\nIf there are two or more ways to do something, and one of those ways can result in a catastrophe, then someone will do it",
            "\nLeft to themselves, things tend to go from bad to worse.",
            "\nIt is impossible to make anything foolproof because fools are so ingenious",
            "Add any other quote and save the file, you do not need to change any other line in the code"
            }
		local strQuote = ''
		strQuote = quotes[math.random(1,#quotes)]
		return strQuote
	end
return quote
save the file and copy to domoticz/scripts/lua/ folder and name it quote.lua

into your script use the following to include and use the quote (can be used in dzVents framework also):

Code: Select all

    package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
    local quote = require("quote")
    print(quote.getQuote())
that's all....
ciao
M

Re: Quote of the Day Module

Posted: Wednesday 25 October 2017 17:02
by elmortero
Hi emme,

I saw you post and for the fun of it I wrote following in dzvents.
Instead of creating a file with quotes it gets the quote of the day from http://quotes.rest/
In my example it writes it to a Text sensor but it can of course be used anywhere.
Spoiler: show
local quotefile = '/tmp/quote.json' -- Temporary file to store the servers response
local fetchIntervalMins = 53 -- (Integer) (Minutes, Range 5-60) How often the data shall be retrieved
--thanks to BakSeeda, it first fetches the json and a minute later it processes it.

return {
active = true,
on = {
timer = {
'every minute'
},
},
execute = function(domoticz, device)
local callUrl = false
if (os.date('*t').min % fetchIntervalMins) == 0 then
callUrl = true
elseif ((os.date('*t').min -1) % fetchIntervalMins) ~= 0 then
return
end
local quoteswitch = domoticz.devices('Quote')
local prevquote = tostring(quoteswitch.rawData[1])
print('** Quoter// prevquote = ' ..prevquote)
local linker = tostring('http://quotes.rest/qod.json')
if callUrl then
os.execute('curl -s "'..linker..'" > '..quotefile..'&')
return -- Nothing more to do for now, we'll be back in a minute to read the data!
end
local function readLuaFromJsonFile(fileName)
local file = io.open(fileName, 'r')
if file then
package.path = './scripts/lua/?.lua;'..package.path
local jsonParser = require('JSON')
local _json = file:read('*a')
local json = jsonParser:decode(_json)
io.close(file)
return json
end
return nil
end
local quoteData = readLuaFromJsonFile(quotefile)
if not quoteData then
print('** Quoter// Could not read quoteData from file: '.. quotefile)
return
end
if quoteData.contents.quotes[1].quote ~= nil then
local Qpop = tostring(quoteData.contents.quotes[1].quote)
Qpop = Qpop:gsub("'","") --removes the single quote as it caused issues sending it to the sensor.
print('** Quoter// Qpop = '..Qpop)
quoteswitch.updateText(Qpop)
kQpop = tostring(Qpop)
end
end
}

Re: Quote of the Day Module

Posted: Thursday 26 October 2017 11:26
by emme
it is a quite good solution!
i was looking for a http service like that to be impemented via MQTT and node-red.
unfortunately the non-italian language is actually a limit for me :(

I wish to get a service like that in italian.. and since that.. I would create my own