Friday, January 19, 2018

Tuning motion.conf lightswitch on raspberry pi for hue lights

Recap on using an non-recording old CCTV to create a motion sensor using raspberry to trigger hue lights with apple homekit automation. Ok, thats a long way around things. Here's a flow

1. Network Camera > 2. motion (pi) on_event  > 3. virtual motion sensor (domoticz / homekit plugin) > 4. Apple home automation (motion on, no motion off)

One of the initial problems was that the light turning off will trigger the motion/light. the solution is to modify the 'lightswitch' parameter, which doesn't have a lot of references to what to set, so it's trial and error as each camera feed are always different..

Noted that I did first change the framerate to 20 prior for better quality. To tune the lightswitch setting, set yourself up around the time the motion light is supposed to come on and follow these 3 steps as a general guideline.

1) Set the event_gap to a small number for testing, e.g. 10 seconds so that you do not need to wait for a long time before testing the light changes and motion detection.

2) set some base parameters which make sense, 2 or 3 is ok for the 2 variables.

- set minimum_motion_frames 2
- set pre_capture 2

3) start playing around with the lightswitch value. to figure out how it works, set it to the max and min and see which one doesn't give a false positive. I started with 100 and 10 and played with everything inbetween until i realised that 10 worked best anyway for my hue light at the door.

Wednesday, January 03, 2018

Network camera as motion sensor for lights


Here's a guide to use an old network cctv feed as a motion sensor for lights.

Here's what we need

1. URL for jpg feed from the network camera

The URL varies for different brands and models.

Example URL: http:///cgi-bin/net_jpeg.cgi?ch=0 


2. Raspberry pi with domoticz (already integrated with lights, e.g. hue) 

On the raspberry pi, download Motion

$ sudo apt-get motion

Modify the /etc/motion/motion.conf file (Refer to the motion documentation)

I removed the following from the motion.conf create a new thread file for each video source

comment out in /etc/motion/motion.conf

; videodevice /dev/video0
; target_dir /var/lib/motion
; stream_port 8081

I also changed the webcontrol_port to 8000 as 8080 conflicts with the default domoticz port.

;webcontrol_port 8080
webcontrol_port 8000


I added a thread file for each video source

thread /etc/motion/camera.conf


The following is an example of a tread file called camera.conf. You need to create a new thread file for each camera

netcam_userpass : user:password
text_left Camera
netcam_url http://netcam_ip/cgi-bin/net_jpeg.cgi?ch=0
target_dir /mnt/nas/camera
stream_port 8001
on_event_start /usr/bin/curl -s "http://domoticz_ip:8080/json.htm?type=command&param=switchlight&idxswitch_id=&switchcmd=On"
on_event_end /usr/bin/curl -s "http://domoticz_ip:8080/json.htm?type=command&param=switchlight&idx=switch_id&switchcmd=Off"


The variables you need to replace are:

user:password: if needed
netcam_ip and URL
domoticz_ip: IP address of the rasberry pi running domoticz
switch_id is the switch id of your light that you want.
Make sure that the steam_port is not already used on the pi and for each camera, to change the stream port.


Optional

If you want to have a bit more smarts for the motion sensor rather than having to keep turning on and off the motion process at night, create a Motion detection dummy virtual device on domoticz and replace that lightswitch id with the motion sensor.

It is possible to use the apple homekit/homebridge to add the virtual motion sensor to apple home and created an automation to set it for only turn on the light at night and when someone is home.



Sunday, November 19, 2017

Restart Fibaro HCL when it stops working automatically

It seems that the Fibaro HCL seems to hang every now and then. Instead of trying to restart it regularly, which doesn't really work, as it's almost impossible to predict when this will happen.

This method checks that the HCL is actually running and in the event it stops working, trigger a script that will restart it.


1. Get a non Fibaro controlled power plug and scripts to control it.

I used a wifi smart plug, TP-link HS100 and downloaded the scripts from

https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/


2. Create a bash script to restart the Fibaro, e.g. restart_fibaro.sh

