This guide will walk you through setting up Home Assistant to send sensor data to a TRMNL plugin. It includes installation, configuration, and troubleshooting steps for common issues.
- A working Home Assistant setup
- A TRMNL device or TRMNL plugin API key
- Basic knowledge of YAML and Python
- Python 3.9+ installed on your system
- Open your Home Assistant
configuration.yamlfile. - Add the following code to define REST commands to send data to TRMNL:
rest_command:
send_to_trmnl:
url: "http://IP-TO-Server/receive"
method: "post"
headers:
Content-Type: "application/json"
payload: >
{
"temperature": "{{ states('sensor.air_sensor_temperature') | float }}",
"pm25": "{{ states('sensor.air_sensor_current_pm2_5') | float }}"
}
content_type: "application/json"- Save and restart Home Assistant to apply the changes.
Create a server.py file to receive data from Home Assistant and send it to TRMNL.
Run the following command to install dependencies:
pip install flask requestsfrom flask import Flask, request, jsonify
import requests
app = Flask(__name__)
TRMNL_URL = "https://usetrmnl.com/api/custom_plugins/YOUR_PLUGIN_ID"
@app.route('/receive', methods=['POST'])
def receive_data():
data = request.get_json()
temperature = data.get("temperature", "N/A")
pm25 = data.get("pm25", "N/A")
html_content = f'''
<div class="layout layout--col gap--space-between">
<div class="grid grid--cols-1 gap--xlarge" style="max-width: 85%;">
<div class="grid grid--cols-2">
<div class="item">
<div class="content">
<span class="value value--large">{temperature}Β°C</span>
<span class="label label--large">Current Temperature</span>
</div>
</div>
<div class="item">
<div class="content">
<span class="value value--large">{pm25} Β΅g/mΒ³</span>
<span class="label label--large">PM2.5 Level</span>
</div>
</div>
</div>
</div>
</div>
'''
trmnl_payload = {
"merge_variables": [
{"content": html_content}
]
}
response = requests.post(TRMNL_URL, json=trmnl_payload, headers={"Content-Type": "application/json"})
return jsonify({"TRMNL Response": response.status_code, "Details": response.json()})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)- Run the server:
python server.py- Open Home Assistant UI and go to
Automations. - Click Create Automation and set it up as follows:
- Trigger: Every minute
- Action: Call
rest_command.send_to_trmnl
- Save and restart Home Assistant.
β Fix: Modify the payload to include a default value:
payload: >
{
"temperature": "{{ states('sensor.air_sensor_temperature') | float(default=0) }}",
"pm25": "{{ states('sensor.air_sensor_current_pm2_5') | float(default=0) }}"
}β
Fix: Ensure the server.py is running and reachable from Home Assistant.
Try:
curl -X POST -H "Content-Type: application/json" -d '{"temperature": 22.5, "pm25": 12.7}' "http://192.168.1.169:5000/receive"If this fails, restart the server and check network connectivity.
β Fix: Ensure the payload follows the required format:
trmnl_payload = {
"merge_variables": [
{"content": html_content}
]
}β Fix: Try sending a test request directly to TRMNL:
curl -X POST -H "Content-Type: application/json" \
-d '{"merge_variables": [{"content": "<h2>π‘ Temp: 22.5Β°C</h2><h2>π¨ PM2.5: 12.7 Β΅g/mΒ³</h2>"}]}' \
"https://usetrmnl.com/api/custom_plugins/YOUR_PLUGIN_ID"If this does not update the display, restart TRMNL and check for API restrictions.
- Ensure Home Assistant & TRMNL are communicating
- Restart Home Assistant and TRMNL after any changes
- Check Home Assistant logs for automation execution
- Check
server.pylogs for errors
You have successfully integrated Home Assistant with TRMNL! π