Automation based on MQTT topic [Solved]

Hi,

I have a SonoffSV flashed with Tasmota and connected to a Reed switch, when the Reed switch is closed it posts the Switch2 state as “ON” when the magnet is removed the Switch2 state goes to “OFF”

tas-1 tas-2

I wrote a simple automation that when Reed switch is closed it should send a text as per automation below:

- id: '1612856073957'
  alias: Garage
  description: 'Send text if garage door opens'
  trigger:
  - platform: mqtt
    topic: stat/tasmota_01/SWITCH2
    payload: 'ON'
  condition: []
  action:
  - service: notify.gmail_notification
    data:
      message: GMail:Garage Door Opended!
      title: HA
  mode: single

If I manually execute the automation from automation editor, I get the text so GMail notification service is set up correctly. But when I close the Reed switch, I can see with MQTT Explorer that Switch2 state changes to “ON” but the automation does not get triggered.

Please see if you can point me where I might missing something conceptually.
Thanks.

  trigger:
  - platform: mqtt
    topic: stat/tasmota_01/SWITCH2
    payload: '{"STATE":"ON"}'

The usual way to do it though is to create a binary sensor (so you can see the state in Lovelace as well):

binary_sensor:
  - platform: mqtt
    name: "Reed Switch"
    state_topic: "stat/tasmota_01/SWITCH2"
    payload_on: '{"STATE":"ON"}'
    payload_off: '{"STATE":"OFF"}'
    device_class: door  # or some other device class, see: https://www.home-assistant.io/integrations/binary_sensor/#device-class

You can then use a state trigger in your automation:

  trigger:
  - platform: state
    entity_id: binary_sensor.reed_switch
    to: 'on'

More options for the binary sensor here:

4 Likes

Thanks @tom_l for your input, I made some progress but not all the way.

Before that I checked from terminal command line if Mosquitto broker is actually getting updated.
Screenshot from 2021-02-09 18-50-43

Option-1: Successfully created a binary sensor for garage door by adding following code in the integration folder:

binary_sensor garage_sensor:
  - platform: mqtt
    name: "Garage Door Sensor"
    state_topic: "stat/tasmota_01/SWITCH2"
    payload_on: "ON"
    payload_off: "OFF"
    device_class: garage_door
    qos: 1

The binary sensor name binary_sensor.garage_door_sensor does shows up but in lovelace UI it is always shown as “unavailable”. And that might explain why the automation below did not work:

- id: '1612924834709'
  alias: Garage-02
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_door_sensor
    from: 'OFF' 
    to: 'ON'
  condition: []
  action:
  - service: notify.gmail_notification
    data:
      message: Great news, garage door just opened!
      title: Important!
  mode: single

Option-2: did not create the binary sensor and just used MQTT topic in automation but this one did not work either:

- id: '1612923798074'
  alias: Garage-01
  description: Texts when garage door opens
  trigger:
  - platform: mqtt
    topic: stat/tasmota_01/SWITCH2
    payload: '{"STATE":"ON"}'
  condition: []
  action:
  - service: notify.gmail_notification
    data:
      message: PM garage door just opened
      title: Important Notice!

This one did not work either, I’ll appreciate if anyone can point out the error or my mistake here.
Thanks.

Binary sensor states are always 'on' or 'off'. The lower case letters are important.

Though the fact that your sensor is always unavailable is also an issue.

Have you considered just setting option 19 in the device Tasmota web console and using discovery to set it up?

Changing to lower case ‘on’ and 'off" made no difference. Let me delete the integration and use SetOption19 to see if I get any better luck!
Thanks.

Yes because if this:

If you’re running tasmota 9.2 then you can enable SetOption114. This decouples switches from relays and makes device triggers for home assistant automations. You will need to make sure you have switchmode 1 enabled (or any on/off switchmode varients)

Ok, so it turns out that Tasmota device did have “SetOption19 On” when I turned this option off the Mosquitto broker stopped receiving updates from the device. The Tasmota Integration loads without any errors but not creating any sensors for detected devices so the problem may not be with the code above but with the Tasmota setup.

So I managed to mess up Tasmota to the extent that it will not do anything…probably time for a beer :grinning:

I’ll start from scratch tomorrow and share my updates!
Thanks.

1 Like

So here is the latest update, I flashed the Tasmota one more time and reverted to my older snapshot of HA to start fresh and followed these steps:

  1. On Tasmota “SetOption19 0” to allow native discovery
  2. Using MQTT Explorer, I could see Tasmota topic created (there were many including one that seemed to have all the configuration of Tasmota in a JSON object)
    tas-01
  3. Added Tasmota integration on HA, and added following code:
binary_sensor:
  - platform: mqtt
    name: "Reed Switch"
    state_topic: "cmnd/garagestate/POWER2"
    payload_on: '{"STATE":"ON"}'
    payload_off: '{"STATE":"OFF"}'
    device_class: garage_door
  1. From HA terminal, I am able to see MQTT topic specific to Tasmota and also see its status update as move the reed switch magnets.
    mqtt
  2. Restarted HA, the binary_sensor.reed_switch still shows up as unavailable

I will appreciate any pointers on troubleshooting this one.
Thanks,

Look at the payloads in the mqtt messages you have captured. Are they this?

'{"STATE":"ON"}'
'{"STATE":"OFF"}'

No they are not. Now that you have changed the Tasmota configuration they are ON and OFF. So this is what your payloads have to be set to in your binary sensor. Fortunately for you this is exactly what the default states are:

Screenshot_2021-02-12 MQTT Binary Sensor

So all you need is this:

binary_sensor:
  - platform: mqtt
    name: "Reed Switch"
    state_topic: "cmnd/garagestate/POWER2"
    device_class: door 

No, I do not see the JSON object in MQTT. In fact shows up as just on/off that too in color bands (see picture below).

Is there a way to change it from Tasmota device?

You don’t need to. Adjust your binary sensor instead. To this:

binary_sensor:
  - platform: mqtt
    name: "Reed Switch"
    state_topic: "cmnd/garagestate/POWER2"
    device_class: door 

@tom_l thanks for all your help and finally got to the bottom of it.

The problem was not in HA but at the Tasmota end and I was making a basic mistake in the UI. I manually changed the prefix to “Garage Door” but it seems it messed up MQTT topics queue. As soon as I changed to default values the code above with some modification (for MQTT topic path) started working.

A really good start to the weekend!!
Thanks again.