MotionEye > MQTT > Binary Sensor Motion

hi all, I’ve been going around in circles for days with MQTT / MotionEye / Motion Binary Sensor. I think I have missed something with the documentation which I’m finding difficult to follow. my use case is that I want to send a moquito publish command to mqtt on home assistant when motion is detected in motioneye. I am using the following command:

mosquitto_pub -h 192.168.1.XXX -p 1883 -u YYY -P ZZZ -t cameras/1/motion -m ON

The contents of my mqtt.yaml file is as follows:

binary_sensor:
  ### motioneye motion sensor ###
  - name: "Front Porch Motion"
    unique_id: front_porch_motion
    state_topic: "cameras/1/motion"
    device_class: motion

Do I have to send a config for that topic to mqtt at first before sending the state commands? I’m really not clear from the mqtt docs how I do that? I have set the discovery topic to cameras instead of homeassistant to try and force the discovery of this topic.
When I set the payload to ON manually on this topic using the MQTT integration Publish a packet feature, my binary sensor updates it’s state to motion detected, but when I test the mqtt publish command from motioneye, I am never able to get the binary sensor to change its state. This makes me think that my command above is wrong somehow, or I do not have the configuration of MQTT set in a way that allows this published command to be received by the broker.

Please can anyone help? Unfortunately many of the community forum posts are from before the change to mqtt config which is making things even more confusing.

binary_sensor:
  ### motioneye motion sensor ###
  - name: "Front Porch Motion"
    unique_id: front_porch_motion
    state_topic: "cameras/1/motion"
    payload_on: "ON"
    payload_off: "OFF"
    device_class: motion

would be one way to go so the actual Topic (cameras/1/motion) has the payload “ON”

lets u think motion eye sends an “OFF” payload too .

have a try

Thanks for this. I tried these payload_on and off entries already, but unfortunately they didn’t make a difference. The sensor remained in an unavailable state.

wouldn’t it be easier with a webhook?

Thanks for this suggestion. I’ve definitely found webhooks easier for the notifcation automation which I’ve created. But I haven’t been able to get the webhook trigger working with the binary sensor. What is the difference between using Post Query and Post JSON, I had mainly been testing with Post JSON. But I’m not sure how to read that json or query for debugging.

Working automation for notification

- id: 'xxxxxxx'
  alias: MotionEye webhook motion notification
  description: ''
  trigger:
  - platform: webhook
    allowed_methods:
    - POST
    - PUT
    local_only: true
    webhook_id: motioneyeonXX
  condition: []
  action:
  - service: notify.mobile_app_iphone
    data:
      message: Motion detected on Front Porch
      data:
        entity_id: camera.camera_name
        actions:
        - action: URI
          title: Open Camera
          uri: /lovelace/cameras

Binary sensor yaml file contents (have added binary sensor yaml in config yaml too)

- trigger:
    - platform: webhook
      webhook_id: motioneyeonXX
  binary_sensor:
    - name: "MotionEye Porch Motion"
      state: "{{ trigger.json.status == 'on' }}"
      device_class: motion

Ideally I’d like a binary sensor which triggers a binary sensor state change based on both the motioneyeonXX and motioneyeoffYY webhooks. But my templating isn’t good enough to figure this out.

I created a template with as below which changes based on the motioneye POST JSON body.

- trigger:
    - platform: webhook
      webhook_id: abc-123-xyz
  binary_sensor:
    - name: "Entrance Lobby Motion"
      unique_id: entrance_lobby_motion
      state: >
        {%- if trigger.json.changed_pixels != "0" -%}
        {%- set motion = true -%}
        {%- endif -%}
        {%- if trigger.json.changed_pixels == "0" -%}
        {%- set motion = false -%}
        {%- endif -%}
        {{- motion -}}
      device_class: motion

The webook is called on both motion detected and motion stopped action