Publish current temperature to mqtt topic

Hello,
I have read some threads here on the community forums, but I still struggle to determine the cause of my mishaps.
I am simply trying to publish current temperature from my aqara sensor every 20 seconds to an mqtt topic.
until now, my config looks like this:

- alias: 'send temp'
  trigger:
    - platform: time
      seconds: 20
  action:
    service: mqtt.publish
    data:
      payload_template: '{{ states('sensor.0x00158d000465809a_temperature') }}'
      topic: opentherm-thermostat/current-temperature/set

I pasted this into dev tools - template and I know for sure the line with payload_template will output the current temperature. But I get an error on the next line when pasting this into automations.yaml and I do not know why.

I really have veeeery low knowledge in programming, abd I need some help :slight_smile:

Thanks!

Hi Ion,

this is how my payload_template looks:
payload_template: '{{ states(''sensor.temperatur_aussensensor'') }}'

Just try the double-single quotes inside states().

Or you can use doublequotes outside then you can use single-quotes inside)

payload_template: "{{ states('sensor.temp2_temperature') }}"

Thank you all for the quick replies.

The resulting working formula is:

- alias: sendtemp
  trigger:
    - platform: time_pattern
      seconds: "/20"
  action:
    service: mqtt.publish
    data: {"payload": "{{ states('sensor.0x00158d000465809a_temperature') }}", "topic": "opentherm-thermostat/current-temperature/set", "qos": 0, "retain": 0}

where:
the trigger is: every 20 seconds.
action is: it publishes to the topic opentherm-thermostat/current-temperature/set the value of the sensor sensor.0x00158d000465809a_temperature.