Mqtt binary sensor front end fail

I am setting up an alarm system in which an active infrared beam sensor will set off an alarm and notification. The infrared receiver is wired to a Wemos D1 mini flashed with Tasmota and working with Mosquitto mqtt addon. All of this is working fine. The sensors are operating as expected. Tasmota console shows payload on and off as I pass my hand over the beam. There are no errors in the HA logs. The yaml validates. BUT the entity card on the frontend does not effect the change. Here is my config:


binary_sensor:
- platform: mqtt
name: "/Courtyard Beam Break"
state_topic: "stat/courtyard-motion/POWER"
payload_on: "ON"
payload_off: "OFF"
qos: 0
device_class: motion

Mqtt:

19:09:04 MQT: tele/courtyard-motion/STATE = {"Time":"2021-02-25T19:09:04","Uptime":"0T01:40:12","UptimeSec":6012,"Heap":25,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":28,"MqttCount":1,"POWER":"OFF","Wifi":{"AP":1,"SSId":"BELL290","BSSId":"18:90:D8:C5:D2:BE","Channel":6,"RSSI":42,"Signal":-79,"LinkCount":1,"Downtime":"0T00:00:06"}}
19:09:04 MQT: tele/courtyard-motion/SENSOR = {"Time":"2021-02-25T19:09:04","Switch1":"OFF"}

I did not use mqtt discovery because I didn’t want the sensor to show as a switch. I have triple-checked the wiring etc. Don’t know where else to look.

I assume your pasted config does not actually look like that or you would be getting configuration errors from the incorrect indentation. It should be:

binary_sensor:
  - platform: mqtt
    name: "/Courtyard Beam Break"
    state_topic: "stat/courtyard-motion/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 0
    device_class: motion

You have not shown what is received on the topic you used in the sensor configuration:

stat/courtyard-motion/POWER

In addition to everything tom_I mentioned, what’s the purpose of the leading forward-slash character in the name option?

name: "/Courtyard Beam Break"
       ^
       |
      This

As far as I know, that’s not a valid character for an entity’s name and will be converted to an underscore.

1 Like

The config looks like yours. Yaml is valid. sorry, sloppy cut and paste. Here is the stat received:

19:59:38 RUL: SWITCH1#STATE=1 performs "publish stat/courtyard-motion/courtyard-motion Detected"
19:59:38 MQT: stat/courtyard-motion/courtyard-motion = Detected
19:59:38 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}
19:59:40 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
19:59:40 MQT: stat/courtyard-motion/courtyard-motion = Clear
19:59:40 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}

You are right. I had corrected that but copied an earlier version. Sorry, didn’t proof very well.

None of those match the topic or payload you are monitoring.

Looks like it should be this topic:

stat/courtyard-motion/courtyard-motion

And the payload is Detected or Clear.

binary_sensor:
  - platform: mqtt
    name: "Courtyard Beam Break"
    state_topic: "stat/courtyard-motion/courtyard-motion"
    payload_on: "Detected"
    payload_off: "Clear"
    qos: 0
    device_class: motion

Ok, Tried that but still no effect on frontend entity card. Here is the console after breaking the beam a few times.:

20:53:53 MQT: stat/courtyard-motion/courtyard-motion = Detected
20:53:53 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}
20:53:54 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
20:53:54 MQT: stat/courtyard-motion/courtyard-motion = Clear
20:53:54 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}
20:53:55 RUL: SWITCH1#STATE=1 performs "publish stat/courtyard-motion/courtyard-motion Detected"
20:53:55 MQT: stat/courtyard-motion/courtyard-motion = Detected
20:53:55 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}
20:53:56 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
20:53:56 MQT: stat/courtyard-motion/courtyard-motion = Clear
20:53:56 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}
binary_sensor:
  - platform: mqtt
    name: Courtyard Beam Break
    state_topic: stat/courtyard-motion/SWITCH1
    value_template: '{{ value_json.STATE }}'
    device_class: motion

Check the entity’s state in Developer Tools > States. If it reports the correct state there but not in the Entity card, then double-check how you configured the card.

If it fails to report the correct state in Developer Tools > States then there’s a problem with the configuration of the MQTT Binary Sensor.

thank you for your help, Taras. I tried your config. Here is the console:

