Markdown source

#SONOFF TH10/16 integration tests

<abstract>
Notes on OpenHAB SONOFF devices integration
</abstract>

To interface a Sonoff device with applications other than the original app, requires flashing a new firmware on the board and an MQTT broker. The firmware, valid for all the Sonoff family is available on [Github](https://github.com/arendst/Sonoff-Tasmota). An installation tutorial is available on [Youtube](https://www.youtube.com/watch?v=n4MDRm2yAJg).

To install the MQTT broker (Mosquitto) on the [CM3-Home board](https://www.acmesystems.it/CM3-HOME) it can be used the Openhabian configuration menu:

	sudo openhabian-config

Setup the client for connection to CM3-Home MQTT broker using the web configuration page of Sonoff-Tasmota:

<img src="./sonoff_mqtt_config" class="img-responsive center-block" width="30%"/>

Once installed the [MQTT binding on OpenHAB](https://docs.openhab.org/addons/bindings/mqtt1/readme.html), it has to be configured:

### File /etc/openhab2/service/mqtt.cgf

	# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
	localbroker.url=tcp://localhost:1883
	# Optional. User id to authenticate with the broker.
	localbroker.user=openhabian
	# Optional. Password to authenticate with the broker.
	localbroker.pwd=openhabian
	
To use the switch with Basic or Classic UI configure sitemap:

### sonoff.sitemap

	sitemap sonoff label="Sonoff" {
	    Frame label="Sonoff" {
	        Switch item=LivingRoom_Light
	    }
	}

### sonoff.items

	Switch LivingRoom_Light "Interruttore WiFi Sonoff" <light> (LR,gLight)
	    { mqtt=">[localbroker:cmnd/DVES_41E25A/POWER:command:*:default],<[localbroker:stat/DVES_41E25A/POWER:state:default]" }  
	    
Using either out (>) and in (<) configuration, enables the reading of the Sonoff device status, in order to update the OpenHAB panel icon even if the power is driven by the device physical button.

The Sonoff TH10/16 device is available also with [temperature and humidity sensors](https://www.itead.cc/sonoff-th.html). To read the enviromental data requires to more items:
	
	Number Sonoff1Temp "Temp. zona Nord (Soggiorno)[%.1f °C]" <temperature> (sonoff) { mqtt="<[localbroker:tele/TH10_sensor/SENSOR:state:JSONPATH($.SI7021.Temperature)]" }
	Number Sonoff1Hum "Umidità zona Nord [%.1f %%]" <humidity> (sonoff) { mqtt="<[localbroker:tele/TH10_sensor/SENSOR:state:JSONPATH($.SI7021.Humidity)]" }
	
to be used on sitemap:

	Frame label="Meteo Locale"
	{
    	Text label="Interno" icon=presence
		{
			Text 	item=TempIntValue valuecolor=[>25="red",>20="green",<=20="blue"]
			Text 	item=Sonoff1Temp valuecolor=[>25="red",>20="green",<=20="blue"]
			Text    item=Netatmo_Indoor_Temperature valuecolor=[>25="red",>20="green",<=20="blue"]
			Text    item=Netatmo_Indoor_Humidex  valuecolor=[<20.1="green",<29.1="blue",<28.1="yellow",<45.1="orange",<54.1="red",>54.1="maroon"]
			Text 	item=Sonoff1Hum valuecolor=[>60="blue",>40="green",<=40="orange"]
			Text    item=Netatmo_Indoor_Humidity valuecolor=[>60="blue",>40="green",<=40="orange"]
			Text    item=Netatmo_Indoor_Dewpoint
			Text    item=Netatmo_Indoor_Co2  valuecolor=[<800="green",<1000="orange",<1400="red",>1399="maroon"]
			Text    item=Netatmo_Indoor_Pressure
			Text    item=Netatmo_Indoor_Noise
			Text    item=Netatmo_Indoor_WifiStatus
			Text    item=Netatmo_Indoor_TimeStamp
			Text    item=Netatmo_Indoor_Location
    	}
    }
    
<img src="./ClassicUI.png" class="img-responsive center-block" width="30%"/>

or in an Habpanel

	<table id="weather-table">
	<tr>
		<td><img id="weather-icon" src="../static/weather-data/images/{{IconSet}}/{{statesnames[itemValue('Condition_ID')]}}.png" align="left"></td>
		<td id="weather-temp">{{'%.1f' | sprintf:itemValue('Netatmo_Outdoor_Temperature')}}</td>
		<td id="weather-temp-sign">°C <BR> ext</td>
    	<td id="weather-temp">{{'%.1f' | sprintf:itemValue('TempIntValue')}}</td>
		<td id="weather-temp-sign">°C <BR> int Nord L</td>
    	<td id="weather-temp">{{'%.1f' | sprintf:itemValue('Sonoff1Temp')}}</td>
		<td id="weather-temp-sign">°C <BR> int Nord S</td>
    	<td id="weather-temp">{{'%.1f' | sprintf:itemValue('Netatmo_Indoor_Temperature')}}</td>
		<td id="weather-temp-sign">°C <BR> int Sud</td>
	</tr>
   
	<tr>
   		<td></td>
    	<td id="weather-table-details1">Umidità:&nbsp </td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Outdoor_Humidity')}} %</td>
   		<td id="weather-table-details1"> </td>
		<td id="weather-table-details2"> </td>
    	<td id="weather-table-details1">Umidità:&nbsp </td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Sonoff1Hum')}} %</td>
    	<td id="weather-table-details1">Umidità:&nbsp </td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Indoor_Humidity')}} %</td>
	</tr>
	<tr>
   		<td></td>
    	<td id="weather-table-details1">Pressione:&nbsp</td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Indoor_Pressure')}} mBar</td>
   		<td id="weather-table-details1"> </td>
   		<td id="weather-table-details2"> </td>
   		<td id="weather-table-details1"> </td>
   		<td id="weather-table-details2"> </td>
    	<td id="weather-table-details1">CO2:&nbsp</td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Indoor_Co2')}} ppm</td>
	</tr>
	<tr>
   		<td></td>
  		<td id="weather-table-details1">Percepita:&nbsp</td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Outdoor_Humidex')}} °C</td>  
   		<td id="weather-table-details1"> </td>
   		<td id="weather-table-details2"> </td>
   		<td id="weather-table-details1"> </td>
   		<td id="weather-table-details2"> </td>
   		<td id="weather-table-details1">Percepita:&nbsp</td>
		<td id="weather-table-details2">{{'%.0f' | sprintf:itemValue('Netatmo_Indoor_Humidex')}} °C</td>
	</tr>

<img src="./Habpanel.png" class="img-responsive center-block" width="50%"/>


### The direct MQTT commands: 

	mosquitto_pub -h localhost --username openhabian --pw openhabian -t cmnd/DVES_41E25A/power -m 0

### Messages check

	mosquitto_sub --username openhabian --pw openhabian -t "#"

<img src="http://ewelink.coolkit.cc/wp-content/uploads/2016/09/JIEXIAN.jpg">

## Links

* [MQTT Binding](https://github.com/arendst/Sonoff-Tasmota/wiki/openHAB)
* [Tasmota OTA firmware repository](https://github.com/arendst/Sonoff-Tasmota)
* [Tutorial on Youtube](https://www.youtube.com/watch?v=n4MDRm2yAJg)
* [SONOFF TH16 info](http://ewelink.coolkit.cc/?p=149)
* [SONOFF devices info](http://ewelink.coolkit.cc/?p=67)
* [SONOFF without OpenHab: firmware modifications to use with RPI/MQTT/Nodered](http://www.instructables.com/id/Powerful-Standalone-Home-Automation-System-Pi-Sono/)

@include='bio_sergio_tanzilli'
@include='bio_guido_ottaviani'