How do I manually create and read mqtt sensor data?

I apologize for yet another one of these threads, but I’ve reached the bottom of my debug barrel…

SETUP

  • RPi4 with home assistant installed
  • MQTT server installed through HA supervisor
  • RPi Zero w/ SDR dongle attached and an rtl_433 server installed/running tuned to 345mhz AND publishing sensor data through MQTT
  • Honeywell 5800 open/close and PIR sensors

ISSUE
I’m able to verify that the rtl_433 server on the RPi0 is properly reading the broadcast signal from my Honeywell devices. I can also verify that the MQTT data is being published from the rtl_433 server. I am able to successfully listen for the specific device topics through the MQTT integration inside HA.

What I can’t seem to figure out is how to properly create and connect a device or entity inside HA to read the necessary data. The closest attempt I have at the moment is this entry in my sensor.yaml for my open/close sensor:

  - platform: mqtt
    state_topic: 'rtl_433/security/devices/Honeywell-Security/8/81290/state'
    unique_id: 'door_sensor_patio'
    name: 'Patio Door'
    qos: 0
    value_template: '{% if value_json.id is equalto 81290 %} {{value_json.state}} {% else %} {{states.sensor.door_sensor_patio.state}} {% endif %}'

With this I at least have an entity showing up in HA, but it always in an ‘unknown’ state. My guess is I’m goofing something in the value_template area? I’ve combed through other threads trying to Frankenstein together some combination of parameters in hopes this will magically start to work, but it might be better if I could get some help understanding the fundamentals of how this type of thing should be set up.

Please post the whole value_json.

Verify the entity_id sensor.door_sensor_patio in Dev Tools/States.

Here’s what I think you mean by the ‘value_json’

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
time      : 2022-05-21 18:21:37
model     : Honeywell-Security                     id        : 13d8a
channel   : 8            event     : 40            state     : closed        contact_open: 0
reed_open : 0            alarm     : 0             tamper    : 1             battery_ok: 1             heartbeat : 0
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Here’s the sensor in question through the Dev Tool/States menu

Right off the bat I clearly had the name of the sensor wrong. I’ve corrected that, and after looking at the two other threads you posted, took a couple new attempts at reading in a useful value. No dice, still ‘unknown’. Here’s my latest attempt:

  - platform: mqtt
    state_topic: 'rtl_433/security/devices/Honeywell-Security/8/81290/state'
    unique_id: 'door_sensor_patio'
    name: 'Patio Door'
    qos: 0
    value_template: >
      {% if value_json is defined and value_json.state == 40 %}
        {{ value_json.state }}
      {% else %}
        {{ states('sensor.patio_door') }}
      {% endif %}

I think I just fundamentally misunderstand the syntax involved. Based on my value_json, I’m assuming I can call any one of those pieces using ‘value_json.’ (value_json.state, value_json.alarm, value_json.tamper)? So with that assumption, my hope would be that calling ‘{{value_json.state}}’ would return either ‘open’ or ‘closed’.

No, i mean the MQTT integration in Settings > Devices & Services > MQTT > Configure.
Paste your state_topic and ‘Start Listening’.

If the value_json is correct, your value_template is wrong, because the state is ‘open’ and you’re testing for ‘40’.
Try:

    value_template: >
      {% if value_json is defined and value_json.id == '13d8a' %} # with and without quotes
        {{ value_json.state }}
      {% else %}
        {{ states('sensor.patio_door') }}
      {% endif %}

Tried your snippet. Without quotes HA barks, but even with quotes I’m still getting a perpetually ‘unknown’ state.

Here’s the output listening to the state topic through the MQTT integration:

Message 24 received on rtl_433/security/devices/Honeywell-Security/8/81290/state at 10:37 AM:
open
QoS: 0 - Retain: false
Message 23 received on rtl_433/security/devices/Honeywell-Security/8/81290/state at 10:37 AM:
closed
QoS: 0 - Retain: false

I made sure to include the output from both an open and closed just to make the point that it is reading a change in state when I move the magnet away from the sensor.

If this is the message on that topic you don’t need a value_template at all. :slight_smile:
Just remove it.
BTW: I’d rather use a mqtt binary_sensor for door states.

Oh wow. Yup that did it. It’s now reporting open/closed.
How would I go about referencing the other data the sensor is reporting?

Also, I totally agree about it being better as a binary sensor, however, I can’t seem to copy/paste this code from sensor.yaml to binary_sensor.yaml. Is there something special I need to do to make that work?

Install MQTT Explorer and you can browse your MQTT Server for the topics.

Example:

binary_sensor:
  # Window/Door sensor
  - platform: mqtt
    state_topic: 'rtl_433/security/devices/Honeywell-Security/8/81290/state'
    unique_id: 'door_sensor_patio'
    name: 'Patio Door'
    qos: 0
    payload_on: 'open'
    payload_off: 'closed'
    device_class: window

That example still comes back as ‘unknown’…
Again, I’m sure this is just a blind spot in my knowledge of reading/processing mqtt sensor data.

I did install MQTT Explorer, but at first glance the data coming out of that feels very reminiscent of the data I was already getting through HA. Do I need to keep digging around there to find what I’m looking for?

What software is that running on the rpi0? Is it something you can point us to, or did you write it?

I’m using the fantastic rtl_433 project. Or if you’d prefer to install directly through HA.

I am not interested for myself, but doesn’t it have HA discovery built in? Integration | rtl_433, or rather there is a python script which does.