Showing posts with label Items. Show all posts
Showing posts with label Items. Show all posts

Sunday, 16 April 2017

HOME AUTOMATION: Teptron Move control from OpenHAB

I've previously explored how to control the bluetooth version of Teptron Move from a linux PC (or Raspberry Pi) with the built in Bluetooth adapter. While it's interesting to be able to do this from the Linux commands line it's not particularly practical for use in a home automation system. In this post

I'll explain how to integrate those controls with openHAB for more practical use.


OpenHAB Binding Configuration

In order to run a linux script from within openHAB we need to first install the "Exec Binding".

Exec Binding Installation


There are a few examples on how to configure the exec binding but none of the examples worked for me. I ended up following this post to get the right combination of Things/Items/Sitemaps working to give the following result:


I'll go in to some more detail below on how I configured my Things/Items/Sitemap to get the units working.


There are a few peculiarities in working with the Bluetooth version of the Teptron Move with the command line functions. I've noticed the following inconsistencies:

  • Sometimes the device does not respond and the command reports an error
  • Other times the device responds and accepts the commands but does not move
  • The time between receiving the accepted response and the device moving varies from immediate to ~5 seconds

I've tried a number of workarounds for this but so far I've had the most success with simply just retrying the command multiple times, but this has the issue of pausing the unit motion when the second/third command is resent. I'm not ready to give up on this one just yet, but I'm fast running out of ideas.


Move Control Script

Create a new script in the in the openHAB scripts folder:
 sudo nano /etc/openhab/scripts/teptron_move_control.sh  

Copy the following lines in to the file and save the file:
 #!/bin/sh  
 RESPONSE=""  
   
 # execute the command multiple times  
 #for i in 1 2 3  
 for i in 1  
 do  
  RESPONSE="$(/home/openhabian/development/csrmesh/bin/csrmesh-cli move --pin 8888 --dest 43:c5:5b:04:00:06 --objid $1 --position $2)"  
  echo "Response: ${RESPONSE}"  
  sleep 1  
 done  
   

Now make the script executable with the command:
 chmod +x /etc/openhab/scripts/teptron_move_command.sh  

This script accepts two input arguments:
  1. The move unit we want to move
  2. The position to be moved to (0 to 255)
Test the command is working as expected, as an example use the command:
 /etc/openhab/scripts/teptron_move_command.sh 2 255  

This will command unit 2 to move to the fully opened position.

Things Configuration

Create a new .things file for the blinds:
 sudo nano /etc/openhab/things/blinds.things  

Add the following lines:
 Thing exec:command:Blinds_GF_Lounge_North "Blinds" [ command="bash /etc/openhab2/scripts/teptron_move_control.sh 1 %2$s", interval=0, autorun=true ]  
 Thing exec:command:Blinds_GF_Lounge_South "Blinds" [ command="bash /etc/openhab2/scripts/teptron_move_control.sh 2 %2$s", interval=0, autorun=true ]  

The important part of the command here is the  name of the Thing, in this case "Blinds_GF_Lounge_North" and "Blinds_GF_Lounge_South". These will need to match the name of the items we will create next.


Items Configuration

Next we must define items that will take the input from the sitemap and send it as a parameter to the exec binding and execute the script:

Create a new .items file:
 sudo nano /etc/openhab/items/blinds.items  

Add the following items:
 String Blinds_GF_Lounge_North_Command "Blinds" { channel="exec:command:Blinds_GF_Lounge_North:input", autoupdate="false" }  
 String Blinds_GF_Lounge_South_Command "Blinds" { channel="exec:command:Blinds_GF_Lounge_South:input", autoupdate="false" }  

Ensure the names of the defined items match the names of the things we defined previously.

Sitemap Configuration

We need to define a couple of switches with custom mapping to command the move units to the "Open" and "Close" positions:
 sudo nano /etc/openhab/sitemaps/default.sitemap  

Add the following lines where applicable:
 Switch item=Blinds_GF_Lounge_North_Command label="North Blinds" mappings=["255"="Open", "0"="Close"]  
 Switch item=Blinds_GF_Lounge_South_Command label="South Blinds" mappings=["255"="Open", "0"="Close"]  

If everything was working properly, pressing the buttons on the sitemap should command the move units to the open and closed positions.



Technically it should be possible to use a slider here to set the blind position, but I'm yet to work out how to do it.

Remember the following command is great to be able to debug any openHAB issues:
 tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log   

Monday, 13 March 2017

HOME AUTOMATION: MiLight/LimitlessLED/Easybulb OpenHAB Configuration

I've been thinking about wireless lighting control for a while now and tested out the space with the MiLight wireless controlled light bulbs.

My initial use case was focused around bulb dimming and while I realise I could have achieved this with a simple dimmer switch costing around 20 bucks from Bunnings, the cost of hiring an Electrician to come to wire a single switch would easily run be over $100. I know some people take to doing this themselves but you need to remember this is illegal in Australia.


The Research

The majority of my house is fitted with modern downlights that use the GU10 socket. This is a pretty common fitting type so the number of options is quite large. Here are a few of the options I considered:
  • MiLight/LimitlessLED/Easybulb - 15USD and around 10USD for a bridge
  • Phillips Hue - 4 wireless bulbs and a bridge at $399 or $89 per bulb
  • LiFX GU10 WiFi bulbs - 4 wireless downlights at $120 or $40 per bulb

