Defining MQTT-publish topic and payload, from HTTP sensor

Hi,

I am currently trying to set up a voice-activation schema, where any voice command is published to an HTTP Sensor, and then from there sent as MQTT - and may or may not match an existing MQTT trigger. This idea is how I want to easily expand a wide variety of voice activation, based on minimal trigger setup.

Where I am having some difficulty, is defining the MQTT payload and topic from the HTTP Sensor attributes. I can’t make any progress on the data template approach, and trial and error via restarts is SLOW :slight_smile:

The automation runs fine; it detects the sensor state switch from off to on, and will run other simpler actions. The MQTT publish part fails, as I don’t know how to write the template part. If anyone could point me in the right direction, I would be very happy :slight_smile:

The HTTP sensor, with MQTT data as attributes:

The automation, which uses the HTTP sensor for state-change trigger, and MQTT data:

#Process HTTP sensor data, send MQTT
- alias: HTTP Sensor to MQTT
  id: httpsensormqtt
  trigger:
    platform: state
    entity_id: sensor.mqttsensor
    from: 'off'
    to: 'on'
  action:
    - service: mqtt.publish
      data_template:
        topic: '{{ sensor.mqttsensor.topic }}'
        payload: '{{ sensor.mqttsensor.payload }}'
        qos: 1

OK after finding the “Templates” area in Home Assistant, and google searching madly for similar examples, I found what I was looking for;

- alias: HTTP Sensor to MQTT
  id: httpsensormqtt
  trigger:
  - entity_id: sensor.mqttsensor
    platform: state
    from: 'off'
    to: 'on'
  action:
  - data_template:
      payload: "{{ states.sensor.mqttsensor.attributes.payload }}"
      topic: "{{ states.sensor.mqttsensor.attributes.topic }}"
    service: mqtt.publish

My greater plan is working great; the same Tasker ‘voice capturing’ task results in any number of MQTT triggers. Now I just need to write down a logical schema for people to use. The last word is the payload, the rest are the topic.

1 Like

Hi, I know this is old topic, but it seems you have found the way to publish sensor value to mqtt broker. I have tried numerous syntax variations by now but still can not get it to work. Do you mind sharing full yaml (without user names and psw, of course) with me ?