Integrating motion eye with hass by MQTT

Hi !

I’m quite a programming newby somebody can put me in the right direction. What I’m trying to accomplish is a python script which is ran on motion from motioneye to do the following :

  • publish a MQTT message that there is motion
  • publish a motion picture over MQTT when motion starts
  • publish a motion video over MQTT when motion ends (if file small enough)

and I want to accomplish this without the need to install things like mosquitto pub. For this reason I try to use the MQTT.publish service from home assistant. It’s not a problem to publish a message that there is motion, but I can’t succeed to send the raw jpg file through this service.

When I do this I get the following error :

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/real_ip.py", line 40, in real_ip_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 73, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 231, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 128, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/api/__init__.py", line 346, in post
    body = await request.text()
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_request.py", line 579, in text
    return bytes_body.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 13: invalid start byte

I tried to just encode the jpg as utf-8 but than the jpg won’t work withing hass… I also tried to encode as base64 but than hass also doesn’t understand the image (the MQTT camera).

I took a look how dafang hacks does it, but they just use mosquitto_pub with the -t parameter. I could install this manually but I’m trying to make this a more usefull solution which also works for hassos users etc.

I’m not sure if this is a great approach anyway, maybe a custom_component is more usefull which just checks the motion eye folder for latest images and publishes these… Please advice!

My current code can be found here : https://github.com/kloknibor/Motioneye-to-hass/blob/bbddabfb5d3acc462fde0bf2251759c1b74bb522/run_on_motion.py

thanks!