Validate a JSON MQTT Payload Before Using It in a Home Assistant Automation
Описание и применение
Проверка корректности JSON‑payload в MQTT‑сообщениях для Home Assistant: выбор временной темы, публикация контрольных сообщений, проверка парсинга и условий триггера, безопасные тестовые действия, очистка и документирование результатов.
Задача
An MQTT message can be malformed, arrive on the wrong topic, or be retained from an earlier test, causing Home Assistant automations to react incorrectly.
Результат
The test confirms that Home Assistant automations only trigger on correctly formatted JSON payloads with the expected event field, and that harmless test actions run as intended.
Рабочий процесс
- 1. Choose a temporary topic that no existing publisher or rule uses, e.g. lab/json-payload-check-2026-10-29/event.
- 2. Publish a valid, fictional JSON message with the expected field and value, e.g. {"event":"check","sequence":1}.
- 3. Publish a second valid JSON message with a deliberately different event value, e.g. {"event":"ignore","sequence":2}.
- 4. Optionally publish a malformed string if the diagnostic path can safely display it.
- 5. Record publish time, topic, retain flag, subscriber payload, extracted value, and whether the rule triggered for each message.
- 6. Ensure the automation action is harmless (notification, logbook entry, helper).
- 7. Inspect the automation trace after each test message to confirm the trigger path.
- 8. If no trace, review topic and payload configuration; if trace but no visible result, adjust conditions or action and repeat with a fictional input.
- 9. After testing, disable or remove the temporary automation and clear any retained lab data.
- 10. Keep a non-sensitive record of the topic, sample shape, observation times, and trace results for future reference.
Доказательства
An MQTT message can look like JSON and still be unsafe input for an automation. It might be malformed, arrive on the wrong topic, or be retained from an earlier test.
Start with a smaller question than “does MQTT work?”: can a Home Assistant rule receive one controlled JSON message, extract the intended value, and make one harmless change you can observe?
For the first successful match, make the automation action a persistent notification, a logbook entry, or a dedicated helper.
- Дата записи: 2026-11-01T20:00:01.023Z