Adding a MQTT motion sensor to the configuration.yaml file? What am I doing wrong?

Hello HA community!

I have been able to add a few MQTT sensors to my Home Assistant instance but I’m having issues with one motion sensor. All of my sensors except the motion sensor send individual topics back to the MQTT broker. With each of those topics I am able to add them to the YAML file without issue and they work as expected (see green box within the below screenshot). When the motion sensor sends its data back to the MQTT broker it does so with all the individual topics in a single line under “status” (see red box within the below screenshot. How do I go about splitting off each topic from the status line within the YAML file so I can use each within Home Assistant?

Here is my broken YAML.

binary_sensor:	
  - platform: mqtt
    name: "Shelly Motion Sensor 1 - Motion"
    state_topic: "shellies/Shelly_Motion_Sensor_1/status"
    availability_topic: "shellies/Shelly_Motion_Sensor_1/status"
    device_class: motion
    payload_on: "true"
    payload_off: "false"
    qos: 0

1 Like

I can’t see the screenshot (corporate firewall blocking it). Can you either paste it as formatted text (preferred) or drag and drop the image into a post?

I’m using MQTT Explorer so its a screenshot. Thank you in advance.

That MQTT topic is returning JSON instead of a simple true/false string. You need to add a value_template line to tell it to look for the true/false string in the motion part of the JSON string.

Adding this to your config should work.

value_template: {{value_json.motion}}

1 Like

Might as well make the value_template do a bit more work:

binary_sensor:	
  - platform: mqtt
    name: "Shelly Motion Sensor 1 - Motion"
    state_topic: "shellies/Shelly_Motion_Sensor_1/status"
    availability_topic: "shellies/Shelly_Motion_Sensor_1/status"
    value_template: "{{ 'ON' if value_json.motion == 'true' else 'OFF' }}"
    device_class: motion

When I add this to my YAML file HA shows the motion sensor as “unavailable”. Any idea?

When I add this to my YAML file HA shows the motion sensor as “clear” but the status is never updated if motion is detected. Any idea?

That means the template is always reporting OFF and never ON. In this case it may be due to how true is interpreted. Currently it’s being handled as a string value but it may in fact be understood as a boolean value. It’s easy to confirm test that hypothesis.

Replace the existing value_template with this:

value_template: "{{ 'ON' if value_json.motion else 'OFF' }}"
1 Like

That did it! Thank you very much.

I want to learn how to do this myself. Any websites you would recommend for a beginner?

Thanks again for the help. :slightly_smiling_face:

1 Like

Is this still working in the latest HA version 2021.12.x ? For me i tried the suggestion with

value_template: "{{ 'ON' if value_json.motion else 'OFF' }}"

But for me the binary sensor just displayed as unavailable, here is the snip from my binary_sensor.yaml:

- platform: mqtt
  name: 'Gang Oben Bewegungsmelder Shelly'
  state_topic: "shellies/gangobenmotion/status"
  availability_topic: "shellies/gangobenmotion/status"
  value_template: "{{ 'ON' if value_json.motion else 'OFF' }}"
  device_class: motion

If i manually copy the json from the MQTT and test the value_template then it’s working. I’m a little bit lost here :confused: and also out of ideas.

binary_sensor:

  • unique_id: door_motion
    name: ‘door_motion’
    state_topic: “yicam/motion”
    payload_on: “motion_start”
    payload_off: “motion_stop”
    device_class: motion
    qos: 0