Deep sleep mqtt for espcam

hi,
i’m trying to make an espcam which is controled by a binary sensor.
I would like to use mqtt but i can’t make it work.
nothing I try is working and i m loosing my mind and my sanity…
here’s the actual code:

in esphome :

substitutions:

  devicename: espcam0

  friendly_name: espcam0

esphome:

  name: $devicename

esp32:

  board: esp32dev

  framework:

    type: arduino

api:

  encryption:

    key: "BkAvA2SOBK0tnuiW2tQR/PnwfA79RYMyeGs3UZPin18="

# Enable logging

logger:

# Enable Home Assistant API

ota:

  password: "6b408b2147dd1cf8c45c82d3470229f3"

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails

  ap:

    ssid: "Espcamtest Fallback Hotspot"

    password: "DZrNdM4Oa8Li"

captive_portal:

esp32_camera:

  name: $friendly_name

  external_clock:

    pin: GPIO0

    frequency: 10MHz

  i2c_pins:

    sda: GPIO26

    scl: GPIO27

  data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]

  vsync_pin: GPIO25

  href_pin: GPIO23

  pixel_clock_pin: GPIO22

  power_down_pin: GPIO32

# Image/Video settings, https://esphome.io/components/esp32_camera.html

  max_framerate: 24 fps    # default: 10 fps, max 60

  idle_framerate: 0.2 fps # default: 0.1 fps - framerate for 'picture' in HA dashboard

  resolution: 800x600  

  jpeg_quality: 10

  vertical_flip: False

  horizontal_mirror: False

  contrast: 0 # default: 0, variable -2 to 2

  brightness: 0 # default: 0, variable -2 to 2

  saturation: 0 # default: 0, variable -2 to 2

# Server video/pictures, https://esphome.io/components/esp32_camera_web_server.html

esp32_camera_web_server:

  - port: 8080

    mode: stream

  - port: 8081

    mode: snapshot

sensor:

  - platform: wifi_signal

    name: $friendly_name Wifi signal

    update_interval: 10s

  - platform: uptime

    name: $friendly_name Uptime

text_sensor:

  - platform: version

    name: $friendly_name ESPHome Version

  - platform: wifi_info

    ssid:

      name: $friendly_name WiFi

switch:      

  - platform: restart

    name: $friendly_name restart

output:

#flashlight

  - platform: gpio

    pin: GPIO4

    id: gpio_4

#statuslight led 33 for wifi connect

  - platform: gpio

    pin:

      number: GPIO33

      inverted: True

    id: gpio_33

light:

#flashlight

  - platform: binary

    output: gpio_4

    name: $friendly_name light

#statuslight led 33 for wifi connect

  - platform: binary

    output: gpio_33

    name: $friendly_name wifi light state

deep_sleep:

  id: deep_sleep_0

  run_duration: 1 min

  sleep_duration: 1 min

 

binary_sensor:

  - platform: homeassistant

    id: prevent_deep_sleep0

    name: Prevent Deep Sleep0

    entity_id: input_boolean.deep_sleep_disable0

  - platform: homeassistant

    id: control_deep_sleep0

    name: control deep sleep0

    entity_id: input_boolean.deep_sleep_control0

 

mqtt:

  broker: !secret mqtt_broker

  username: !secret mqtt_username

  password: !secret mqtt_password

  discovery: True

  discovery_retain: True

  # ...

  on_message:

    - topic: espcam0/deep_sleep/deep_sleep_control0

      payload: "on"

      then:

        - deep_sleep.prevent: deep_sleep_0

    - topic: espcam0/deep_sleep/deep_sleep_control0

      payload: "off"

      then:

        - deep_sleep.allow: deep_sleep_0

in home assistant:

automation:
  - id: deep_sleep_control0_on
    alias: "Deep Sleep Control On"
    trigger:
      platform: state
      entity_id: binary_sensor.deep_sleep_control0
      from: 'off'
      to: 'on'
    action:
      - mqtt.publish:
          topic: espcam0/deep_sleep/deep_sleep_control0
          payload: "on"
          retain: true

  - id: deep_sleep_control0_off
    alias: "Deep Sleep Control Off"
    trigger:
      platform: state
      entity_id: binary_sensor.deep_sleep_control0
      from: 'on'
      to: 'off'
    action:
      - mqtt.publish:
          topic: espcam0/deep_sleep/deep_sleep_control0
          payload: "off"
          retain: true

  - id: deep_sleep_prevent
    alias: "Deep Sleep Prevent"
    trigger:
      platform: mqtt
      topic: espcam0/deep_sleep/deep_sleep_control0
      payload: "on"
    action:
      - logger.log: 'deep sleep interrompu'
      - deep_sleep.prevent: deep_sleep_0

  - id: deep_sleep_allow
    alias: "Deep Sleep Allow"
    trigger:
      platform: mqtt
      topic: espcam0/deep_sleep/deep_sleep_control0
      payload: "off"
    action:
      - logger.log: 'deep sleep autorisé'
      - deep_sleep.enter: deep_sleep_0
  deep_sleep_control0:
    name: control deep sleep0
    initial: off