Querying MQTT topic from Tasmota device in template

I want to add an device field to a script, where the user of that script can provide a Tasmota device as an input, and i then want to query the command_topic attribute from that device somehow, to use it in an MQTT publish action later in that script.

Is this even possible? I do not seem to find any way to query the topic from the Tasmota device (added through the Tasmota integration using auto discovery).

Hi,
I’m afriad you’re going to have to break your need down more as it’s not clear enough to help directly.

There’s a few examples of taking MQTT data in from topics, processing, and publishing to MQTT which might help?

Personally, I use mosquitto_pub & mosquitto_sub in Bash scripts to prototype MQTT actions - can be quicker to test stuff like auto-discovery.

If this helps, :heart: this post!

What exactly is not clear? I have a Tasmota entity, from which i want to get the command_topic attribute “programatically” in a template.

I want to do something like (pseudo code) getMQTTInfo(device_id, "command_topic") or something that achieves the same thing.

Home Assistant obviously has this information, because otherwise it could not communicate with the Tasmota entity. My question is, how do i query the information from Home Assistant inside a template.

Additional Info

  • I have a script (a Home Assistant script, that is), that takes an entity as an input
  • If that entity is a Tasmota entity, the script should take the attribute command_topic from that entity and proceed
  • The script then sends out a MQTT message to that command_topic

YAML Code

alias: Blink Tasmota device
sequence:
  - service: mqtt.publish
    metadata: {}
    data:
      qos: "2"
      retain: false
      topic: {{deviceVar.HOW_DO_I_GET("command_topic")}}
      payload: blink
mode: single
icon: mdi:comment-flash
fields:
  deviceVar:
    selector:
      device: {}
    name: Tasmota device
    description: The device that should blink
    required: true

I don’t think you can get the command topic through a template.