Creating the Automation

The first thing we need to do is create an Automation that will pass data from Home Assistant to our MQTT topics whenever that information is updated. To do this you will want to open the main menu on the left side of the Home Assistant screen.

While Home Assistant can use MQTT, most data within that system is handled using an on-board API. So we will need to create an automation that takes data from that API and formats it to publish to our MQTT topic. This is actually rather easy using the Home Assistant UI, and this will show you how to create the content for pyportal/feed1 and pyportal/feed2.


  1. Click on the Configuration menu
  2. Scroll to and click Automations
  3. Click the + at the bottom right of the screen to create a new Automation.
    • Note: to edit an existing Automation, click the pencil icon to the right of the Automation you wish to edit.
  4. You may be asked to type out what your automation will do so that the new Home Assistant AI can create the Automation. What we are doing is a bit more complex so you will just click SKIP for this option so we can format the Automation normally.
  5. Enter a name for your Automation like Format Weather for MQTT or something like that.
  6. You can also add a Description to explain what this automation is all about.

The Trigger

Now that we have our Automation created, we need to tell it when this automation should be executed. This is called the Trigger and there are many ways we can set it up. For this example we will set our automation to trigger whenever the weather report from Home Assistant changes its State.


  1. Ensure that the Trigger type is set to State
  2. Select the Entity option
  3. Scroll to and select your main weather entity. This will most likely be weather.home

The From, To, and For options are useful if you want this to trigger only if the weather changes from sunny to rainy, but we want this to trigger whenever there is any change to this entity. So we will leave these blank and any change to the weather.home entities state will result in this automation running.

You can add as many triggers as you want and if any of them are true, then the automation will run.

Conditions are similar to Triggers but are not required. If you set any Conditions, all of them need to be True for the Automation to be executed. An example of how this could be used would be to make it so that this automation only ran if someone was home and the sun is up.


Now we get to the fun part, were we tell the automation what to do once triggered. You can have this do as many things as you like, but for this example we will simply format a message for MQTT that contains the current weather and temperature. This process uses Data Templates from Home Assistant so that we can get additional data attributes from the weather.home entity.

  1. Ensure that Call service is selected for Action Type.
  2. Select the Service drop-down.
  3. Scroll to and select mqtt.publish
  4. Now select the Service data field.
  5. Enter the following YAML code into the Service data field:
topic: pyportal/feed1
payload_template: >- 
  {{ states('weather.home') }} and {{ state_attr('weather.home', 'temperature') }}°
retain: true
qos: 2
  • Topic: The MQTT topic that you want to publish to.
  • Retain: Is this message flagged to be retained.
  • QOS: Quality of Service for this message. QOS of 2 ensures that the message is delivered no more or less than one time.
  • Payload Template: This allows you to pull data from the Home Assistant local API and format it into a message string. 
    • {{ states('entity_id') }} - prints the current State of the stated Entity.
    • {{ state_attr('entity_id', 'entity_attribute') }} - prints the value of a particular attribute from a stated Entity.

Now you should be able to click the Save icon at the bottom right of the screen.

You will want to create another one of these for pyportal/feed2 and any other data that does not already use MQTT before displaying on your PyPortal.

Going Further

If you want to format your message so that it displays information on multiple lines, you can use \n to indicate a new line. Here is an example using data from some sensors pulling data from the OneBusAway REST API:

topic: pyportal/feed1
payload_template: |-
  {{ states('sensor.next_bus') }}mins 
        Then: {{states('sensor.2nd_bus') }}mins 
          and: {{ states('sensor.3rd_bus') }}mins
retain: true
qos: 2

For this to work well you would also want to add triggers for each of the sensors, so that data is updated with the latest information for all three sensors.

This guide was first published on Jan 08, 2020. It was last updated on Jan 08, 2020.

This page (Send Data to the PyPortal) was last updated on Dec 26, 2019.

Text editor powered by tinymce.