SONOFF TH10/16 integration tests

Notes on OpenHAB SONOFF devices integration

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. An installation tutorial is available on Youtube.

To install the MQTT broker (Mosquitto) on the CM3-Home board 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:

Once installed the MQTT binding on OpenHAB, 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. 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
    }
}

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>

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

Links

Sergio Tanzilli (Autore - Progettista hw & sw - Imprenditore - Musicista dilettante, classe 1964)
Fondatore di Area SX srl, Acme Systems srl e del TanzoLab, si occupa dal 1974 di elettronica digitale, dal 1978 di microprocessori, dal 1992 di microcontroller, dal 1995 di Linux e pubblicazioni su Web, dal 2005 di progettazione, produzione e commercializzazione di schede Linux Embedded.
http://www.tanzilli.com - http://www.acmesystems.it - https://github.com/tanzilli - sergio@tanzilli.com
Guido Ottaviani (Author/Designer/Developer for electronics-firmware-robotics)
He deals with electronic design and embedded system firmware for Robotics and industrial and civil applications, as well as technical disclosure on the internet.
http://www.guiott.com - https://github.com/guiott - guido@guiott.com