- This cannot display messages from bots due to Telegram's privacy policies.
- Only 255 characters can be displayed due to Home Assistant sensor limitations.
- This does not work with the Home Assistant Docker installation. You need to use the full Home Assistant OS or supervised installation with Add-ons support.
- A working Telegram bot.
- Your bot's API token and the chat/group ID you want to use.
- Telegram triggers an event when a new message is sent in the chat.
- Home Assistant receives the trigger and writes the message to a text file.
- A virtual sensor reads the last few lines of the text file.
- The sensor is then displayed on your dashboard.
-
Install the File Editor Add-on from Home Assistant Add-on Store.
-
In your Home Assistant
configfolder, create a new folder, e.g.,file_notifications. -
Inside that folder, create a new file, e.g.,
telegram_log.txt. -
Open
configuration.yamlusing the File Editor. -
Add the following lines to allow access to your log file:
homeassistant: allowlist_external_dirs: - "/your/path/to/txt/" - "/config/file_notifications/"
-
Go to Settings > Devices & Services > Integrations, search for File, and click Add Integration.
-
Choose Set up a notification service, enter the path to your log file, and click OK.
-
Go back to the File Editor and open
configuration.yamlagain. -
Add the following configuration for the Telegram bot, notification service, and the virtual sensor:
# Telegram Integration telegram_bot: - platform: polling api_key: "your_bot_api_token" allowed_chat_ids: - 123456789 # your personal chat ID - -1001234567890 # your group chat ID (note the minus) notify: - platform: telegram name: telegram chat_id: 123456789 # this should match one of the allowed_chat_ids - platform: file name: file filename: /config/file_notifications/telegram_log.txt timestamp: false sensor: - platform: command_line name: Telegram Virtual Sensor command: "tail -n 5 /config/file_notifications/telegram_log.txt" scan_interval: 5
π You can change the
tail -n 5to show more or fewer lines, but remember: sensor values are limited to 255 characters. -
Save the file and open
automations.yaml. -
Add the following automation to write Telegram messages to the log file:
# automations.yaml - alias: Telegram Receive β Write to File trigger: platform: event event_type: telegram_text action: - service: notify.send_message data: entity_id: notify.file message: > {{ now().strftime('%Y-%m-%d %H:%M:%S') }} - {{ trigger.event.data.user_id }}: {{ trigger.event.data.text }}
βοΈ You can remove the timestamp or user ID in the message to save characters.
-
Save all files and restart Home Assistant.
-
Open your Dashboard and add the
sensor.telegram_virtual_sensoras an entity card.
- If your sensor shows
unknown, try reducing the number of lines read in thetail -ncommand. - Check Settings > System > Logs for detailed error messages.
- Ensure your bot has the correct permissions and the text file exists at the specified path.
