How to get entity ID base on mqtt topic

Hi, I’m new user of HA, but I’m javascript developer and I try to make some integration with my private system. On my system I have IP, and name of ESP8266 sensor device (run on Tasmota). My system listen on the same events as HA. So if Sensort send MQTT tele/sensor_topic/LWT I would like to find out entity_id of that sensor based on “sensor_topic” and publish some event on mqtt, which allow me to connect my system Sensor data with HA Sensor.
Other way I find out to make integration is to use HA API and get based on entity_id “sensor_topic”.

So I have question is any of that integration possible or not? Maybe there is another way to integrate such systems?

Any help or tip, how to solve taht problem, will more than welcome

If it runs Tasmota just setoption19 = 1

This enables discovery and home assistant will automatically discover the entities associated with the device.

Yes, this I know and I’ve done it. After that HA knows somehow that topic sensor_topic is assign to entity for example sensor.my_new_weather_sensor. And my problem is to know where HA is store that relations and how to get that information from API or other way?

Because know when my Tasmota send tele/sensor_topic/LWT to HA, I would like to create automation which based on “sensor_topic” send to my system via mqtt entity_id of that sensor. Or if it is not possible I would like to get sensor_topic based on entity_Id in HA.

All that work is for my custom component to HA, which takes list of entity_id. Based on that list it call my API and try to get data, but know I dont have any connections between “sensor_topic” and “entity_id”

When you do ‘setoption19 1’ in the tasmota console, tasmota sends some configuration messages to HA.
For example :

{"name":"lichtbadkamer status","stat_t":"~HASS_STATE","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","json_attributes_topic":"~HASS_STATE","unit_of_meas":" ","val_tpl":"{{value_json['RSSI']}}","uniq_id":"C7D5C9_status","device":{"identifiers":["C7D5C9"],"name":"lichtbadkamer","model":"Sonoff Basic","sw_version":"6.6.0(release-sonoff)","manufacturer":"Tasmota"},"~":"lichtbadkamer/tele/"}

Here HA will create an entity named sensor.lichtbadkamer_status, because of this part :

"name":"lichtbadkamer status"

There is no direct relation between lichtbadkamer/tele/LWT and the entity_id

Oh, on newer tasmota versions the topic will be tele/lichtbadkamer/LWT

You are right, but on other way, there is possibility to change device nam on tasmota, and also change entity_id on HA. And still after such change all functionallity works. based on that I assume that there is some correlations between “topic” or “other device/sensor value” and entity_id.

If you have done a SetOption19 1 in Tasmota, and then change the device name in Tasmota, Tasmota sends a new discovery message, but this part will be the same :

"device":{"identifiers":["C7D5C9"],

The relation is created when the entity_id is created. There is no way I know of once created how you can detect the entity_id from a received mqtt message.

Ok, so this “identifiers” is constant value.

But HA has some possibility to convert such mqtt message

17:38:45 MQT: tele/ws_08F212/SENSOR = {"Time":"2020-05-11T17:38:45","DHT11":{"Temperature":24.9,"Humidity":54.0,"DewPoint":14.9},"TempUnit":"C"}

to proper entity?

Anyone knows how it works?

If you look at the trigger event for mqtt, the entity_id is not listed :

MQTT

Template variable Data
trigger.platform Hardcoded: mqtt .
trigger.topic Topic that received payload.
trigger.payload Payload.
trigger.payload_json Dictonary of the JSON parsed payload.
trigger.qos QOS of payload.

I know that, because I study ths documentation, but I mean HA knows it somehow and I woulld like to figure out “How”?

There is only one answer to that : study the source-code of the mqtt integration.

I was afraid that it will be the only way, but I will try in free time, if I figure out I will share with you that knowladge.

@qjon Did you ever find a solution for this? I have a similar use-case where I want to subscribe to the MQTT topic to trigger anytime its published but I also want to keep the auto discovery working so hardcoding the topic path doesn’t feel right.

The problem with something like this is if the sensor stays “on” for longer than 15mins without flicking to “off” my flights go off.

- alias: 'Start/Restart timer if motion OR switch pressed office'
  trigger:
    - platform: state
      entity_id: binary_sensor.office_sensor_occupancy
      to: 'on'