Forward sensors to MQTT using data template

Tags: #<Tag:0x00007f7c57497e80> #<Tag:0x00007f7c57497d18>

Hi,

i tried to forward my sensor data to an MQTT topic using data templates so that the entity ID is contained in the transmitted JSON as MQTT payload.

My current automation looks like this:

- id: '1558647997872'
  alias: Sensor to MQTT
  trigger:
  - entity_id: sensor.temp_bath
    platform: state
  condition: []
  action:
    - service: mqtt.publish
      data_template:
        payload: '{ "entity": "{{trigger.entity_id}}", "value": {{trigger.to_state.state}}}'
        topic: homeassistant/incomming

It seems that the trigger variable is not available when rendering the template as i receive the following error:

ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'trigger' is undefined

Is there anything i did no notice when using data_templates? Mit HA version is 0.93.2

I’ve the same issue.
I found another post:


That suggest to restart Home Assistant.

I thought it was enough reload automations… but not! HA needs to be restarted!
After that, “trigger” is correctly defined! :smiley:

Hope to be helpful,
Roberto