D'rMorris wrote:Strange... When I look at my devices, the 2 virtual sensors are:
Hardware:VirtualSwitches
Name: TravelToHome
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20
and
Hardware:VirtualSwitches
Name: TravelToWork
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20
Can you check if the lastupdate has a value in the devices section? Otherwise, first start with the script below.
Code: Select all
print('<b style="color:Blue">> Monitoring Traveltimes</b>')
------------- Begin code -------------
commandArray = {}
function traveltime(departx,departy,arrivex,arrivey)
--get route from WAZE--
local waze=assert(io.popen('curl "https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'..departx..'+y%3A'..departy..'&to=x%3A'..arrivex..'+y%3A'..arrivey..'&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS%3At%2CALLOW_UTURNS"'))
local trajet = waze:read('*all')
waze:close()
print("========================================================================================================")
print(trajet)
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")()
-- Update these variables ---------------
--idx of devices for capturing the travel minutes in both direction
idxhomework = 'xxx'
idxworkhome = 'xxx'
--coordinates Home
departy="xx.xxxxxx"
departx="x.xxxxxx"
--Coordinates Work
arrivey="xx.xxxxxx"
arrivex="x.xxxxxx"
-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
workhome=traveltime(arrivex,arrivey,departx,departy)
print("homework:",homework,"workhome:",workhome)
-- 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..'"')
------------- End code -------------
return commandArray
I got an error after retrieving data from Waze:
"...........................
{"x":4.4859796245308,"y":51.905177579834,"z":"NaN"},{"x":4.4859796245308,"y":51.905177579834,"z":"NaN"},{"x":4.485384204847058,"y":51.90486958680452,"z":"NaN"},{"x":4.485384204847058,"y":51.90486958680452,"z":"NaN"},{"x":4.4847550607899835,"y":51.90454488519832,"z":"NaN"},{"x":4.4847550607899835,"y":51.90454488519832,"z":"NaN"},{"x":4.484661151354932,"y":51.904497777241176,"z":"NaN"},{"x":4.484617489349021,"y":51.90447587497455,"z":"NaN"},{"x":4.484617489349021,"y":51.90447587497455,"z":"NaN"},{"x":4.484446818065281,"y":51.904488792804386,"z":"NaN"},{"x":4.484350476380506,"y":51.90454638986189,"z":"NaN"}],"segCoords":null}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 48 100 48 0 0 301 0 --:--:-- --:--:-- --:--:-- 303
========================================================================================================
{ "error" : "Error: 503|Could not find a route"}
lua: script_waze_reistijden.lua:18: attempt to index field 'response' (a nil value)
stack traceback:
script_waze_reistijden.lua:18: in function 'traveltime'
script_waze_reistijden.lua:52: in main chunk
[C]: in ?
"
____________________________________________________________________________________________________________________________
After changing the lat-lon at my work a bit it worked; in front of it is a one way road and Waze couldn't cope with that apparantly.
Now the log displays:
2016-01-25 16:35:02.588 LUA: homework:
2016-01-25 16:35:02.588 LUA: 26
2016-01-25 16:35:02.588 LUA: workhome:
2016-01-25 16:35:02.588 LUA: 26
Only the virtual devices get no values
When I put
http://192.168.123.127:8080/json.htm?ty ... evice&idx=' .. 64 .. '&nvalue=0&svalue=' .. 26 in a browser, I get "status" : "ERR"
___________________________________________________________________________________________________________
I had to change the last part of the script and then it worked! Now the scripts ends like this:
Code: Select all
-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
workhome=traveltime(arrivex,arrivey,departx,departy)
print("homework:",homework,"workhome:",workhome)
-- 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..'"')
commandArray[1] = {['UpdateDevice'] = idxhomework .. '|0|' .. homework}
commandArray[2] = {['UpdateDevice'] = idxworkhome .. '|0|' .. workhome}
------------- End code -------------
return commandArray