YAML openHAB 3 MQTT TASMOTA SONOFF

Getting Tasmota working with OH3 using MQTT

This is for someone that has installed openhabian and wants to use tasmota to control their device.

Prerequisite

SSH Access - I like and use https://termius.com/

MQTT Broker - I Mosquitto as the Broker for all of my MQTT devices. I install using the openhabian config tool.

Device - Tasmota installed on a device and be able to view webpage.

MQTT Client - http://mqtt-explorer.com/

SSH

If you haven’t done so installed mosquitto using ssh

  1. login to SSH
  2. sudo openhabian-config
  3. Optional Componets
  4. Mosquitto
  5. If you want password enter it in here. Mine is pleasework

openhabian-configuration-tool

Tasmota

For this example I will use a sonoff basic for a Light called My Light.

  1. Get device working with the webage so you can control it through TASMOTA
    Doccumentation
  2. Open Configuration - Config MQTT
  • In Host enter the IP address of your MQTT Broker (RPI)
  • Port 1883 and Client DVES_%06X can be left default
  • User if default and password is set openhabian
  • Pass pleasework
  1. Test Connection to MQTT works in console

MQTT Client

  1. Enter connection details

Using the client is will automaticy subscribe to every topic and show it to you. You Can also publish anything to any topic.

  1. Get the MQTT Client to turn the light ON

Publish ON to cmnd/MyLIGHT/POWER topic.

You can see that after publishing this you can see that TASMOTA sends state to stat/MyLIGHT/POWER

openHAB 3 Broker Creation

Only one broker bridge is needed for all of you’re MQTT Thing’s

  1. Install MQTT Binding

Settings / Addons / Bindings / Blue + / MQTT Binding

  1. Create Broker Bridge connection
  • Settings / Things / Blue + / MQTT Binding / MQTT Broker Link
  • Change the Unique ID at the top it can’t be changed later.
  • Enter other details select show advanced if you need to put a password in

You’re Broker should show online

openHAB 3 Generic MQTT Thing Creation

  1. Settings / Things / Blue + / MQTT Binding / Generic MQTT Thing Link

  2. Enter settings clicking Show advanced

  • Unique ID MYlight
  • Label MY Light
  • Click on Parent Bridge and Select
  • Avalibility Topic tele/MyLIGHT/LWT
  • Payload available Online
  • Payload not availabe Offline
  1. Create Thing

Add Channels to Generic MQTT Thing

  1. Select The My Light thing
  2. Channels Tab
  3. Add Channel
  4. Enter Settings
  • Channel Identifier POWER - Can be anything
  • Label MY Light
  • On/Off Switch
  • MQTT State Topic stat/MyLIGHT/POWER
  • MQTT Command Topic cmnd/MyLIGHT/POWER
  • Custom On/Open Value ON
  • Custom Off/Closed Value OFF

This is how you add channels to you thing you can go ahead and create all the channels you need this way or enter them in YAML code in the code tab.

channels:
  - id: POWER
    channelTypeUID: mqtt:switch
    label: Power Switch
    description: ""
    configuration:
      stateTopic: stat/MyLIGHT/POWER
      off: OFF
      on: ON
      commandTopic: cmnd/MyLIGHT/POWER

Add these channels below

  - id: Uptime
    channelTypeUID: mqtt:string
    label: UP time
    description: ""
    configuration:
      stateTopic: tele/MyLIGHT/STATE
      transformationPattern: JSONPATH:$.Uptime
  - id: LoadAvg
    channelTypeUID: mqtt:string
    label: LoadAvg
    description: null
    configuration:
      stateTopic: tele/MyLIGHT/STATE
      transformationPattern: JSONPATH:$.LoadAvg
  - id: WIFI_Signal
    channelTypeUID: mqtt:string
    label: WIFI Signal
    description: null
    configuration:
      stateTopic: tele/MyLIGHT/STATE
      transformationPattern: JSONPATH:$.Wifi.Signal

Install JSONPATH transofrmation

If you haven’t allready done so add a couple of locations to your Model.

Create items using Create Equipment from Thing

  1. Settings / Model / Select Location if desired /Create Equipment from Thing

  2. Select your thing MY Light

  • Simantic Class: Light Bulb
  1. Select ALL

  2. Add To Model

  3. Navigate to My Light Power Switch Point and Add Metadata

  • Change Default Standalone Widget to oh-toggle-card

This Should now show up in the equipment tab of your openhab Front Page under light bulb

Coustom Widget

