I’m very noob when comes to home automation, I’m trying to send a message to my android device via telegram. Now I’ve setup my configuration as follows. Most of the code is compiled together from someone else’s projects. because I don’t know anything about homeassistant,.
notify:
- platform: telegram
name: telegram
chat_id: xxxxxxxx
telegram_bot:
- platform: polling
api_key: yyyyyyyyyyy:some_thing_somet_hing_here.
allowed_chat_ids:
- xxxxxxxxxxx
mqtt:
#this is empty here
automation:
- alias: Doorbell_pressed_automation
trigger:
platform: mqtt
topic: dev/test
payload: 'Capture!'
action:
- service: automation.turn_off
entity_id: automation.doorbell_pressed_automation
- service: script.turn_on
entity_id: script.doorbell_pressed_script
- delay: "00:00:5"
- service: automation.turn_on
entity_id: automation.doorbell_pressed_automation
script:
doorbell_pressed_script:
alias: 'notify me'
sequence:
- service: notify.telegram
data:
title: Home Assistant Notification
message: --Doorbell Pressed--
data:
photo:
- url: http://192.168.0.103/images/image.jpg
caption: Doorbell View
dev_publish_on_script:
sequence:
- service: mqtt.publish
data: {"topic":"dev/test", "payload":"ON"}
test_telegram:
alias: 'test_telegram'
sequence:
- service: notify.telegram
data:
title: TEST Message
message: 'This is a test!'
data:
photo:
- url: http://192.168.0.103/images/image.jpg
that was all about the homeassistant configuration, now there is an mqtt script that runs with nohup, publishes to the dev/test and dev/camera. Here’s the script.
import RPi.GPIO as GPIO
import picamera
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import argparse
import signal
import sys
import time
import logging
import datetime
### camera
camera = picamera.PiCamera()
camera.vflip=True
#mqtt
broker = '192.168.0.103'
topic = 'dev/test'
mqttQos = 0
mqttRetained = False
# setup for taking input from switch
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
prev_input = 0
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
#
def on_message(client, userdata, msg):
payload = str(msg.payload.decode('ascii')) # decode the binary string
print(msg.topic + " " + payload)
process_trigger(payload)
client = mqtt.Client()
client.on_connect = on_connect # call these on connect and on message
client.on_message = on_message
client.username_pw_set(username='pi',password='user_pass') # need this
client.connect(broker)
client.loop_start() # run in background and free up main thread
def post_image():
print('Taking photo')
camera.capture('image.jpg')
file_name = 'image_' + str(datetime.datetime.now()) + '.jpg'
camera.capture(file_name) # time-stamped image # create an object for this file and then pass the object here
with open('image.jpg', "rb") as imageFile:
myFile = imageFile.read()
data = bytearray(myFile)
client.publish('dev/camera', data, mqttQos, mqttRetained) #
client.publish('dev/test', 'Capture!')
print(file_name + 'image published')
while True:
input = GPIO.input(17)
if ((not prev_input) and input):
post_image()
time.sleep(2)
prev_input = input
time.sleep(0.2)
This script takes the picture whenever I press the switch, saves it with a timestamp and image.jpg.
This is to be sent via the bot.
What I can’t figure out is, why isn’t telegram bot sending any notification/image/message on my device?
Any help is much appreciated.