Repository: blakeblackshear/frigate

Issue:
[Support]: motion sensor stays on indefinitely after restart
### Describe the problem you are having

The following can be reliably reproduced in my setup:

- I enter the room, frigate's motion sensor (`binary_sensor.<camera>_motion`) turns `on` 
- I restart the frigate add-on
- I leave the room while frigate is restarting.

During the restart `binary_sensor.<camera>_motion` becomes `unavailable`, and after the restart turn back to `on` and it remains on indefinitely.

More restarts do not affect the sensor. The only way to turn in back `off` is to enter the room again, cause a new motion detection, and wait for the new detection to finish.

Here's a log of the MQTT messages sent during this procedure:
```
2022-07-15 11:04:28 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/motion: b'ON'
2022-07-15 11:04:39 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/available: b'offline'
2022-07-15 11:04:40 ERROR (MainThread) [custom_components.frigate.api] Error fetching information from http://ccab4aaf-frigate-fa-beta:5000/api/stats: Cannot connect to host ccab4aaf-frigate-fa-beta:5000 ssl:default [Connect call failed ('172.30.33.2', 5000)]
2022-07-15 11:04:40 ERROR (MainThread) [custom_components.frigate] Error fetching frigate data: 
2022-07-15 11:04:45 ERROR (MainThread) [custom_components.frigate.api] Error fetching information from http://ccab4aaf-frigate-fa-beta:5000/api/stats: 502, message='Bad Gateway', url=URL('http://ccab4aaf-frigate-fa-beta:5000/api/stats')
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/recordings/state: b'OFF'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/snapshots/state: b'OFF'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/detect/state: b'OFF'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/motion/state: b'ON'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/available: b'online'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/recordings/state: b'OFF'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/snapshots/state: b'OFF'
2022-07-15 11:04:46 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/detect/state: b'OFF'
2022-07-15 11:04:48 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on frigate/frigate_livingroom/all: b'0'
```
If I understand correctly:
- `frigate/<cameta>/motion` sets the state of `binary_sensor.<camera>_motion`
- `frigate/<cameta>/motion/state` sets the state of `switch.<camera>_motion`

It looks like after a restart, frigate sends a message to inform HA about the state of the `switch`, but not about the `binary_sensor`, which stays on until a future detection.





### Version

0.11.0-ef54cd6

### Frigate config file

```yaml
mqtt:
  host: 192.168.2.5
  user: mqtt_user
  password:

detectors:
  cpu1:
    type: cpu
    num_threads: 2

cameras:
  frigate_livingroom:
    ffmpeg:
      inputs:
        - path: "rtsp://<ip>/stream2"
          roles:
          - detect
        - path: "rtsp://<ip>/stream1"
          roles:
          - rtmp
          - record
    motion:
      mask:
        - 235,23,233,0,0,0,0,22
    detect:
      width: 640
      height: 360
      fps: 5
      enabled: False

record:
  enabled: False
  retain:
    days: 1

objects:
  track: []
```


### Relevant log output

```shell
[2022-07-15 10:42:29] frigate.app                    INFO    : Starting Frigate (0.11.0-ef54cd6)
[2022-07-15 10:42:29] frigate.app                    INFO    : Creating directory: /tmp/cache
Starting migrations
[2022-07-15 10:42:29] peewee_migrate                 INFO    : Starting migrations
There is nothing to migrate
[2022-07-15 10:42:29] peewee_migrate                 INFO    : There is nothing to migrate
[2022-07-15 10:42:29] frigate.app                    INFO    : Output process started: 219
[2022-07-15 10:42:29] detector.cpu1                  INFO    : Starting detection process: 218
[2022-07-15 10:42:29] frigate.edgetpu                WARNING : CPU detectors are not recommended and should only be used for testing or for trial purposes.
[2022-07-15 10:42:29] frigate.app                    INFO    : Camera processor started for frigate_livingroom: 222
[2022-07-15 10:42:29] ws4py                          INFO    : Using epoll
[2022-07-15 10:42:29] frigate.app                    INFO    : Capture process started for frigate_livingroom: 225
[2022-07-15 10:42:30] ws4py                          INFO    : Using epoll
```


### FFprobe output from your camera

```shell
ffprobe version 4.2.7-0ubuntu0.1 Copyright (c) 2007-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  WARNING: library configuration mismatch
  avcodec     configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libaribb24 --enable-liblensfun --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0
  Metadata:
    title           : Session streamed by "TP-LINK RTSP Server"
    comment         : stream2
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 640x360, 15 fps, 20 tbr, 90k tbn, 30 tbc
    Stream #0:1: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
```


### Frigate stats

_No response_

### Operating system

Debian

### Install method

HassOS Addon

### Coral version

CPU (no coral)

### Network connection

Wireless

### Camera make and model

TP-LINK Tapo C210

### Any other information that may be helpful

_No response_


Discussion:
Thanks for the detailed issue! Will double check and add fix today 👍

Resolving patch:
diff --git a/frigate/mqtt.py b/frigate/mqtt.py
--- a/frigate/mqtt.py
+++ b/frigate/mqtt.py
@@ -314,6 +314,11 @@ def on_connect(client, userdata, flags, rc):
             config.cameras[name].motion.contour_area,
             retain=True,
         )
+        client.publish(
+            f"{mqtt_config.topic_prefix}/{name}/motion",
+            "OFF",
+            retain=False,
+        )
 
     return client
 


Test patch: