MQTT BROKER and entity

Hi

I’ve been searching everyvery, and tried “everything” but can’t figure out how to add an entity from an MQTT discovered motion sensor.

This is what I see inn MQTT broker:

Message 36 received on /mqtt/out at 4:27 PM:
20;97;EV1527;ID=000373;SWITCH=0a;CMD=ON;

Tried this in configuration.yaml, but there is no response from the sensor.

binary_sensor:
  - platform: mqtt
    name: "PIR TEST"
    state_topic: "/mqtt/out/EV1527_000373_0a"
    device_class: motion
    payload_on: "on"
    off_delay: 5

PS: The sensor only sends ON command

What am I missing? :slight_smile:

PI4 - Sonoff bridge with RFLink32

MQTT stuff has changed do it like this

Thx 4 quick reply.

I am a totally noob to most of this, and has been playing around with HA for 2-3 months now. I have tried to do it like in the link, but I must have done something wrong some place. Can I create a motion-sensor entity with the information provided above, or do I need some other info?

Maybe someone is willing to help me with my first sensor? :slight_smile:

Post what you tried.

Well I have tried the config exsample:

# Example configuration.yaml entry
mqtt:
  binary_sensor:
    - name: PIR Test
      state_topic: "mqtt/out/EV1527_000373_0a"
      payload_on: "on"

And I have tried to modify it to make it show as a motion

mqtt:
  binary_sensor:
    - name: PIR Test
      state_topic: "mqtt/out/EV1527_000373_0a"
      device_class: motion
      payload_on: "on"
      off_delay: 5

No mater what I do it shows up in entities, but does not react to motion ( wrong code somewhere )
Also tried to write it inside binary-sensor with mqtt platform like this:

binary_sensor:
  - platform: mqtt
    name: "PIR TEST"
    state_topic: "mqtt/out/EV1527_000373_0a"
    device_class: motion
    payload_on: "on"
    off_delay: 5

Based on this:

are you sure your state topic is not just “mqtt/out” and you receive the rest as semicolon separated list in the payload?

Armin

That might be right. But how do I set it up then? :slight_smile:
Can you help me on the way?

Hi
not really sure if this is correct, don’t have much experience with templates, only did JSON-Data so far, but your data seems to be just a semicolon separated string

mqtt:
  binary_sensor:
    - name: PIR Test
      state_topic: "mqtt/out"
      payload_on: "ON"
      value_template: >-
        {% set list1 = value.split(';') %}
        {% set cmd = list1[5].split("=") %}
        {{cmd[1]}}

the value-template splits the data at the semicolon, takes the 6th (list starts at 0) element in the list and splits at the “=”-sign
works for me in the DEV-Tools, but I’m not sure if the value in the state topic is contained in variable “value”

Armin

Assuming the correct state topic is mqtt/out and published messages always contain either CMD=ON or CMD=OFF then this should be all that’s needed.

mqtt:
  binary_sensor:
    - name: PIR Test
      state_topic: "mqtt/out"
      value_template: "{{ 'ON' if 'CMD=ON' in value else 'OFF' }}"

Could you help share what this topic would look like using tools such as mqtt explorer? This should be a good start to troubleshoot.

Hi and thanx to all of you for helping a n00bie :slight_smile:

Whit this code in confiuration.yaml it still looks the same in MQTT Explorer. See attatched image

mqtt

What are expecting to see in MQTT Explorer?

When you configure an MQTT Binary Sensor properly, it will appear as a new binary_sensor in Developer Tools > States and in Configuration > Devices & Services > Entities. It won’t appear as a new topic in your MQTT Broker.

Are you seeing an entity named binary_sensor.pir_test in Developer Tools > States?

  • If you don’t see it in the list, execute Developer Tools > YAML > Reload Manually Configured MQTT Entities (or restart Home Assistant).

  • If you do see it in the list, what is its state value?

I how no ide what to expect in Explorer, but I guess somthing like ON since that is what I need from the sensor to use it.

The state value of the sensor is unknown

It does show the payload contains CMD=ON.

The fact that binary_sensor.pir_test reports unknown means that it has not yet received any payload via its state_topic. Are you sure Home Assistant is logged in and communicating with your MQTT broker?

Go to Configuration> Integrations > Mosquitto broker and click Configuration. You will be presented with a screen like shown below. Enter mqtt/out as shown in the screenshot and click Start Listening. It should display the payloads published to mqtt/out.

Aha… It was sending at

/mqtt/out

Changed it to mqtt/out and now it working. Thanx alot guys.

On to the next ( simple ) question: Can I add more than one value to the value template? Since I have alot of these sensors I need to separate them. So instead of the CMD=ON I would like to use both the ID and the switch.

value_template: "{{ 'ON' if 'BOTH ID AND SWITCH HERE' in value else 'OFF' }}"

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.


If you have one topic, mqtt/out, that contains information from multiple devices then it’s best to use Strategy #2 in the following post:

In a nutshell, you create an automation that subscribes to mqtt/out (using an MQTT Trigger) and it examines the received payload. If the payload is from device A (represented by some combination of ID and SWITCH values), the automation publishes it to a different MQTT topic dedicated for the use of device A only. It’s a proven technique that simplifies the process of configuring MQTT entities that share a common MQTT topic.

I can help you implement this method but I would need to know the ID and SWITCH values for each device that publishes information to mqtt/out.

1 Like