Why the script is not functioning?
-- script_time_Waze.lua
print('<b style="color:Blue"> >> Monitoring Traveltimes</b>')
------------- Begin time functions -------------
function timedifference_lastupdate_homework (s)
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_lastupdate_homework = os.difftime (t1, t2)
return difference_lastupdate_homework
end
function timedifference_lastupdate_workhome (s)
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_lastupdate_workhome = os.difftime (t1, t2)
return difference_lastupdate_workhome
end
------------- End time functions -------------
------------- Begin local variables -------------
-- User variables --
----------------------
-- IP Address and port
IP = '192.168.1.8'
port = '8080'
--idx of devices for capturing the travel time (in minutes) in both direction
idxhomework = '7' -- Home to Work
idxworkhome = '8' -- Work to Home
HomeToWork = 'Home to Work' -- Replace by the name of your dummy sensor
WorkToHome = 'Work to Home' -- Replace by the name of your dummy sensor
--Coordinates Home
--
https://www.waze.com/fr/livemap?zoom=17 ... n=12.52294 -- Ex. : Bagneux, France
departy = '41.96476'
departx = '12.52294'
--Coordinates Work
--
https://www.waze.com/fr/livemap?zoom=17 ... n=12.46096 -- Ex. : La Défense, France
arrivey = '41.9612'
arrivex = '12.46096'
----------------------
-- Debug variables:
----------------------
debug = uservariables["debug_general"]
timeout = uservariables["timeout_general"]
difference_homework = timedifference_lastupdate_homework(otherdevices_lastupdate[HomeToWork])
difference_workhome = timedifference_lastupdate_workhome(otherdevices_lastupdate[WorkToHome])
------------- End local variables -------------
------------- Begin debug statements -------------
if debug=="Y"
then
print('<b style="color:Red"> >> Debug is set to: ' .. debug .. '</b>')
print('<b style="color:Red"> >> Update timeout: ' .. timeout .. '</b>')
print('<b style="color:Red"> >> Lastupdate Home to Work : ' .. difference_homework .. '</b>')
print('<b style="color:Red"> >> Lastupdate Work to Home : ' .. difference_workhome .. '</b>')
end
------------- End debug statements -------------
------------- Begin code -------------
commandArray = {}
if (difference_homework > timeout and difference_workhome > timeout)
then
function traveltime(departx,departy,arrivex,arrivey)
--get route from WAZE--
local waze=assert(io.popen('curl "
https://www.waze.com/row-RoutingManager ... LOW_UTURNS"'))
local trajet = waze:read('*all')
waze:close()
if debug=="Y"
then
print("========================================================================================================")
print(trajet)
print("========================================================================================================")
end
local jsonTrajet = json:decode(trajet)
--Get major road from JSON
routeName = jsonTrajet['response']['routeName']
--Get route steps from JSON info
route = jsonTrajet['response']['results']
--Calculate the total time by adding the time of all sections
routeTotalTimeSec = 0
for response,results in pairs(route) do
routeTotalTimeSec = routeTotalTimeSec + results['crossTime']
end
--translate the number of seconds to minutes
routeTotalTimeMin = routeTotalTimeSec/60-((routeTotalTimeSec%60)/60)
return routeTotalTimeMin
end
--import JSON addin (already used with DTGBOT and stored in the standard library)
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- check presence of this script if problem
-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
workhome=traveltime(arrivex,arrivey,departx,departy)
if debug=="Y"
then
print('Home to Work : ' .. homework .. ' minutes')
print('Work to Home : ' .. workhome .. ' minutes')
end
-- update Domoticz devices
url='http://' .. IP .. ':' .. port .. '/json.htm?type=command¶m=udevice&idx=' .. idxhomework .. '&nvalue=0&svalue=' .. homework
read = os.execute('curl -s "'..url..'"')
url='http://' .. IP .. ':' .. port .. '/json.htm?type=command¶m=udevice&idx=' .. idxworkhome .. '&nvalue=0&svalue=' .. workhome
read = os.execute('curl -s "'..url..'"')
print('<b style="color:Green"> >> Update OK !</b>')
else
if debug=="Y"
then
print('<b style="color:Red">>> HomeWork lastupdate: ' .. difference_homework .. ' seconds ago. Must be > to '.. timeout ..'</b>')
print('<b style="color:Red">>> WorkHome lastupdate: ' .. difference_workhome .. ' seconds ago. Must be > to '.. timeout ..'</b>')
end
end
------------- End code -------------
return commandArray
Which is the procedure to associate the script to the virtual sensor?
Raspberry PI 3, 3x Yeelight bulbs, 1x Yeeligh led strip, 1x Sonoff with ESP826, Controlicz, Kodi