I created a small Node.js library (mainly for myself) to create Telegram bots for Home Assistant with very little and clean code.
A simple bot looks like this:
var Bot = require('home-assistant-telegram-bot').Bot
var bot = new Bot({
token: '<Telegram bot token>',
url: '<Home Assistant url>',
})
bot.on('/temp', function(chat, hass) {
chat.send(' Temp: {{sensor.temperature.state}} °C ')
})
bot.on('/light', function(chat, hass) {
hass.switch.light1.toggle()
chat.send('Ok')
})
bot.observeState('sensor.wind', function(state, hass) {
if (state > 10) {
bot.sendToAll('It`s windy!')
}
})
As you can see, it can do three things:
- Send values from Home Assistant sensors
- Control HomeAssistant devices
- Observe a state and inform the user when it changes
The library is available on Github and via npm.
Maybe it’s useful to someone.