#!/bin/bash

ip_addr=
scripts=

$scripts/tplink-smartplug.py -t $ip_addr -c off
sleep 10
$scripts/tplink-smartplug.py -t $ip_addr -c on


3. Create a test global variable in the Fibaro HCL

Go to the variables panel and create a test variable, e.g. Test


4. Add a cron entry to test that the Fibaro API is still working and restart if it's not.

The cron script is scheduled every 5 mins and turns on and off the power to the HCL in 10 seconds when the Fibaro API does not respond.

Cron entry:

*/5 * * * * wget http://:@/api/globalVariables/Test --timeout 30 -O - 2>/dev/null  | grep "Test" || /home/pi/scripts/restart_fibaro.sh

Make sure to replace the following and for the Fibaro HCL

Sunday, May 14, 2017

Daikin FT aircon and Remotec ZXT 120

In the quest to make the existing appliances "smart" or otherwise enabling dumb devices to be even dumber with unnecessary initiative.

I got the reputed bad review Remotec ZXT 120. As I already expected it to be bad, my expectations were clearly exceeded.

This isn't the first attempt I've tried to get IR blasters to work. I vaguely remembered early attempts at trying to program the IR on my Palm 3 and Palm V. Needless to say, this went a lot better than expected.

Tip 1:

After reading the instructions first (rare occasion) , being a bit more careful. I discovered you need to change the mode of the Remotec first before joining the Z-wave bridge, in my case Fibaro. This is important as you cannot change the mode once connected. If you forget to do so, you need to exclude the device and do the setup again.


Tip 2:

The temperature is wrong.  Well, there is an offset that can be configured on the z-wave bridge. There is a parameter to allow for the offset. The short of it is that the thermostat is slow and inaccurate, but you can muck around with it until you get fairly average temp readings over a long averaged period which isn't that bad.

Tip 3:

I use a Daikin FT25DVM. The Remotec site have a serious number of codes for Daikin and I must have spent at least 2 hours trying codes in a tikam tikam approach. Noted not the best, hence I never got the code until I did a google and find a forum response of yet another pettson complaining about the Remotec and Daikin, But the silver lining was that he gave the code. Much thanks there.

It's Code 6 !!

If I did start from 1 and went on to the 2nd number instead  of jumping around, it might have been faster. I was also misled by the Fibaro HCL to say a soft reset is needed before the code is implemented, which was not true. You can just set the numbers in and save and it is immediately usable in the controls.

Actual Use

So I can now turn on my air-con from my phone in any room without any knowledge if it's actually on or not from a thermostat that might tell me an hour or so later. Is there any good stuff out of this? Now I can add the air-con to scenes for Alexa. So "Alexa, turn on sleep", it actually can turn on the AC. Just in time for hot weather when the Fan just isn't enough.


Saturday, April 15, 2017

Motion Lights using Philips Hue with Z-Wave Sensors


To work around the limitations of Fibaro HCL that does not allow you to use LUA scripts, Domotics fill that gap and allows you to use more than just Z-wave devices.

The major problem with using Hue with the HCL, is that the Hue API requires action via a HTTP PUT to change the state of the lights. The HCL only allows HTTP GET using the string method for Virtual Devices.

Domoticz allows you to add Hue directly as "Hardware" and automatically adds all the  bulbs and scenes as "Devices". To trigger the API to Domoticz, we use HTTP GET (yes, that is a Yay!!!).

Create a 2 button virtual device with the following string using the IP address and port of the Domoticz server

On button:

GET /json.htm?type=command&param=switchlight&idx=&switchcmd=On&level=0&passcode= HTTP/1.10x0D0x0A0x0D0x0A

Off  button:

GET /json.htm?type=command&param=switchlight&idx=&switchcmd=Off&level=0&passcode= HTTP/1.10x0D0x0A0x0D0x0A

When we create the new scene with the motion sensor being "breached", we can then select the On to link to the new Virtual Device.

