Adding a MQTT doorbell to my configuration.yaml file - What am I doing wrong?

Hello HA Community!

I’ve managed to get my doorbell to publish mqtt events to the Mosquitto broker installed in my HA instance but I’m having trouble creating the binary sensor to state change on those incoming mqtt events.

My goal is that when the doorbell rings I can trigger an automation to play a sound on several smart speakers.

This is the event that the doorbell emits when someone rings the bell:

{
    "Action": "Start",
    "Code": "CallNoAnswered",
    "Data": {
        "CallID": "8",
        "LocaleTime": "2022-04-28 10:21:55",
        "UTC": 1651170115.0
    },
    "Index": 0,
    "serialNumber": "LNWZ6C34FA0F941"
}

And this is the binary sensor create that I’ve added to my configuration.yaml file which isn’t working:

binary_sensor:
  - platform: mqtt
    name: "Doorbell Button State"
    state_topic: "DahuaVTO/CallNoAnswered/Event"
    availability_topic: "DahuaVTO/CallNoAnswered/Event"
    value_template: "{{ 'ON' if value_json.Action == 'Start' else 'OFF' }}"

The resulting binary sensor has failed to pick up any state changes:
Overview_–_Home_Assistant

Thanks for any help!

I’m not sure you should use the availability_topic: . Since the MQTT setup for the “Doorbell” availability_topic: is not really there.

That’s all I can think of at the moment.

Good idea! I just tried removing availability_topic and it’s still not picking up the event.

binary_sensor:
  - platform: mqtt
    name: "Doorbell Button State"
    state_topic: "DahuaVTO/CallNoAnswered/Event"
    value_template: "{{ 'ON' if value_json.Action == 'Start' else 'OFF' }}"

Maybe it needs the double quote marks for Start command.

value_template: "{{ 'ON' if value_json.Action == "Start" else 'OFF' }}"

or try a triple quoted '"Start"'

I was originally using "Start" but vscode threw an error. It’s complaining about triple quotes as well.

Is it safe to ignore this warning and give it a try?

No it’s a genuine validation error.
You could only escape the quotes:

\"Start\"

But it wouldn’t make any difference, the quotes aren’t the problem in this logic. I can’t see any reason it wouldn’t work. It may however be worth subscribing to the topic in the MQTT integration page in Home Assistant, just so you can be absolutely certain that what Home Assistant is receiving, matches what you see in MQTT Box.

Edit:

You might also try:

value_json['Action'] == 'Start'
1 Like

Did you try:

binary_sensor:
  - platform: mqtt
    name: "Doorbell Button State"
    state_topic: "DahuaVTO/CallNoAnswered/Event"
    availability_topic: "DahuaVTO/CallNoAnswered/Event"
    value_template: "{{  value_json.Action  }}"
    payload_on: "Start"
    payload_off: "Stop"

Shouldn’t really make a difference though.
payload_on defaults to ‘ON’ and payload_off defaults to ‘OFF’.
The logic that was posted in the value_template should at the absolute minimum if it is not matching ‘Start’ - be setting the sensor to ‘OFF’

I would be more inclined to believe that this was a case of Home Assistant simply never receiving the message in the first place.

True but this eliminates any weirdness in C++ types, although comparing arrays to strings usually throws an error. Dunno… :slight_smile:

@mobile.andrew.jones !

It may however be worth subscribing to the topic in the MQTT integration page in Home Assistant, just so you can be absolutely certain that what Home Assistant is receiving

Progress! When I explicitly tell MQTT to listen for that topic it picks it up and my automation worked!

Which leaves me with two questions:

  1. Why wasn’t Home Assistant picking up this topic out of the box?
  2. The “Listen to a topic” setting in MQTT config looks like a troubleshooting tool since it only allows you to specify one topic to listen to at a time. Is there a better place where I can specify that this topic should be listened to?

Thank you!

Defining it as a binary_sensor should tell it to listen. Did you restart after though? configuration.yaml changes require a restart to take effect.

1 Like

Yup, I have it configured as a binary sensor just like this

  - platform: mqtt
    name: "Doorbell Button State"
    state_topic: "DahuaVTO/CallNoAnswered/Event"
    value_template: "{{ 'ON' if value_json.Action == 'Start' else 'OFF' }}"```

and yep, I'm restarting after each config file edit.

Someone should really create a simple web tool that allows us to add device config and it generates the MQTT Discovery message for us to post to Home Assistant.

Bug maybe? If you can reproduce the behaviour with a new sensor maybe log an issue on GitHub?

Shouldn’t be necessary -
but might also be worth trying to use the same format as is listed in the official documentation:

- platform: mqtt
    name: "Doorbell Button State"
    state_topic: "DahuaVTO/CallNoAnswered/Event"
    value_template: >
      {%if value_json.Action == 'Start'-%}ON{%-else-%}OFF{%-endif%}