MQTT publish automation, need help

I have a few Wemos units with displays and I would like to configure the “Generic - MQTT Import” sensor to receive a broadcast of sorts that contains the real temperature and feels like temperature to the units to display. I have the units set up to receive traffic and that part works fine.

I am having trouble with the following automation:

- id: "Broadcast Real Temp MQTT"
  alias: 'Broadcast Real Temp MQTT'
  trigger:
    - platform: time
      minutes: '/1'
      seconds: '0'
  action:
    service: mqtt.publish
    data_template:
      payload_template: '{{ sensor.pws_temp_f.payload }}'
      topic: 'Real'

My goal is to transmit the temperature to the topic “Real”, every X number of minutes.
I am getting the following errors every interval:

018-02-07 22:30:00 ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'sensor' is undefined
2018-02-07 22:30:00 ERROR (MainThread) [homeassistant.core] Invalid service data for mqtt.publish: required key not provided @ data['topic']. Got None

The sensor in the States panel is:

sensor.pws_temp_f

with a numeric value containing a decimal.
I know I have something screwed up in the template or maybe I need to template the topic some how…

Thanks Matt

I think this sensor.pws_temp_f should be states.sensor.pws_temp_f.state

1 Like

You are awesome! It works. I have been reading about templates and I just don’t get it. I am not a programmer, that does not help!

:slight_smile: glad you got it working :slight_smile: BTW hit the solution icon to show others how to fix it.

do you mind sharing how you setup you mqtt settings??

Not at all… do you mean the broker or the automation or the code on the device catching the broadcast?

i would probably say all… since i been trying this for a week now without success… maybe i can start fresh

if you want i can post mine and maybe you can spot what am i missing

It is probably more lines than are really needed, but could not add the second service line to the first automation without a duplicate error. These are in an automation file, so you would need to format it for use in the configuration.yaml

This works well though

- id: "Broadcast Real Temp MQTT"
  alias: 'Broadcast Real Temp MQTT'
  trigger:
    - platform: time
      minutes: '/3'
      seconds: '0'
  action:
    service: mqtt.publish
    data_template:
      payload_template: '{{ states.sensor.pws_temp_f.state }}'
      topic: 'Real'
 
- id: "Broadcast Feels Like Temp MQTT"
  alias: 'Broadcast Feels Like Temp MQTT'
  trigger:
    - platform: time
      minutes: '/3'
      seconds: '0'
  action:
    service: mqtt.publish
    data_template:
      payload_template: '{{ states.sensor.pws_feelslike_f.state }}'
      topic: 'FeelsLike'

I am using Wemos D1’s to receive the “broadcast”. The Wemos units are running ESPEasy and I have defined a device as MQTT Sniffer and it detects the MQTT topic and pulls out the payload and then displays it on the little Wemos display.

Hope this helps, it is a very handy addition to a Wemos stack!

Wemos sniffer:

Wemos modules:

Wemos display setting:

thank you let me try this settings out