The other thing I did is put in a weekend value, and if true it goes to end of the script right away ( goto ::done::)
@sjefk: I've also included to virtual switches to check, which are tied to my iPhone and geofencing. This means that I won't get any messages for my Home Work travel in the morning if I'm not actually home, same goes for the return from Office to home again. I've decleared these 2 switches at the top of the script, which I've shared below:
Code: Select all
---------------------------------
--Script to calculate duration and distance between two points using Google Maps
--Author : woody4165 based on Neutrino Traffic with Waze, updated by G3rard
--Date : 9 April 2016
---------------------------------
RayHome = 'RayHome' -- Domoticz name of the iPhone/Mobile device or other presence switch or virtual switch
RayWork = 'RayWork'
commandArray={}
time = os.date("*t")
day = tonumber(os.date("%w"))
--idx of devices for capturing the travel minutes in both direction
idxtraffic='yourIDXhere'
idxroute='yourIDXhere'
--usual traveltime (mins)
usualtimehomework = 40
usualtimeworkhome = 40
-- Coordinates starting point
fromx="xx.xxxxxx"
fromy="xx.xxxxxx"
-- Coordinates destination point
tox="xx.xxxxxx"
toy="xx.xxxxxx"
--Google Api Key
key='your key here'
--determine workday (Mo-Fri)
if (day > 0 and day < 6) then
week=true; weekend=false
else
week=false; weekend=true
end
--determine time (7:00-9:00, 16:00-19:00)
if ((time.hour > 6 and time.hour < 9) or (time.hour == 9 and time.min < 1)) then
mattina=true; sera=false
elseif ((time.hour > 16 and time.hour < 19) or (time.hour == 19 and time.min < 1)) then
mattina=false; sera=true
else
mattina=false; sera=false
end
if weekend then goto done
end
--calculate traveltime
function traveltime(fromx,fromy,tox,toy)
--import JSON.lua library
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
-- get data from Google Maps and decode it in gmaps
local jsondata = assert(io.popen('curl "https://maps.googleapis.com/maps/api/directions/json?origin='..fromx..','..fromy..'&destination='..tox..','..toy..'&departure_time=now&key='..key..'"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local gmaps = json:decode(jsondevices)
-- Read from the data table, and extract duration and distance in value. Divide distance by 1000 and duration_in_traffic by 60
distance = gmaps.routes[1].legs[1].distance.value/1000
duration = gmaps.routes[1].legs[1].duration_in_traffic.value/60
summary = gmaps.routes[1].summary
-- mins round the duration
mins=math.ceil(duration)
return mins
end
--weekday (Mo-Fri) and time between 7:00-9:00
if (week and mattina) and otherdevices[RayHome] == "On" then
--every 15 minutes
if((time.min % 15)==0) then
traffic=traveltime(fromx,fromy,tox,toy)
message=tostring(traffic)..' min reistijd naar werk '..("%02d:%02d"):format(time.hour, time.min)..' route '..tostring(summary)
print(message)
--send message if traveltime is longer than usual
if(traffic > usualtimehomework) then
--os.execute('curl --data chat_id=xxxxxxxx --data-urlencode "text='..message..'" "https://api.telegram.org/botxxxxxxxxxxxxx/sendMessage" ')
commandArray['SendNotification']=message
end
--return a text to the device (eg. 12 mins)
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(traffic)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(message)}
end
--weekday (Mo-Thu) and time between 16:00-19:00
elseif (week and sera) and otherdevices[RayWork] == "On" then
--every 15 minutes
if((time.min % 15)==0) then
traffic=traveltime(tox,toy,fromx,fromy)
message=tostring(traffic)..' min reistijd naar huis '..("%02d:%02d"):format(time.hour, time.min)..' route '..tostring(summary)
print(message)
--send message if traveltime is longer than usual
if(traffic > usualtimeworkhome) then
--os.execute('curl --data chat_id=xxxxxx --data-urlencode "text='..message..'" "https://api.telegram.org/botxxxxxxxxx/sendMessage" ')
commandArray['SendNotification']=message
end
--return a text to the device (eg. 12 mins)
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(traffic)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(message)}
end
-- set device to 0 to prevent device get the last value after working hour
elseif((time.min % 15)==0) then
zero=0
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(zero)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(zero)}
end
::done::
return commandArray