Friday, August 24, 2018

Multiple Broadlink RM mini 3 integration using MQTT

Broadlink now has quite a lot of integration options almost out of the box. If you enable Broadlink IHC, you can directly link it to Alexa by giving the device a unique name.

There is a homebridge plug in for homekit integration but I haven't tried or tested this. https://lprhodes.github.io/slate/

I wanted to put the device in domoticz so I can have more control over what can trigger my broadlink. I decided to use broadlink-mqtt for this, which is a fairly easy method to trigger from any source.

Setup Instructions for broadlink-mqtt

1. git clone https://github.com/eschava/broadlink-mqtt

2. vi /home/pi/broadlink-mqtt/mqtt.conf

3. Update the mqtt.conf file with the device type set as multiple_lookup

device_type = 'multiple_lookup'
mqtt_multiple_subprefix_format = '{type}_{mac_nic}/'

4. Start the python script and check that it started and detected all the RM devices.

Check the log file to see what the IP / MAC addresses are.
DEBUG Connected to RM2 Broadlink device at '192.168.1.101' (MAC 34:ea:34:01:01:01)
DEBUG Connected to RM2 Broadlink device at '192.168.1.102' (MAC 34:ea:34:00:00:00)


5. Record the IR command - substitute the 00_00_00 with the last 3 octets of the mac address for the device. (I haven't tried this yet, as I previously added my commands when I only had 1 RM mini)

mosquitto_pub -h 192.168.1.10 -t broadlink/RM2_00_00_00/aircon/off -m auto


6. Configure Domoticz virtual device to replay the IR command

If the MQTT IP address is 192.168.1.10, the following "On Action" can be used to turn on and off the broadlink virtual switch device created in Domoticz

script:///usr/bin/mosquitto_pub -h 192.168.1.10 -t broadlink/RM2_00_00_00/aircon/on -m auto
script:///usr/bin/mosquitto_pub -h 192.168.1.10 -t broadlink/RM2_00_00_00/aircon/off -m auto


Update to avoid python 2.7 errors, use python3.

$ sudo pip3 install paho-mqtt

and start by

$ python3 /home/pi/broadlink-mqtt/mqtt.py




Thursday, May 17, 2018

Installing Raspbian Stretch on a new Pi

My first pi is fully used and has hit production mode where it runs the house and I can't use it as a play toy anymore. It's running Jessie, which is super stable.

Guess to move on, I've got a 2nd pi which I've decided to put raspbian stretch and here are some notes of the install because it never really goes as planned.

After downloading and running raspi-config to region, etc. apt upgrade failed with hash sum mismatch using the default apt sources. I tried a few and what worked for me was:

deb http://mirror.rise.ph/raspbian/raspbian/ stretch main contrib non-free rpi

after apt-get upgrade worked, I went on to install java. 
I had some issues as well with  "Error: missing `server' JVM at `/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm/server/libjvm.so'." which was fixed with

sudo apt-get purge openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre-headless

sudo apt-get install openjdk-8-jre

Ok, moving on to some software, i prefer a manual installations since I can squeeze more stuff into the pi.

As I mostly use Domoticz, I've decided to muck around with home assistant this time and the installation went smoothly enough.
HomeAssistant - https://www.home-assistant.io/docs/installation/raspberry-pi/

Next on the list;

  • BLE Beacon presence as detection has a really short range and this can be used for specific room detection.
  • Other stuff that didn't fit on the first pi was syslogd and ELK for log analysis.
  • ffmpeg for converting video streams






Tuesday, May 15, 2018

Displaying a common device status

Once everything is merged, the challenge will be to make sure all devices are reported to the correct status. There are 2 main stores of device state in my setup;

  • Domoticz
  • Fibaro
Basically we want Domoticz to know the state of the Fibaro devices and also Fibaro to be able to know the state of the domoticz devices.

There shouldn't be 2 different master sources of data. As Domoticz seems to be the master controller and other devices, e.g. Philips hue is capable of updating the status in domoticz automatically and homebridge uses the domoticz plugin, I'm going with Domoticz as the master status and everything will just query domoticz for the state.

To do this, there need to be a virtual device for each appliance that will be have it's state reported. Domotics support all sorts of virtual device, the more common ones we will use is 
  • Switches
    • Sockets (On/Off)
    • Motion Sensor
    • Contact Sensors
    • Lamps/Dimmer
  • Temperature
The full list is located here. https://github.com/PatchworkBoy/homebridge-edomoticz. Most devices will use the standard sockets device, i.e. On or Off. 

The "Ping test"  hardware devices that can be added to domoticz if you wish to check the status of the NAS or the TV or other ping-able device. Note that this does not apply to mobiles or iPhones which need a separate script to update the device status.

HomeBridge Updates

Add the device to the Homekit room and the device will automatically appear in Apple Home. I did not add a ping test device to homebridge directly, but add the actual virtual control device to homebridge and on the backend, use the ping test device to update the actual device.

For devices that I think needed to be on all the time, I can add notifications to when the ping goes down, e.g. if your NAS was turned off instead rather than have an extra icon in Apple Home.


Fibaro Updates

Once the devices are added to Domoticz, in Domoticz go to;

Setup > More Options > Data Push > Fibarolink
https://www.domoticz.com/wiki/FibaroLink

This will allow you to send a device state to a Fibaro label. It does not help update the global variable. You will need to create a Fibaro virtual device with a bunch of labels and name it correctly. These labels will be updated with the status of the Domoticz virtual device, e.g. On/Off.

This is probably the easiest way to be able back reporting if you prefer to use the Fibaro app which is much prettier than the domoticz app at this time. If you also like control buttons in Fibaro app, refer to the post on Fibaro virtual devices controlling Domoticz using HTTP commands.

Note, it's also possible to update Fibaro global variables where needed, this can't be done via FibaroLink, but should be easy enough to put into the On/Off action of the virtual device to call a script:/// to use the Fibaro API to update accordingly. I've not done this yet as so far, I haven't found a need for it.


Monday, April 23, 2018

Fibaro HCL Virtual Device Slider

How to setup Fibaro home center lite (HCL) slider for virtual devices.

As the Fibaro HCL does not support LUA. The question was how to update the number value of the slider to send to the HTTP string. Thanks to this site which is a really good reference https://www.vesternet.com/resources/application-notes/apnt-88/

The 2 use cases here are;

  • Sonos HTTP API Volume


To allow for volume control for all Sonos devices, add a virtual device with the IP address for SONOS HTTP API and specify the default port to 5005. Create Slider and put the following text into the string.

GET /volume/_sliderValue_ HTTP/1.10x0D0x0A0x0D0x0A


  • Domotiz Virtual Devices


GET /json.htm?type=command&param=switchlight&idx=XX&switchcmd=Set%20Level&level=_sliderValue_ HTTP/1.10x0D0x0A0x0D0x0A

Replace XX with the device ID.

Friday, April 13, 2018

HomeKit Outdoor Weather and Indoor Temperature

This post adds both outdoor temperature and indoor temperature to Apple Homekit.

Outdoor Weather / Temperature

Using homebridge-weather https://www.npmjs.com/package/homebridge-weather

Add the following weather accessory to the homebridge config.json

    "accessories": [
    {
      "accessory": "Weather",
      "apikey": "enter_your_own_key_here",
      "locationByCoordinates": "lat=your_lattitudelon=your_longitude",
      "name": "Weather"
    }
    ]


Indoor Temperature

I'm taking indoor temperature from the Fibaro HCL temperature sensor, i.e. Aeotec multi-sensor.

Option1: cron/bash file to extract temperature to load into Domoticz temperature device

In the earlier post, I created a batch file to extract the temperature via REST API from Fibaro HCL to upload to a domo device. Not the most graceful way to do it, but it old school and works.


Option 2: Create a MQTT temperature feed using node-red

Another way, I just setup is using Node-red to keep pulling the temperature from the sensor and publishing it to domoticz/in topic to update the domoticz vitual device.

This is installed using https://www.npmjs.com/package/homebridge-mqtt-temperature This requires you to run mosquitto mqtt and node-red locally.

There's probably a few more things you can actually do with MQTT, so I've decided to move to MQTT instead and reduce the number of cron jobs.



  • The timestamp that triggers the flow can be set to repeat on interval of 10 mins.


  • The HTTP input "Room Temp" is the device URL from Fibaro. 

E.g. http://user:password@192.168.1.XX:80/api/panels/temperature?type=devices&method=single&id=XX

Replace the username, password, IP address and device ID.

  • Split is an easy way to remove the square brackets [] from the string output.


  • Fibaro to Temp function to re-write the JSON for Domoticz

The customised function will extract the TemperatureH value from the return JSON string and insert the domotics JSON format into domoticz/in topic for domoticz to update the virtual temperature device.

E.g.

var temperature =  msg.payload.temperatureH;
msg2 = {};
msg2.payload = '{ "idx" : XX, "nvalue" : 0, "svalue" : "'+temperature+'"}';
return msg2;

Again replace the XX with the Domoticz device ID.



Thursday, April 12, 2018

Automating Samsung MU6100 Series K

Finally upgraded to a new Samsung 4K TV and iSamsungTV stopped working. The good thing is that so have most people and there is a new app, samsungctl https://github.com/Ape/samsungctl which  allows command line interface to the TV using websocket, because everyone needs a to open a terminal window to change a channel.

I used this link to install samsungctl https://www.domoticz.com/wiki/Plugins/SamsungTV.html

Basically I needed to

  • sudo pip3 install samsungctl
  • sudo pip3 install websocket-client


After the installation, I had some error messages, i.e. "AttributeError: 'numpy.ndarray' object has no attribute 'tobytes'", which was solved by;
  • sudo pip3 install numpy --upgrade

The new TV can be turned on with WakeOnLAN now. This makes my Broadlink RM3 fairly redundant as the older Samsung wasn't able to turn on hence, I needed the IR blaster before.

Control Sonos outside your home using HomeKit

Applying the same concept as the Aeotec Dimmer for Homekit control, we can also use the Domoticz virtual dimmer to control the Sonos using node-sonos-http-api.

Homekit > Domoticz Virtual Dimmer (LUA) > node-sonos-http-api > Sonos

I'm starting to derive a shorter version of the LUA script for easier maintenance and using the base On and Off action commands on Virtual Device configuration page. Replace the IP address with the raspberry pi running node-sonos-htttp-api.

Virtual Device Name SonosOne_Volume

Action On Command: http://192.168.1.XX:5005/Sonos%20One/play
Action Off Command: http://192.168.1.XX:5005/Sonos%20One/pause


LUA script

/home/pi/domoticz/scripts/lua/script_device_SonosOne_Volume.lua


commandArray = {}

DEV='SonosOne_Volume'

if otherdevices[DEV] == 'Off' then
        os.execute('curl "http://192.168.1.XX:5005/Sonos%20One/pause"')
else
        Level = otherdevices_svalues[DEV]
        os.execute('curl "http://192.168.1.XX:5005/Sonos%20One/volume/"'..Level)
end

return commandArray


Wednesday, April 11, 2018

Extract temperature from Fibaro HCL to Homekit Room

For Apple mobile users who want to know the temperature of a room in Apple home using a temperature sensor from Fibaro HCL

Automation Flow:

Aeotec light/temp sensor > Fibaro HCL > bash script >; Domoticz virtual device > homebridge/homekit


Setup Steps

Step 1. Create a temperature virtual device in Domoticz and take note of the device id.

Step 2. Create a bash file with the following script, replacing the XX and user:password fields.


#!/bin/bash
status=/home/pi/scripts/temperature
domo_idx=XX
fib_idx=XX

curl "http://user:password@XX.XX.XX.XX/api/panels/temperature?type=devices&method=single&id="$fib_idx > $status
temperature=`cat $status |  sed 's/[{}]//g' |  awk -F : '{print $3}' | awk -F , '{print $1}'`

curl "http://XX.XX.XX.XX:8080/json.htm?type=command&param=udevice&idx="$domo_idx"&nvalue=0&svalue="$temperature


Step 3. Create a cron entry to run this script every 5 or 10 mins (temperature doesn't change that quickly)


Home Automation High Level Components

Maybe to put more context into the previous post of a slow build out of home automation components, here's a big picture integration diagram on how everything works together to date.




User Interaction

  • Mobile Apps
  • Voice Commands
  • Cloud Services


Integration Components
  • Home Automation Bridges
  • Domoticz
  • MQTT
  • Scripts (Python, Bash, LUA, etc.)

Connectivity Methods
  • Z-Wave (Fibaro)
  • Zigabee (Philips Hue)
  • Sonos wireless mesh
  • Wifi and LAN
  • Bluetooth Low Energy (BLE)
  • Infra-red

Automation Devices
  • Lights, Smart Dimmers
  • Smart Power plugs
  • Temperature, humidity and light sensors
  • Door sensors
  • Cameras
  • Network speaker system
  • Smart TV
  • Air-Conditioners / Thermostats


Use Homekit to control Fibaro HCL Z-Wave Dimmer



Home Automation Flow

Siri > Homebridge > Domoticz (LUA) > Fibaro HCL > Aeotec Nano Dimmer

Assume that Homebridge, Domoticz and Fibaro is already setup. The homebridge config.json file should have a room reference to avoid Homekit showing all devices

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:21:3E:E4:DE:33",
        "port": 51826,
        "pin": "031-45-154"
    },
    "description": "Configuration file for (e)xtended Domoticz platform.",
    "platforms": [
        {
            "platform": "eDomoticz",
            "name": "eDomoticz",
            "server": "127.0.0.1",
            "port": "8080",
            "ssl": 0,
            "roomid": 2,
            "mqtt": 1
        }
    ],
    "accessories": [
    ]
}



Step 1. Create a new domoticz virtual device example name "My_Dimmer"

Step 2. Add the virtual device to the Homebridge assigned Room "roomid" that is assigned for devices that Homekit will control.

Step 3: Create a new lua script file with the device name in this folder

/home/pi/domoticz/scripts/lua

The script name is important. For the device with the name "My_Dimmer". create the script "script_device_My_Dimmer.lua"

The whole path is as such.

/home/pi/domoticz/scripts/lua/script_device_My_Dimmer.lua

Insert the following to the script file, replacing user:password and device IDs. Script as follows:


commandArray = {}

DEV='My_Dimmer'

if otherdevices[DEV] == 'Off' then
        os.execute('curl "http://:@:80/api/callAction?deviceID=&name=turnOff"')
else
        Level = otherdevices_svalues[DEV]
=        os.execute('curl "http://user:password@:80/api/callAction?deviceID=&name=setValue&arg1="'..Level)
end

return commandArray



Important Note: name your dimmer My_Dimmer so that the name is consistent in the device and in the script for it to work.


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.

https://motion-project.github.io/


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.



Splitting the home network

Who wouldn't want to separate the traffic at home for security reasons. The more common ones include wireless guest and wireless users. ...