Here's how I've implemented something similar using HA (Home Assistant) and a Telegram Bot:
Initial setup should be as simple as:
- Install HA (I first installed it before "hass.io" existed, using the simple process of creating a Python 3 virtualenv and running
pip install homeassistant
, but check the latest installation instructions and choose the method you prefer)
- Configure the HA components Telegram, Telegram chatbot, GPIO binary sensor, and GPIO switch.
By the end of this process you should have:
- A working installation of HA, accessible on your local network at the address
http://<Pi's IP address>:8123/
.
- Confirmed your GPIO switch is visible on the front page and that switching it on and off functions correctly.
- Verified, using Developer Tools -> Services, that you can send a notification to your Telegram bot, using the service called
notify.bluebot
(replace "bluebot" with the name you configured), and then receive that message from your Telegram bot.
- Verified, using Developer Tools -> States, that your GPIO status (
binary_sensor.gpio_port
or whatever you called it) is visible and correct.
Now create two HA automations, one which will send you the value of the GPIO sensor, and the other which will toggle the status of the switch.
You can create these in the HA UI, navigate to Configuration -> Automation, and click the round orange plus button to add a new one.
- Get sensor value
- Triggers
- Trigger type:
Event
- Event type:
telegram_command
- Event data:
{"command": "/get"}
- Conditions
- Actions
- Action type:
Call service
- Service:
notify.bluebot
- Service data:
{"message": "{{ states('binary_sensor.gpio_port') }}"}
- Set sensor value
- Triggers
- Trigger type:
Event
- Event type:
telegram_command
- Event data:
{"command": "/set"}
- Conditions
- Actions
- Action type:
Call service
- Service:
switch.toggle
- Service data:
{"entity_id": "switch.gpio_port"}
(Replace the names of the entities that I've used with the names you've assigned)
Now test that you can send your Telegram bot the command /get
and have it respond with the status of your GPIO port, and send the command /set
to have it toggle the GPIO switch.