Sign inSign up

lordraw/hassgram

By lordraw

Updated 9 days ago

elegram bot to control Home Assistant: typed cmd, It/En nl, voice messages HomeAssistant own stt

Image
Internet of things
0

122

lordraw/hassgram repository overview

hassgram

A Telegram bot that drives Home Assistant — lights, temperatures and voice notes — speaking both Italian and English.

Ask it in whatever language you happen to be typing in: "turn on the light in the study" or "accendi la luce dello studio". It follows you, and answers in the same language, voice notes included.

Quick start

docker run -d --name hassgram --restart unless-stopped \
  -e HOME_ASSISTANT_API_URL="http://homeassistant.local:8123/api/" \
  -e HOME_ASSISTANT_API_ACCESS_TOKEN="<long-lived access token>" \
  -e TELEGRAM_BOT_TOKEN="<token from @BotFather>" \
  -e TELEGRAM_CHAT_ID="123456789" \
  lordraw/hassgram:latest
docker compose
services:
  hassgram:
    image: lordraw/hassgram:latest
    container_name: hassgram
    restart: unless-stopped
    env_file: .env

The bot only makes outbound connections (Telegram long polling, plus the Home Assistant REST API), so there is no port to publish and no volume to mount. It keeps nothing on disk; all state is a small in-memory cache. It runs as an unprivileged user (uid 10001).

Configuration

variablerequiredmeaning
HOME_ASSISTANT_API_URLyesREST API endpoint, e.g. http://homeassistant.local:8123/api/
HOME_ASSISTANT_API_ACCESS_TOKENyesHome Assistant long-lived access token
TELEGRAM_BOT_TOKENyesbot token from @BotFather
TELEGRAM_CHAT_IDrecommendedcomma-separated allow-list of chat ids. If empty the bot answers anyone — test only
BOT_LANGUAGEnostarting language of a new chat, it or en (default it)
HA_STT_ENTITYnospeech-to-text entity, e.g. stt.google_ai_stt. Autodetected when unset
STT_LANGUAGE_ITnolanguage tag for Italian voice notes (default it-IT)
STT_LANGUAGE_ENnolanguage tag for English voice notes (default en-US)

Get the access token from your Home Assistant profile page → Security → Long-lived access tokens. Get your chat id by messaging the bot and reading the log line it prints for unauthorised chats.

Commands

commandwhat it does
/lightsper-room summary plus a keyboard; tap a room to see its lights with toggles
/lights livingonly the lights matching "living"
/whatsonevery light currently on, grouped by room
/on studyturn on one light, a whole room, or the whole house with /on home
/off kitchenturn off; ambiguous names get a button choice
/temperaturetemperature and humidity for every room
/temperature bathroomjust that room
/state <name>state of any entity — sensors, switches, climate, anything
/language it|enpin the language of this chat

Every command has an Italian alias: /luci, /accese, /accendi, /spegni, /temperatura, /stato, /lingua. The name you use is itself a language signal: /lights answers in English, /luci in Italian.

Plain sentences work too — "turn everything off", "how warm is it in the bedroom?", "which lights are on" — and so does "home" as a stand-in for every room at once.

Voice notes

Send a voice message (or an audio file, or a video note) saying the same thing you would type. The bot transcribes it, echoes back what it understood, and runs it.

Transcription uses the speech-to-text engine already configured in Home Assistant (POST /api/stt/<entity_id>) — no extra service, no extra API key. Telegram's ogg/opus voice notes are passed through untouched, so no ffmpeg and no conversion are needed. Voice is transcribed in the chat's current language, so switch with /language (or just write a message in the other language) before recording.

If your Home Assistant has no stt. entity, typed commands keep working and voice notes get a polite explanation instead.

How it works

Room names are not exposed by the Home Assistant REST API, so the entity_id → area map is rendered by a Jinja template on the Home Assistant side and cached. Entity states are cached for 5 seconds and invalidated on every service call. Name matching is fuzzy across friendly name, entity id and room, so "luciCucina", "kitchen" and "cucina lights" all land on the same place.

Security notes

  • Set TELEGRAM_CHAT_ID. An empty allow-list means anyone who finds your bot can switch your lights.
  • The access token is a full-privilege Home Assistant credential — pass it via env_file/secrets rather than baking it into an image or a compose file in version control.
  • The container needs no privileges, no host network and no volumes.

Tags

  • latest — the current release.
  • X.Y.Z — built from the git tag of the same name, never overwritten.

Full documentation, the source and the issue tracker live at https://github.com/lordraw77/hassgram.

Tag summary

Content type

Image

Digest

sha256:53211b7c9

Size

46.6 MB

Last updated

9 days ago

docker pull lordraw/hassgram