Now for the vanity part where we can use Domotics FibaroLink to update the status of the virtual device label in Fibaro. This is best described in this link : http://www.vesternet.com/resources/application-notes/apnt-87#.WPHGlIh96Cg


.





Monday, April 03, 2017

Alexa, Turn on Computer

There is many ways to wake your computer up and put your computer to hibernate using Alexa.

I will use:

1. computer with cygwin enabled with sshd service
2. domoticz on a raspberry pi
3. ha-bridge on raspberry pi
4. Amazon echo

I'm more of a computer user than a developer, but I find that Domoticz is great as it helps non-developers download unix commands and allow a direct call to shell scripts.

More great things about Domoticz is that it convert HTTP POST to GET methods when you are using triggers that can't do POST to Philips Hue lights (I will cover that in another post)


Step 1: Setup a dummy hardware used for virtual switches



Step 2: Create a virtual sensor as a light switch called Computer

For the On script use:
script:///home/pi/domoticz/scripts/pc-wake.sh

The bash script should contain

#!/bin/bash
wakeonlan  BC:5F:F4:88:92:B6


For the Off script use:
script:///home/pi/domoticz/scripts/pc-hibernate.sh

This bash script contents are; (Replace X with the IP address of the computer)

#!/bin/bash
ssh user@192.168.1.X "shutdown /h" &


The off switch assumes you have an sshd service running on your computer with private keys installed on the raspberry pi with no password so it auto autenticates and runs without entering a password. Windows 10 command to hibernate is shutdown /h

You will need to google again to find out how to setup sshd, as it's too long to put into this post.


For ha-bridge to use this, add the domoticz command to Computer (Replace NN with switch ID, you can get this from the devices tab under the "Setup" drop down.

On
http://192.168.1.Y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=On

Off
http://192.168.1.Y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=Off


To use

Alexa, turn on computer
Alexa, turn off computer






Sunday, April 02, 2017

Alexa Turn On / Off TV


I have a Samsung LED smart TV. The "smart" TV let's you turn off the TV, but it doesn't let you turn it on as the IP address is not available when it's off.

That sort of makes sense.

While it is possible to then download a command to turn off the TV. you will need to go through the TV settings to enable HDMI-CEC. This means that turning on the apple TV for example will in turn turn on the TV and set the channel to Apple TV.


What you need.

1. Domoticz
2. iSamsung command (https://www.domoticz.com/wiki/Samsung_TV)
3. atvremote command (i took this from homeassistant)


Use Domoticz to create a dummy device and add a virtual selector device.




There are 4 buttons I programmed and here are the corresponding commands.

if your TV has the IP address 192.168.1.x (Replace x with the actual IP address of the TV)

To turn the TV off
script:///usr/local/bin/iSamsungTV 192.168.1.x -KEY KEY_POWEROFF

To use Apple TV to turn on the TV to Apple TV channel
script:///srv/homeassistant/bin/atvremote -a menu

To change the channel another HDMI input
script:///usr/local/bin/iSamsungTV 192.168.1.x -KEY KEY_HDMI

To change the channel to COMPONENT input
script:///usr/local/bin/iSamsungTV 192.168.1.x -KEY KEY_COMPONENT1


Now that we have Domoticz working, we need to add the buttons to ha-bridge for Alexa commands

For Domotics IP 192.168.1.y (Replace y) and switch ID is NN (Replace NN with the actual switch ID

To turn on the TV
http://192.168.1.y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=Set%20Level&level=10


to turn off the TV
http://192.168.1.y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=Set%20Level&level=0


To change the channel, we can create another ha-bridge device and just leave the turn on button for HDMI and Cable TV, respectively

http://192.168.1.y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=Set%20Level&level=20
http://192.168.1.y:8080/json.htm?type=command&param=switchlight&idx=NN&switchcmd=Set%20Level&level=30



To Use these command with Alexa, you can say this.

Alexa, Turn On TV (Defaults to Apple TV)
Alexa, Turn Off TV
Alexa, Turn On HDMI (does a round robin approach to HDMI surfing
Alexa, Turn On Cable TV (changes the channel to component which is my cable box)