Expose devices with MQTT automatically? Similar to statestream?

Hi everyone,

maybe a simple question, but I have searched for hours now without a solution - maybe I just need someone pointing me in the right direction.

Setup: Hass.io on Raspi, with lots of devices / switches / sensors running smoothly. Set up the MQTT broker add-on to integrate a Tasmota device (TF1 3-channel touch light switch). Entered the broker’s address in Tasmota, connection is running, all is well - or is it?

Working: I can trigger the switch’s relays from HA using MQTT publish messages, no problem. Question is: How do I trigger actions in HA with MQTT messages sent from the Tasmota device? Namely, when I trigger one of the touch buttons. Possible answers:

  • Since the Tasmota button can trigger sending a MQTT message to the broker, the easiest way would be if the HA entity could be automatically / manually be exposed via MQTT (e.g. Send payload “ON” to something like homassistant/switch/myswitch/command
  • I read about the MQTT automation triggers - how do I set them up?
  • One way I have already figured out is to create a MQTT binary sensor in HA, and trigger its state from Tasmota to trigger an automation, but from all the solutions, that seems to be the most complicated

Any hints? Thanks!

All in the docs https://github.com/arendst/Sonoff-Tasmota/wiki/Home-Assistant

No, it’s not. Wouldn’t have asked otherwise, but thanks anyway for linking the address.

I figured out a few things by myself, particularly the last 2 methods I have sketched, so here’s a few details for people who want to do something similar. And that is followed by a question about the third way, which would be the most elegant IMHO.

Method 1: Using a binary sensor’s state in HA

Basically, one of the buttons on the Sonoff T1 is included into HA as a binary sensor. Based on that, you can easily set an automation triggered by a state change on that sensor. There is an automatic method using auto discovery, , as described in the article linked by @nickrout.

(Note that only when you set a “ButtonTopic”/“SwitchTopic” in Tasmota as described in https://github.com/arendst/Sonoff-Tasmota/issues/6530, auto discovery will discover both switches and buttons separately)

Manual method in HA:

binary_sensor:
  - platform: mqtt
    name: "wall_button_1"
    state_topic: "stat/sonoff/button3"
    qos: 0

Use the automatic discovery if possible, makes it easier to keep names and topics consistent. Trigger for the automation:

  trigger:
  - entity_id: binary_sensor.sonoff3_btn
    platform: state
# add the state change as you like
    to: 'on'

Method 2: Using a MQTT trigger directly in an automation

Using the binary sensor seems to me a bit sluggish in reaction compared to this way. Instead, use a rule in Tasmota to fire a MQTT message to the broker when the button is pressed. This is necessary anyway if you want to separate the button from the underlying relay, like I do.

rule1
on button2#state do Backlog publish stat/sonoff/button2 ON; Power2 TOGGLE endon
on button3#state do publish stat/sonoff/button3 ON endon

Both rules in the ruleset send a MQTT message to the broker. The first one for the middle button also toggles the relay. The other one only send the message.

In HA, use an automation trigger like this to start an action based on the MQTT messages above. I’m pretty sure that the messages can be named as you like:

  trigger:
  - payload: 'ON'
    platform: mqtt
    topic: stat/sonoff/button3

Method 3: How can HA expose devices to MQTT, to be triggered by Tasmota directly?

That question is still open. As seen in Method 2, Tasmota can directly publish MQTT messages. So the question is: Can I, and how can I expose HA devices to MQTT, so that they can be controlled by the Tasmota device via MQTT? That yould be the most effective way to communicate, I guess.

According to https://www.home-assistant.io/integrations/mqtt_statestream, you can expose parts of HA to the MQTT broker. Not a good idea to expose everything (system overload, very long start time!), so here’s what my HA configuration looks like. Can be broken down to single entities as well, but I chose all switches and lights:

mqtt_statestream:
  base_topic: homeassistant
  publish_attributes: false
  publish_timestamps: false
  include:
    domains:
      - switch
      - light

Problem is: The MQTT stream seems to be for reading states and attributes only. Sending a payload “on” to the topic homeassistant/switch/name-of-switch/state does nothing at all. I wish there’d be an automatic topic like homeassistant/switch/name-of-switch/cmnd where you’d be able to publish a “on”, “off” or “toggle” message to. So basically, I’d like HA to expose its devices to MQTT just like Tasmota does.

Any ideas?

1 Like