OpenMQTTGateway template sensor problem

Hi All,

I am trying to setup Room Presence with OMG, with the use of my mifit band. But I am unable to get the distance attribute of a specific Bluetooth device as a sensor state from the MQTT data.

Example of output from OWG:

{"id":"xx:xx:xx:xx:xx:xx","manufacturerdata":"W","rssi":-45,"distance":0.06662}

I can get the values as independent sensors from the MQTT state topic via ‘value_json’

But I cannot get a value template to display the distance value of a specific device as the sensor state.

I have set up the following to make sure the values are accessible

sensor:


  - platform: mqtt
    name: "band_distance"
    state_topic: "home/home_presence/OpenMQTTGateway_ESP32_BLE"
    value_template: "{{ value_json.distance }}"

And


  - platform: mqtt
    name: "band_id"
    state_topic: "home/home_presence/OpenMQTTGateway_ESP32_BLE"
    value_template: "{{ value_json.id }}"  

These both output their respective states correctly.

But if I try the following to output the distance value of a specific scanned “ID” it fails?


  - platform: mqtt
    name: "miband_distance"
    state_topic: "home/home_presence/OpenMQTTGateway_ESP32_BLE"
    unit_of_measurement: 'm'
    value_template: >-
      {% if value_json.id == "xx:xx:xx:xx:xx:xx" %} 
      {{value_json.distance}}
      {% else %}
        not detected
      {% endif %}     

I get state of sensor as “not detected”


I have also tried using state_topic of the specific Bluetooth device using"

state_topic: 'home/OpenMQTTGateway_ESP32_BLE/BTtoMQTT/xxxxxxxxxxxx/

but I get no values using the above working independent value methods

In the Home Assistant template developer tool, the JSON output works fine???

EG.


{% set value_json = {"id":"xx:xx:xx:xx:xx:xx","manufacturerdata":"W","rssi":-45,"distance":0.06662} %}
      {% if value_json.id == "xx:xx:xx:xx:xx:xx" %} 
      {{value_json.distance}}
      {% else %}
        not detected
      {% endif %} 

Output is: 0.06662

Can someone please tell me where I am going wrong?

Thanks in Advance!

The problem is that every time the payload changes and the id doesn’t match, the template evaluates to ‘not detected’.
Give the template sensor it’s own value. For example:

  - platform: mqtt
    state_topic: "home/rtl_433"
    name: "Aussen 433"
    unit_of_measurement: "°C"
    value_template: >
      {% if value_json is defined and value_json.id == 46 %}
        {{ value_json.temperature_C | round(1) }}
      {% else %}
        {{ states('sensor.aussen_433') }}
      {% endif %}

EDIT: And in this topic there’s also another solution for this.

Hi @VDRainer thanks for the info

I have tried what I think, you suggested but with no luck

  - platform: mqtt
    name: "miband_distance"
    state_topic: "home/home_presence/OpenMQTTGateway_ESP32_BLE"
    unit_of_measurement: 'm'
    value_template: >-
      {% if value_json is defined and value_json.id == "xx:xx:xx:xx:xx:xx" %} 
        {{value_json.distance}}
      {% else %}
        {{ states('sensor.miband_distance') }}
      {% endif %}     

any thoughts?

bump any one else able to assist?