Get mp4 file from MQTT message and send through telegram

I am using 2 Xiaomi yi 1080p cams, hacked with https://github.com/roleoroleo/yi-hack-MStar so I can recieve MQTT messages. My goal is to create a low cost alarm system and I have already set up HomeAssistant (venv), mosquitto MQTT and working Telegram config. But now I’m struggling with getting the required movie files from the camera and sending them.

My camera MQTT messages are as follows:
First, upon detection of motion the camera sends:
Message 595 received on camera/balcony/motion at 14:44 :
motion_start
QoS: 0 - Retain: false

This is followed by:
Message 596 received on camera/balcony/motion at 14:45 :
motion_stop
QoS: 0 - Retain: false

And lastly:

Message 597 received on camera/balcony/files at 14:46 :
{
“start”: “2020-03-16T13:27:51+0000”,
“end”: “2020-03-16T13:28:53+0000”,
“files”: [
“2020Y03M16D13H/28M00S60.mp4”,
“2020Y03M16D13H/27M50S10.mp4”
]
}
QoS: 0 - Retain: true

Now, I am able to scp / SSH into the camera and use
“scp root:pass@ip:/file/2020Y03M16D13H/28M00S60.mp4 telegramalert” to locally store my file in the telegramalert directory, but this is done manually.
Also, I can use telegram_bot.send_video service to send the file to myself… But this is also manually.
And then I delete the file, but also by hand.

I have created a template sensor with:

  • platform: mqtt
    name: balkoncamfilm
    state_topic: “camera/balcony/files”
    value_template: '{{ value_json[“files”] }} ’

But this outputs:

[‘2020Y03M16D13H/28M00S60.mp4’, ‘2020Y03M16D13H/27M50S10.mp4’]

My scripting / bash / shell command skills are not advanced enough to know where to go from here. Basically I want HomeAssistant to determine the 2 file names, download those from the camera and send them through telegram. But I don’t know how I should do this automatically.

Edit; and it appears sometimes there is even more motion files, longer duration of the event provides more video files:

[‘2020Y03M16D15H/38M00S60.mp4’, ‘2020Y03M16D15H/37M00S60.mp4’, ‘2020Y03M16D15H/36M00S60.mp4’, ‘2020Y03M16D15H/35M24S36.mp4’]

So I need to somehow break everything up in multiple files, ‘foreach’ loop.

Ok, so after some trial and error:
(vaalue on purpose for my own learning)

{% set vaalue_json = {
“start”: “2020-03-16T13:27:51+0000”,
“end”: “2020-03-16T13:28:53+0000”,
“files”: [
“2020Y03M16D13H/28M00S60.mp4”,
“2020Y03M16D13H/27M50S10.mp4”,
“2020Y03M16D13H/27M50S10.mp4”,
“2020Y03M16D13H/27M50S10.mp4”
]
}
%}

{{vaalue_json.files|count}}

Gives the correct amount of files that it needs to download / are sent in a message
Manually creating an list of values:

Values: {{ vaalue_json.files[0] }}
Values: {{ vaalue_json.files[1] }}
Values: {{ vaalue_json.files[2] }}

outputs for example
Values: 2020Y03M16D13H/28M00S60.mp4
Values: 2020Y03M16D13H/27M50S10.mp4
Values: 2020Y03M16D13H/27M50S10.mp4

Now I just need to create a for loop, that outputs ‘values’ for each in ‘count’ .

Apparantly that works with:

{% for value in vaalue_json.files %} {{value}}
{% endfor %}

Which now prints the list of files.

2 Likes

Great job @MoiZie !
I’m trying to do the same task and your post is very inspiring.
Where you able to recursively send the videos via Telegram ?