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:



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 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   

4 comments:

  1. Hi Andrew,
    Thanks for the two posts in this series: they've been a big help!
    I am running my OH server on a device without bluetooth, and this script on a RPi with bluetooth.
    Do you know if it is possible to run a script remotely using the exec binding?
    Cheers!

    ReplyDelete
    Replies
    1. Glad you found it useful!

      I'm not sure if there is an easy way to get that done, the only thing that comes to mind is to run a script from your OH server that connects to your RPi with bluetooth and runs the script. Seems like a messy way to go about it.
      I've used UDP communication with python before, it was nice and simple, check out this example: https://wiki.python.org/moin/UdpCommunication

      But again I'd think the better way would be to just get a bluetooth dongle and use it with the OH server. There might even be a good reason to do it if you read this github issue: https://github.com/nkaminski/csrmesh/issues/17
      Seems like the dropout issues might be RPi bluetooth related

      Delete
    2. I've got it working using ssh.

      I can't seem to get the exec command item to show in sitemap as a switch. I've triple checked my things/items/sitemap but I only see my blinds as a string in sitemap, not a switch.

      Any further clues?

      Delete
    3. Hmm not sure, but take a look at my more recent post on using the exec binding with some examples

      https://andrew-klimovski.blogspot.com.au/2017/06/home-automation-openhab-exec2-binding.html

      Delete