Here is a custom widget you can add so you can set the default stanadalone widget to see more than just a toggle switch.

uid: Tasmota_SONOFF
tags: []
props:
  parameters:
    - description: A text prop
      label: Name
      name: prop1
      required: false
      type: TEXT
    - context: item
      description: An item to control
      label: Item
      name: item
      required: false
      type: TEXT
  parameterGroups: []
component: f7-card
config:
  outline: false
  noBorder: true
  padding: false
  noShadow: true
  style:
    --f7-safe-area-right: 0
    --f7-safe-area-left: 0
slots:
  content:
    - component: oh-button
      config:
        action: toggle
        actionItem: =(props.item + "_PowerSwitch")
        actionCommand: ON
        actionCommandAlt: OFF
        icon-f7: lightbulb_fill
        iconSize: 25
        iconColor: '=(items[props.item + "_PowerSwitch"].state === "OFF") ? "black" : "yellow"'
        
    - component: f7-card
      config:
        content: =items[props.item + "_PowerSwitch"].displayState || items[props.item + "_PowerSwitch"].state

    - component: f7-card
      config:
        title: Load Average
        content: =items[props.item + "_LoadAvg"].displayState || items[props.item + "_LoadAvg"].state
    - component: f7-card
      config:
        title: UP Time
        content: =items[props.item + "_UPtime"].displayState || items[props.item + "_UPtime"].state
    - component: f7-card
      config:
        title: WIFI Signal
        content: =items[props.item + "_WIFISignal"].displayState || items[props.item + "_WIFISignal"].state
         
8 Likes

Hey, thank you for the great tutorial! Really helped me getting started with my openhab 3 Setup :slight_smile:

Can you explain the part with the custom widget? Where can I change the default widget to your custom widget? Just replacing it in the yaml code section didnt work for me :smiley:

Yeah sure.

Create a custom widget in Developer tools widgets add new widget using the blue + in bottom right.
Select everything and delete.
Copy Widget from above
Save Widget top right.

Go back to model page and Select your equipment.
add metadata

Select default standalone widget.

Change the widget to your one

1 Like

Weird thing, when I add the lwt from my sonoff4ch, I can only switch to “on” and then I get a communication error and see in the log, that the topic apparently doesn’t exist? However I can see it with mqtt.fx

After removing the lwt from the thing, everything is working fine.

Which log is this? Can you show the log?

I already called it a day for now, but it was either /var/log/openhab/openhab.log or /var/log/openhab/events.log

I double checked my input and copied it from mqtt.fx…still not working.

I connected several esp easy and set the lwt there as well… And it works fine… 3-4 seconds after disconnecting them, the thing goes offline in oh.

I assume it’s a problem with my tasmota (it’s an ancient version ;))

I’m at a loss…

I want to add two lights that are connected to a Zonoff ZbBridge flashed with Tasmota and set up as module 75.

I can control power, dimmer and CT from the console in Tasmota like this:

ZbSend { "device":"Light_LivingRoomTable", "send":{"Power":"Off"} } 

When I send that command, I see the following on the mqtt broker:

stat/zigbee/ZBSEND {"ZbSend":"Done"}
tele/zigbee/ZBZCLRECEIVED {"ZbResponse":{"Device":"0xCB87","Name":"Light_LivingRoomTable","Command":"0006!00","Status":0,"StatusMessage":"SUCCESS","Endpoint":1,"LinkQuality":60}}
tele/zigbee/SENSOR {"ZbReceived":{"Light_LivingRoomTable":{"Device":"0xCB87","Name":"Light_LivingRoomTable","Power":0,"Endpoint":1,"LinkQuality":60}}}

How can I make my setup work with the first example in this topic?

Newly installed openHABian 3 with Mosquitto broker add-on, and I’ve managed to set up a temperature sensor iin openHAB, using the same broker.

(The same bridge and lights worked fine with Home Assistant (set up as ZHA ZbBridge, module 0), but I want to make it work with openHAB too!)

Found my answer in Switch on by publishing a command and a json string, I need to use:

UID: mqtt:topic:mosquitto:Light_LivingRoomTable
label: Living Room Table Light
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:mosquitto
location: Living Room
channels:
  - id: POWER
    channelTypeUID: mqtt:switch
    label: Power Switch
    description: null
    configuration:
      commandTopic: cmnd/ZbBridge/ZbSend
      formatBeforePublish: '{"device":"Light_LivingRoomTable","send":{"Power":"%s"}}'
      stateTopic: stat/ZbBridge/RESULT
      off: Off
      on: On
2 Likes