The Message log integration allows you to log and view logged messages in Home Assistant added from a external system. Message can be created as info, attention, warning or error.
For installation instructions until the Message log integrations is part of HACS, see this guide. Or click
Configuration is setup via UI in Home assistant. To add one, go to Settings > Devices & Services and click the add button. Next choose the Message log option.
Available services: add, order_by, remove_message and show_message
Below is an example of how to add a message from an external system written in Python.
class MessageLevels(Enum):
"""Message levels."""
INFO = 10
ATTENTION = 20
WARNING = 30
ERROR = 40
def message_to_hass_log(
message: str,
message_level: MessageLevels | None = None,
icon: str | None = None,
remove_after: float | None = None,
notify: bool | None = None,
added_at: datetime | None = None,
) -> None:
"""Message to hass."""
headers = {
"Authorization": f"Bearer {HASS_TOKEN}",
"content-type": "application/json",
}
data: dict[str, Any] = {"message": message}
if message_level is not None:
data["message_level"] = message_level.name
if icon is not None:
data["icon"] = icon
if remove_after is not None:
data["remove_after"] = remove_after
if notify:
data["notify"] = notify
if added_at:
data["added_at"] = added_at.strftime("%Y-%m-%d %H:%M:%S")
post(URL, headers=headers, json=data, timeout=3)