It is time to come back to the timers added to some lamps as shown in the Timers and Notification section of last post of the series X10 with Domoticz on Raspberry Pi (Domespic). The timers were not enabled, because I would "activate them manually when [...] absent from the house. I am sure there is a better way of doing this...".
What I am about to describe is a better way. As to whether it is the best way...
Timers by themselves are a good way to schedule at what times lamps and other devices are to be turned on and off. This may be useful if you want an outside light to be turned on at dusk and turned off at midnight at all times. This is not what I needed. I want to have lights turn on and off at "usual" times when no one is at home to simulate an inhabited house. To my way of thinking this is the prevention half of a home security system. The other half would use detection sensors and sound an alarm of some sort if an intruder were to be detected. More about that later, ... much later.
In essence, what I want are conditional timers, by which I mean, timers that will work only when triggered to do so. The first step is to figure out how to enable and disable timers. And the second step is to setup a trigger.
Enabling Device Timers
Make sure that all schedule timer events are disabled. Go to the
Switches tab and click on the
button of the Dressser
lamp
device.
As can be seen all the scheduled on and off commands are disabled ("Active" is
set to "No").
It is possible to enable or disable a scheduled event, but its index number must
be known. As far as I know, the only way to get these numbers is through the JSON
API. You can display all the schedules in a web browser at the following address
http://192.168.0.22:8080/json.htm?type=schedules
.
Another possibility,
with a Linux desktop on the same network as the Raspberry Pi, is to open a terminal
(Alt-Ctrl-T in Ubuntu) and to use the curl
program. If your desktop
is a Windows system, you could open an SSH sesssion with the Raspberry Pi and
use its curl
program.
We will now enable the '00:52' Dresser lamp off scheduled event
with TimerId = 9. In a web browser enter the following address
http://192.168.0.22:8080/json.htm?type=command¶m=enabletimer&idx=9
Alternatively, open a terminal and type the following command
Go back to the Switches tab and
click on the button of the Dressser
lamp
device and check that the '00:52' Dresser lamp scheduled
off is enabled. Again you can also check by entering the following
http://192.168.0.22:8080/json.htm?type=schedules
in the
address field of a web browser.
To disable a scheduled command replace enabletimer
with
disabletimer
as in
http://192.168.0.22:8080/json.htm?type=command¶m=disabletimer&idx=9
Triggering the Timers
We will create a virtual switch on virtual (dummy) hardware which will be used to enable or disable the timer. We already setup a dummy switch on dummy hardware when adding the Heyu bridge, so here is a quick outline of the steps.
- Click on Setup tab.
- Click on on the Hardware menu choice.
- Fill in the fields
Name =Virtual
, and
Type =Dummy (Does nothing...
- Click on the button.
- Click on the
Virtual
hardware.
in
the - Name the sensor
Away
and change its type to switch. - Click on the Switches tab.
- Click on the
Away
switch and change the icon to theGeneric
on off icon - (optional)
button of the
- Change the On Action field
to
http://192.168.0.22:8080/json.htm?type=command¶m=enabletimer&idx=9
- Change the Off Action field
to
http://192.168.0.22:8080/json.htm?type=command¶m=disabletimer&idx=9
It's time to test if this works. Click on the icon of the Away
switch in the
Switches tab to turn the virtual device on and off. After each click, check on
the status of the '00:52' Dresser lamp off scheduled event as explained above. You
should see that it becomes enabled and disabled in sync with the virtual switch.
The principle is proven but clearly something has to be changed because many scheduled actions need to be enabled or disabled. A script should be able to handle the full task.
Away Scripts
The following Lua script takes care of updating the four timers associated with the Dresser lamp. Note how the script calls on the operating system to invoke the cURL command line tool to send the appropriate JSON Api string to the Domoticz server.
The script does work but the following error/warning message is reported:
Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_device_away.lua
has been running for more than 10 seconds
.
A 10 second delay may be acceptable for a script that is executed rarely. Still, I like to avoid warnings when possible. Reading the forum I saw that some use a bash script to enable or disable timers. I tried that and it works, no more warning.
I decided to write a single bash script that will either enable or disable the timers based on the value of a command line parameter. It should be "1" (without quotes) to enabled the timers or "0" (again without quotes) to disable the timers. If any other value is specified the an "Invalid parameter" message is put on the Domoticz log, and the script stops and returns error code 1. Here is the script named enable_timers.sh:
I created the script on my desktop computer and saved it in a new
directory called bash
of the scripts
directory. Then using FileZilla, I created a
bash
directory in the scripts
of the Raspberry Pi
and copied the script into that directory. Then I clicked with the
right mouse button on the copied file name and chose the menu item
File permissions...
and checked the Execute
box in the Owner permissions
.
Finally I clicked on the Ok
button.
Now the Domoticz server has to be told to invoke
this script when the Away button is clicked.
Click on the button.
The On Action field has to contain the
following link script:///home/pi/domoticz/scripts/bash/enable_timers.sh 1
while the Off Action field must contain script:///home/pi/domoticz/scripts/bash/enable_timers.sh 0
Holidays
Many in the Domoticz
forum
suggest another method. It turns out that all devices can have two
independent sets of timers.
To switch between the sets click on the Setup tab,
choose the Settings menu item and finally
click on the Other tab. Find the
Timer Plan in the
Other Settings page.
Choose either the default
or the Holiday
plan.
Don't forget to click on Apply Settings in the top right corner.
This would be good approach especially if there is a way use a virtual switch to select which timer plan is in effect. It would be a great approach, if there were a second set of Notifications. By default, I would have no timers and no notifications, and when leaving I could switch the Timer/Notification Plans.
The Security Panel
The obvious choice for a trigger to enable and disable timers
would have been the Domoticz security panel.
Click on the Setup tab and select
More Options menu item and then
Security Panel and you will see
a representation of a security system
Play around with the buttons Disarm, Arm Home and so on. It is kind of fun to see. And don't worry, no alarms will wake up the neighbours since nothing is connected yet.
I will look into using the Security Panel as a trigger later when I will have the means of doing detection of intruders.
February 10th, 2017 Update
Of course, it will be necessary to modify the #update timers
part of the enable_timers.sh
script as timers are added or
deleted from the system. A user on the Domoticz forum, philchillbill, has
written a Perl script that will lookup the timer idx values and
generate the script file. That will be a useful time saving tool.
That post has been added to the Domoticz topic that I referenced above. You will find other valuable posts there.