How to get the name of the actual state_topic in an MQTT sensor?

I would like to have a single sensor that gathers all MQTT events for a set of topics (specifically, form Zigbee2MQTT). I tried to add

sensor:
  - platform: mqtt
    state_topic: "zigbee2mqtt/+"
    name: zigbee

and I indeed now see a sensor.zigbee with the value of the last MQTT message from Zigbee2MQTT. The problem is that I do not know what topic it relates to.

Would there be a smart way to create such a sensor and avoid making one entry per device (topic)?

Use an automation instead with a mqtt trigger. Drop the topic and the event into input_texts. Use a template to get the information out.

Thank you for the idea. When you mention “drop”, what would the actual action be? (I understand the templating part (I think), it is more how to actually create (then update) a sensor I do not know)

- id: '1606228580226'
  mode: queued
  max: 10
  alias: Zigbee2MQTT events
  description: ''
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+
  condition: []
  action:
  # here comes the action to extract trigger.topic and trigger.payload and combne them into the sensor ← this is what I do not understand how to do
  

drop, I mean set the text of the input_text to the trigger data. You’re not going to be using a sensor. If you want to use a sensor, you’ll have to use one of the many back door methods to get the information into the sensor.

if you want to see what topics are sent and received to/from your broker, the easiest tool is something like Mqtt explorer. it lists all of your registered subscribers and all of the sent topics with their values.

A simple copy and past will then give you the topic for the mqtt sensor in HA.

OK, thank you, I understand: to use as the action the service set_value to set the content of an input_text defined in the configuration.

That would work - I just hope that a change of the content of that input_text is listenable to (= its state changes when the content change)

I know all the topics (I use MQTT extensively also outside of HA) and actually use MQTT Explorer (after having used MQTT.fx for some time).

The problem is not to know what can come in but to avoid listing my 20 or 30 devices one by one.

ok, sorry, I misunderstood your question.

afraid I wouldn’t really now of a way to circumvent the 255 char limit of the sensor, and set the mqtt payload as attribute for that sensor (the backdoor Petro refers to). Same would go for a notification file platform I fear, which is what I use to record larger sets of data.

You are using ZigBee2MQTT so why not enable MQTT Discovery and allow it to automatically create the sensors in Home Assistant?

I do and they are created.

The problem is that I use the wall switches to automate some lights and I want to keep the zigbee id ← → action in the automation (via pyscript). So I need to know which topic sent which topic sent which payload.

In that case, you’re halfway there. The automation you posted (above) subscribes to all ZigBee2MQTT topics. All that’s left is to create an action that publishes the received topic and payload to an MQTT Sensor’s topic.

Where is this topic? (the topic actually matched by the + wildcard) The only see I see is the payload.

trigger.topic

Documentation:

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.

OK, so it still goes through an extra automation.This is going to be the same as @petro solution, right?

yep, because what you’re trying to do is not possible otherwise

OK, clear. Thanks for the help!

I’d be very interested in what you would come up with to create the listing of topics and events/payloads you are looking for. Could you keep us posted on that?

No problem.

I am currently rethinking my automations. I use AppDaemon but I am considering moving to pyscript. When this actually happens I may do one of two things:

  • use an automation in HA to send my MQTT data to a @service there (this is what is pointed to by the answers in this thread)
  • or connect to MQTT directly from pyscript (via paho)

I would prefer the first option if I was able to have a sensor for my data. I have one for RF433 signals (I go though a Sonoff RF Bridge that posts everything to one topic). But if I cannot have the same for Ziggbee2MQTT then I will probably go for the second solution and centralize the wall switch management there. Any maybe update a state in HA from there (to have a feedback/history in HA)

I will update once this is in place.