As my main use case was brightness control I went for the MiLight bulbs that I bought from the LimitlessLED website.

While the cheapest, the MiLight bulbs have downside of no publishing their state when the lights are turned on.

This may not sound like a big deal, but after having used the bulbs in my home automation setup I can say that this limitation is quite annoying as there is no easy way to manage the state of the bulbs in your home automation setup. Unless you can see the lights left on you don't really know what state they are in, so the only useful feature when you are away is the blanket "All Off". It's not the worst but it's definitely not ideal.

The MiLight bulbs are not Wi-Fi, they use the a 2.4GHz wireless protocol that requires a Wi-Fi bridge to connect it to your home network. There are a few limitations with these Wi-Fi bridges and some folks have developed their own bridges that overcome these limitations, but I'm going to be sticking with the standard as the OpenHAB MiLight binding has been designed to work with this device.


Setting Up Your MiLight Bridge

Follow the MiLight instructions to set up your bridge to control the bulb groups in your house using the Android or iOS apps. Here are a few tips:
  • If you are using the Android app I've noticed it works much better on an old phone in airplane mode. The app experience on Android Nougat is horrible
  • Make sure you assign a static IP for your Wi-Fi bridge on your router
  • Once you have taught the bridge your light groups make a note so your don't forget which group was associated with each channel of the bridge
  • Each bridge can only control 4 groups of WW/CW and 4 groups of RGB/W bulbs
  • Each bridge has an "All Off" channel. Think about how many bridges you might want to make this function useful. In my setup I have 1 bridge per floor.


MiLight Wi-Fi Bridge with OpenHAB

I follow these steps to get my MiLight bulbs set up with my OpenHAB installation:
  • Navigate to the PaperUI page of your OpenHAB installation to configure you devices
PaperUI Configuration

  • Install the MiLight add-on 
MiLight Add-on

  • Navigate to the inbox and search for new devices
  • At this point you will see a number of devices listed under the MiLight services in the inbox 
    • 4 channels of WW/CW
    • 4 channels of RGB/W
    • 1 channel of WW/CW all off
    • 1 channel of RGB/W all off 
MiLight Bridge Channel
Now we need to add the devices be previously taught the Wi-Fi bridge in the setup step. This is done by clicking the check icon on the device we want to add, then follow the steps to add the device.

I followed the advice in the OpenHAB instructions and set the system to enable "Item Linking" to make the addition of Things easier.
Item Linking configuration


Once you have added the devices you plan to use the next thing to do is check they are working as expected via the configuration panel.

Testing Things in configuration panel


If your device's aren't working at this point you'll need to go back to the bridge setup step and make sure everything is working there. 


OpenHAB .items Configuration

With the devices now added as Things in your OpenHAB installation we need to add the bulbs as a .items file to get them running with our user interface.
Navigate to the items folder of the OpenHAB installation folder and create a new .items file:
 cd /etc/openhab2/items/  
 sudo nano lights.items  

Here we will add the devices we wish to use with our user interface. The MiLight binding gives some examples of how to configure the devices, but for quick reference this is how it's done.

  • Get the unique ID of the device you want to add from the Things banner of the Configuration panel. You need to click the Thing and view the available channels
Click the Thing you want to add to the .items file

Copy the channel you want to use

  • Add the ID to the item and give it a unique name. Here is an example:
 Switch Light_GF_Laundry_OnOff        {channel="milight:whiteLed:ACCF23508A5A:1:ledbrightness"}
 Switch Light_GF_Laundry_NightMode    {channel="milight:whiteLed:ACCF23508A5A:1:lednightmode"}
 Dimmer Light_GF_Laundry_Temperature  {channel="milight:whiteLed:ACCF23508A5A:1:ledtemperature"}
 Dimmer Light_GF_Laundry_Brightness   {channel="milight:whiteLed:ACCF23508A5A:1:ledbrightness"}

  • Repeat this process for all the devices you want to add, then move on to the sitemap
Note: the MiLight binding does not expose and on/off state for the bulbs, rather you need to define a switch for the brightness to control the on/off and a dimmer for the brightness adjustment both using the same channel



OpenHAB Sitemap Configuration

With the devices added to your items file now we can add them to your sitemap. This is the file that outlays the user interface for user interaction with the OpenHAB setup.

Navigate to the OpenHAB installation directory and then to the sitemap directory. If you are coming from a fresh installation you probably won't have anything in here other than a readme.txt file so you will also need to create a default.sitemap file.
 cd /etc/openhab2/sitemaps/  
 sudo nano default.sitemap  

We will start with a basic working sitemap to test the functions of the devices we just added.
 sitemap default label="Test Layout" {  
     Frame label="Laundry" {  
         Switch item=Light_GF_Laundry_OnOff label="On/Off"  
         Slider item=Light_GF_Laundry_Brightness label="Brightness"  
         Switch item=Light_GF_Laundry_NightMode label="Night Mode"  
     }  
 }  

And here is the output: