[OH2] Tasmota flashed Sonoff Basic with DHT22 via MQTT


I have a number of Sonoff Basic R2 switches around the house, attached to desk lamps, floor standing lamps, or spliced into the wiring for the outdoor porch light. As I already had the ESP8266 inside the Sonoffs at various locations, why not try and add a temperature sensor to them?!

Note that whilst I used a DHT22, Tasmota supports a number of different sensors. Just ensure you choose the correct sensor within Tasmota’s Module parameters, and ensure that the name of the sensor is corrected in the openHAB Thing Channels (change AM2301 to whatever your sensor is).




  • MQTT broker - I use Mosquitto, setup as per its defaults
    • On my setup, Mosquitto and OpenHab are running on the same device, at
  • JSONPath Transformation installed in OpenHab
    • PaperUI -> Add-ons -> Transformations -> JSONPath Transformation -> Install
  • MQTT Binding (not MQTT Binding (1.x))
    • PaperUI -> Add-ons -> Bindings -> MQTT Binding -> Install


There are three parts to setup: the hardware, Tasmota and openHAB.

Hardware setup

Connecting the DHT22 sensors to the Sonoff Basic can be fiddly - I predominantly used the method described on this page, with the main differences being:

  • I used DuPont wires.
  • The DHT22 was pre-soldered to a board which already had its own resistor

Tasmota setup

Module Configuration

I connected the DHT22 data pin to the Sonoff GPIO3 pin (Serial In), and configured Tasmota to suit (http://<sonoff-ip-address>/md, or device home -> Configuration -> Configure Module). If you used a different sensor, or a different pin, make the required changes here.


Back on the device home page (http://sonoff-ip-address), check that the temperature and humidity are displayed (may take a little while to show real values)



http://<sonoff-ip-address>/mq, or device home -> Configuration -> Configure MQTT

OpenHab setup



I have a separate file which just contains the bridge Thing to my Mosquitto MQTT broker.

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" [


The device is defined in a things file called lights.things. If you used a different sensor then swap all mentions of AM2301 with the name of your sensor (that you selected in Tasmota’s Module parameters).

Thing mqtt:topic:swPorchLight "Switch Porch Light" (mqtt:broker:MosquittoMqttBroker) {
		Type switch : switch "Power Switch" [ 
		Type string : reachable "Reachable" [
			stateTopic = "tele/swPorchLight/LWT"
		Type number : temperature "Temperature" [ 
		Type number : humidity "Humidity" [ 


Within an Items file, add the following:

Switch sPorchLight "Porch Light"  { channel="mqtt:topic:swPorchLight:switch" }
String strPorchLightReachable "Porch Light" { channel="mqtt:topic:swPorchLight:reachable" }
Number nPorchLightTemperature "Porch Light Temperature" { channel="mqtt:topic:swPorchLight:temperature" }
Number nPorchLightHumidity "Porch Light Humidity" { channel="mqtt:topic:swPorchLight:humidity" }

Sitemap file

Within the sitemap file, add the following

Text item=nPorchLightTemperature label="Porch temperature [%.1f °C]" icon="temperature"
Text item=nPorchLightHumidity label="Porch humidity [%.1f %%]" icon="humidity"

Advanced Sitemap file

The lights.things file above defined a string which stores whether the Sonoff device is reachable or not (online/offline). This uses the MQTT broker’s Last-Will-and-Testament (LWT) message. The Sitemap snippet below uses this information to display a warning when the device is offline.

Text item=nPorchLightTemperature label="Porch temp [%.1f °C]" icon="temperature" visibility=[strPorchLightReachable=="Online"]
Text item=nPorchLightHumidity label="Porch humidity [%.1f %%]" icon="humidity" visibility=[strPorchLightReachable=="Online"]
Text item=strPorchLightReachable label="Porch sensor [%s]" icon="error" visibility=[strPorchLightReachable=="Offline"]

Edit 03/09/2020: Updated to use separate bridge.things, and some formatting changes.
Edit 18/05/2020: Updated to use default Tasmota MQTT settings for FullTopic, and added advanced Sitemap snippet.

1 Like

i also did that but not half has nice as you so codos
once i will move to me new home i will come back here and do it this way :slight_smile:

keep up the good work !