21:36:30 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
21:36:30 MQT: stat/courtyard-motion/courtyard-motion = Clear
21:36:30 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}
21:36:31 RUL: SWITCH1#STATE=1 performs "publish stat/courtyard-motion/courtyard-motion Detected"
21:36:31 MQT: stat/courtyard-motion/courtyard-motion = Detected
21:36:31 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}
21:36:32 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
21:36:32 MQT: stat/courtyard-motion/courtyard-motion = Clear
21:36:32 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}
21:36:32 RUL: SWITCH1#STATE=1 performs "publish stat/courtyard-motion/courtyard-motion Detected"
21:36:32 MQT: stat/courtyard-motion/courtyard-motion = Detected
21:36:32 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}
21:36:33 RUL: SWITCH1#STATE=0 performs "Publish stat/courtyard-motion/courtyard-motion Clear"
21:36:33 MQT: stat/courtyard-motion/courtyard-motion = Clear
21:36:33 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"OFF"}

When I check Dev Tools>states for entity: binary_sensor.courtyard_beam_break it does not report the correct state after applying your config. So the problem must be with the configuration of the mqtt sensor? Any suggestions about where I should be looking?

This is how I have configured the mqtt sensor: Device Name = D1 mini, friendly name = courtyard-beam-break, Module = generic (based on sonoff Basic(1), Topic = courtyard-motion, Template = generic (Switch1, GPIO5)

I did try setting some options like SetOption 1 and SetOption 0. Not sure if that would make a diifference.

In your first post,you stated everything is working fine with the binary_sensor except for the Entity card in the frontend.

What exactly did you mean when you said “All of this is working fine”? Did binary_sensor.courtyard_beam_break report its state correctly in Developer Tools > States when using your original configuration, namely this one?

binary_sensor:
  - platform: mqtt
    name: "/Courtyard Beam Break"
    state_topic: "stat/courtyard-motion/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 0
    device_class: motion

Or did you never check that and assumed it was working?

When I’m getting at is that a lot less is working than you may have originally stated.

  1. Go to Configuration > Integrations > MQTT > Configure.
  2. At the bottom of the page you’ll find “Listen to a topic”.
  3. Enter stat/# in “Topic to subscribe to” then click “START LISTENING”.
  4. Activate the physical motion sensor. You should see MQTT messages appear on the page.

Screenshot from 2021-02-25 17-09-16

  • At least one of those messages should contain the topic stat/courtyard-motion/SWITCH1.
  • If it doesn’t then there may be a problem with Home Assistant’s connection to the MQTT broker.

When I said everything was fine I meant that I was getting response from the sensors and it was showing in the mqtt console and the config was valid and not throwing any errors but I had not checked Devtools > state of the entity until you suggested it. The state is not correct. When I listen I get this:


Message 17 received on stat/courtyard-motion/courtyard-motion at 5:23 PM:
Clear
QoS: 0 - Retain: false
Message 16 received on stat/courtyard-motion/courtyard-motion at 5:23 PM:
Detected
QoS: 0 - Retain: false
Message 15 received on stat/courtyard-motion/courtyard-motion at 5:23 PM:
Clear
QoS: 0 - Retain: false
Message 14 received on stat/courtyard-motion/courtyard-motion at 5:23 PM:
Detected
QoS: 0 - Retain: false

So it is working at that point (yay!!!) but the frontend is still not changing. It is, however, now indicating Detected. Odd thing is the on - off are reversed ie. when I break the beam it shows ‘clear’ and default is detected.

Oh, I missed the part about Switch1 so I guess it is not working. I am using the config that you suggested so Switch1 should show?

Correct. Tasmota’s console reported this:

21:36:31 MQT: stat/courtyard-motion/SWITCH1 = {"STATE":"ON"}

but, for whatever reason, Home Assistant isn’t seeing that topic. Therefore the configuration I had suggested, which depends on stat/courtyard-motion/SWITCH1, will not work.

If you have another MQTT client handy, like MQTT Explorer, you could use it to determine if anything is (or is not) actually being published to stat/courtyard-motion/SWITCH1. Otherwise, simply use the configuration provided by tom_I (the one that depends on stat/courtyard-motion/courtyard-motion).


EDIT

Out of curiosity, why did you create a Tasmota rule to publish its state to stat/courtyard-motion/courtyard-motion?

TaDa!!! Working. As you suggested I went back to the previous config and it is now working great. Thank you so much for your help. I really wanted this to work.

About the Tasmota rule. This is what I used:

SwitchMode1 1
SwitchTopic 0
Rule1 on Switch1#state=1 do publish stat/%topic%/courtyard-motion DETECTED endon on Switch1#state=0 do Publish stat/%topic%/courtyard-motion CLEAR endon
Rule1 1

I got this from the Tasmota PIR options and tried it when I couldn’t get the sensors to even respond. So would this cause the redundant courtyard-motion? If so is there any way to undo that? Like if I used Rule1 0?