Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Mijnhout committed Mar 23, 2017
2 parents 3452b89 + 23af185 commit a52126f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Change Log - domoticz scripts
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
Changelog based on guidelines from [Keep a CHANGELOG](http://keepachangelog.com/).

## 1.0.1
#### Fixed:
- script_timae_radar.lua script now uses alert virtual hardware device with status updates.
- script_timae_traffic.lua script now uses alert virtual hardware device with status updates.

## 1.0.0
#### Added:
- script_timae_radar.lua script.
- script_timae_traffic.lua script.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# domoticz_scripts
Collection of domoticz lua scripts
# Domoticz Lua scripts
Collection of domoticz lua scripts, currently consisting of:

- `script_time_radar.lua`: Radar detection and alerting on specified roads (Netherlands only)
- `script_time_traffic.lua`: Traffic jam detection and alerting on specified routes


## Installation:
Put the scripts you want inside your domoticz lua scripts folder:

`domoticz/scripts/lua`

Keep the name of the script filenames the same, and they will be
triggered every minute (actual code execution every x minutes can
be set inside every script).
33 changes: 22 additions & 11 deletions script_time_radar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- send out a notification.
--
-- In Domotics create new hardware of type Dummy if you don't already
-- have it. After that, create a new virtual sensor, type = 'tekst'. In
-- have it. After that, create a new virtual sensor, type = 'waarschuwing'. In
-- 'apparaten' you will find this new device. The number in the 'Idx'
-- column is your virtual_device_index.
--
Expand All @@ -14,7 +14,7 @@
--
-- (c) 2017 Johnny Mijnhout

commandArray={}
------------ EDIT THESE SETTINGS TO YOUR PERSONAL VALUES ----------------------------

-- road names to check, enclosed and separated by pipe symbols(for example |A1|A201|)
roads="<-- your road list -->"
Expand All @@ -34,6 +34,11 @@ home_hour_end = 9
work_hour_start = 15
work_hour_end = 17

------------------------ END SETTINGS -----------------------------------------------

-- init commandArray
commandArray = {}

-- check if a message has been sent
message_sent = tonumber(uservariables[user_variable_name])

Expand All @@ -56,30 +61,36 @@ end
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

-- set defaults
message = ""
radar_found = false
message = ""
total_radars = 0

-- get data from ANWB
jsondata = assert(io.popen("curl 'https://www.anwb.nl/feeds/gethf'"))
jsondevices = jsondata:read("*all")
jsondata:close()
anwb = json:decode(jsondevices)
json_data = assert(io.popen("curl 'https://www.anwb.nl/feeds/gethf'"))
json_table = json_data:read("*all")
json_data:close()
anwb = json:decode(json_table)
for k, v in pairs(anwb.roadEntries) do
road = tostring(v.road)
if string.match(roads,"|"..road.."|") then
radar_number = # v.events.radars
if radar_number > 0 then
radar_found = true
message = message..tostring(radar_number).." flitser(s) op de "..road.."! "
total_radars = total_radars + radar_number
end
end
end

-- if no radars are found, set this in the message
if not radar_found then
if total_radars == 0 then
message = "Geen flitsers gevonden op de "..string.sub(string.gsub(roads, "|", ", "), 3, -3)
end

-- set alert status
alert_status = total_radars + 1
if alert_status > 4 then
alert_status = 4
end

-- append time to message
message = message.." (update "..tostring(os.date("%H:%M"))..")"

Expand All @@ -90,7 +101,7 @@ if radar_found and message_sent == 0 then
end

-- update virtual device text
commandArray["UpdateDevice"] = virtual_device_index.."|0|"..tostring(message)
commandArray["UpdateDevice"] = virtual_device_index.."|"..alert_status.."|"..tostring(message)

::done::
return commandArray
47 changes: 33 additions & 14 deletions script_time_traffic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
-- You will find the geolocation after the @ symbol in the url.
--
-- In Domotics create new hardware of type Dummy if you don't already
-- have it. After that, create a new virtual sensor, type = 'tekst'. In
-- have it. After that, create a new virtual sensor, type = 'waarschuwing'. In
-- 'apparaten' you will find this new device. The number in the 'Idx'
-- column is your virtual_device_index.
--
Expand All @@ -23,7 +23,7 @@
--
-- (c) 2017 Johnny Mijnhout

commandArray={}
------------ EDIT THESE SETTINGS TO YOUR PERSONAL VALUES ----------------------------

-- location coordinates (for example 12.3456,65.4321)
home_coordinates = "<-- your home geo location -->"
Expand Down Expand Up @@ -55,6 +55,11 @@ max_work_home_minutes = 60
home_name = "huis"
work_name = "werk"

------------------------ END SETTINGS -----------------------------------------------

-- init commandArray
commandArray = {}

-- check if a message has been sent
message_sent = tonumber(uservariables[user_variable_name])

Expand All @@ -69,13 +74,11 @@ end
if time_object.hour >= home_hour_start and time_object.hour <= home_hour_end then
origin_coordinates = home_coordinates
destination_coordinates = work_coordinates
origin_name = home_name
destination_name = work_name
max_travel_minutes = max_home_work_minutes
elseif time_object.hour >= work_hour_start and time_object.hour <= work_hour_end then
origin_coordinates = work_coordinates
destination_coordinates = home_coordinates
origin_name = work_name
destination_name = home_name
max_travel_minutes = max_work_home_minutes
else
Expand All @@ -90,43 +93,59 @@ end
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

-- get traffic data from Google Directions API
jsondata = assert(io.popen("curl 'https://maps.googleapis.com/maps/api/directions/json?origin="..origin_coordinates.."&destination="..destination_coordinates.."&departure_time=now&key="..google_api_key.."'"))
jsondevices = jsondata:read("*all")
jsondata:close()
gmaps = json:decode(jsondevices)
json_data = assert(io.popen("curl 'https://maps.googleapis.com/maps/api/directions/json?origin="..origin_coordinates.."&destination="..destination_coordinates.."&departure_time=now&key="..google_api_key.."'"))
json_table = json_data:read("*all")
json_data:close()
gmaps = json:decode(json_table)

-- Read from the data table
distance_km = math.ceil(gmaps.routes[1].legs[1].distance.value/1000)
distance_km = math.ceil(gmaps.routes[1].legs[1].distance.value/1000)
duration_minutes = math.ceil(gmaps.routes[1].legs[1].duration_in_traffic.value/60)
summary_text = gmaps.routes[1].summary
summary_text = gmaps.routes[1].summary

-- calculate delay
delay_minutes = duration_minutes - max_travel_minutes
if delay_minutes < 0 then
delay_minutes = 0
end

-- translate summary text
summary_text = string.gsub(summary_text, "and", "en")

-- create string with travel information
travel_text = tostring(duration_minutes).." min reistijd van "..tostring(origin_name).." naar "..tostring(destination_name).." ("..tostring(distance_km).." km), via "..tostring(summary_text)
travel_text = tostring(duration_minutes).." min reistijd naar "..tostring(destination_name).." ("..tostring(distance_km).." km), via "..tostring(summary_text)

-- set message to be pushed and stored
if duration_minutes > max_travel_minutes then
if delay_minutes > 0 then
message = "FILE! "..travel_text
-- if no message has been sent, send out the notification
if message_sent == 0 then
commandArray["SendNotification"] = message
commandArray["Variable:"..user_variable_name] = "1"
end
-- set alert status
if delay_minutes > 30 then
alert_status = 4
elseif delay_minutes > 15 then
alert_status = 3
elseif delay_minutes > 0 then
alert_status = 2
end
else
message = "Weg is vrij, "..travel_text
-- if a message has been sent, send a new one that traffic has cleared
if message_sent == 1 then
commandArray["SendNotification"] = message
commandArray["Variable:"..user_variable_name] = "0"
end
--reset alert status
alert_status = 1
end
-- append time to message
message = message.." (update "..tostring(os.date("%H:%M"))..")"

-- update virtual device text
commandArray["UpdateDevice"] = virtual_device_index.."|0|"..tostring(message)
-- update virtual device alert + counter
commandArray['UpdateDevice'] = virtual_device_index.."|"..alert_status.."|"..tostring(message)

::done::
return commandArray

0 comments on commit a52126f

Please sign in to comment.