Pets waiting outside automation, using esp32+ vl53l0x

Hello everyone. I have a problem with my automation for pets waiting outside of the backyard door. I used t have a ld2410 sensor there, but it was a lot of falls alarms (rain, bee, and etc), so I decided to use TOF sensor VL53L0X with esp32. But cant figure it out how to make automation to trigger only if distance between 5cm and 90cm. I know, I should use a condition IF but not really good in coding like that.
Here is my esp32 code:

esphome:
  name: "pets-presence-2"
  friendly_name: Pets Presence-2

esp32:
  board: esp32dev
  framework:
    type: arduino
    version: recommended
# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "   "

ota:
  password: "   "

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Pets-Presence-2"
    password: "    "

captive_portal:

i2c:
  sda: 22
  scl: 21
  scan: true
  id: bus_a

sensor:
  - platform: vl53l0x
    name: "Pet Presence"
    icon: mdi:paw
    address: 0x29
    update_interval: 5s
    long_range: false

web_server:
  port: 80 

an my automation:

alias: Pet is waiting outside, Let me in
description: ""
trigger:
  - alias: Pet Outside
    platform: state
    entity_id:
      - sensor.pets_presence_2_pet_presence
    to: "on"
    id: Detected
    for:
      hours: 0
      minutes: 0
      seconds: 45
    from: unknown
    enabled: false
  - platform: state
    entity_id:
      - binary_sensor.pets_presence_1115h_occupancy_or_movement
    to: "off"
    alias: Clear
    id: Clear
    from: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    enabled: false
  - alias: Pet waiting Outside
    type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    below: 1
    id: Detected-2
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    above: 1
    alias: No pet around
    id: Clear-2
condition:
  - condition: time
    after: "07:30:00"
    before: "22:00:00"
  - condition: state
    entity_id: binary_sensor.back_door_contact
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Detected
        sequence:
          - service: notify.mobile_app_sm_s918u
            data:
              message: Пустите Домой, Пожалуйста
              data:
                priority: high
                ttl: 0
                channel: pets
                notification_icon: mdi:paw
          - service: tts.google_translate_say
            data:
              cache: false
              entity_id: media_player.masyanya
              message: Пустите Домой, Пожалуйста
              language: ru
    enabled: false
  - alias: Let me in
    if:
      - condition: trigger
        id:
          - Detected-2
    then:
      - service: notify.mobile_app_sm_s918u
        data:
          message: Пустите Домой, Пожалуйста
          data:
            priority: high
            ttl: 0
            channel: pets
            notification_icon: mdi:paw
      - service: tts.google_translate_say
        data:
          cache: false
          entity_id: media_player.masyanya
          message: Пустите Домой, Пожалуйста
          language: ru
    else: []
mode: single

It’s a lot of unused or disabled triggers and conditions, but project is in trials and mistakes stage :smiley:
Thank you in advanced for help

Anyone? Please.

Just add an and condition via the UI and check that the sensor is above 5 and below 90.

Add the condition to the overall conditions or in each choose. No logic required.

1 Like

Will try, I think I already did this and for some reason it was not working the way I wanted. Thank you. Will report back.

Good day
I did like you suggested Petro, but now it’s triggering even if no pets sitting in front of the sensor.

alias: Pet is waiting outside, Let me in
description: ""
trigger:
  - alias: Pet Outside
    platform: state
    entity_id:
      - sensor.pets_presence_2_pet_presence
    to: "on"
    id: Detected
    for:
      hours: 0
      minutes: 0
      seconds: 45
    from: unknown
    enabled: false
  - platform: state
    entity_id:
      - binary_sensor.pets_presence_1115h_occupancy_or_movement
    to: "off"
    alias: Clear
    id: Clear
    from: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    enabled: false
  - alias: Pet waiting Outside
    type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    below: 0.8
    id: Detected-2
    for:
      hours: 0
      minutes: 0
      seconds: 10
    above: 0.1
  - type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    above: 1
    alias: No pet around
    id: Clear-2
condition:
  - condition: time
    after: "07:30:00"
    before: "22:00:00"
  - condition: state
    entity_id: binary_sensor.back_door_contact
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Detected
        sequence:
          - service: notify.mobile_app_sm_s918u
            data:
              message: Пустите Домой, Пожалуйста
              data:
                priority: high
                ttl: 0
                channel: pets
                notification_icon: mdi:paw
          - service: tts.google_translate_say
            data:
              cache: false
              entity_id: media_player.masyanya
              message: Пустите Домой, Пожалуйста
              language: ru
    enabled: false
  - alias: Let me in
    if:
      - condition: trigger
        id:
          - Detected-2
    then:
      - service: notify.mobile_app_sm_s918u
        data:
          message: Пустите Домой, Пожалуйста
          data:
            priority: high
            ttl: 0
            channel: pets
            notification_icon: mdi:paw
      - service: tts.google_translate_say
        data:
          cache: false
          entity_id: media_player.masyanya
          message: Пустите Домой, Пожалуйста
          language: ru
    else: []
mode: single

So, I am asking for help again.
Any ideas what I am doing wrong and how to fix that
Thank you

You didn’t add the condition… Please try to add the condition. You’ve already added 2 other conditions to your conditions. This is no different than those… you can do it.

1 Like

@petro ,sorry, but I fill stupid. Can’t think of any other conditions to add to this automation. Please name one . I clean my code a little bit:

alias: Pet is waiting outside, Let me in
description: ""
trigger:
  - alias: Pet waiting Outside
    type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    below: 0.8
    id: Detected-2
    for:
      hours: 0
      minutes: 0
      seconds: 10
    above: 0.1
  - type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    above: 1
    alias: No pet around
    id: Clear-2
condition:
  - condition: time
    after: "07:30:00"
    before: "22:00:00"
  - condition: state
    entity_id: binary_sensor.back_door_contact
    state: "off"
action:
  - alias: Let me in
    if:
      - condition: trigger
        id:
          - Detected-2
    then:
      - service: notify.mobile_app_sm_s918u
        data:
          message: Пустите Домой, Пожалуйста
          data:
            priority: high
            ttl: 0
            channel: pets
            notification_icon: mdi:paw
      - service: tts.google_translate_say
        data:
          cache: false
          entity_id: media_player.masyanya
          message: Пустите Домой, Пожалуйста
          language: ru
    else: []
mode: single

Thank you

I thought it inside the first( trigger) part

image

I use the TOF sensor to tell me if the trashbin is present. I know it could be done in one automation, but why complicate things?

Here’s my automations:

alias: trashbin-present
description: Turns off a light if the trashbin distance is within one  meter
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.trashbin_vl53_2
    above: 0
    below: 0.51
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.trashbin_present
mode: single

alias: trashbin-gone
description: Turns off 'input_boolean.trashbin_present' if the bin is gone
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.trashbin_vl53_2
    above: 0.52
condition: []
action:
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.trashbin_present
mode: single

Well, I think it’s pretty much the same automation. I try to accomplish only one thing: if backyard door is closed and time between 7:30Am and 9:00PM and pet is seating outside, waiting to get inside, it’s send a command to Google hub to announce: Pets is outside, Let me in, and send notification to my phone with same message. Everything is working, except, it’s trigger even if no pet outside( I think it’s to sensitive or ???) Used to have a LD2410 but even havy rain was triggering this automation.

No, you need it as a condition.

Look down. Orient the sensor to be looking down where the pets are trained to sit.

like this?

alias: Pet is waiting outside, Let me in
description: ""
trigger:
  - type: value
    platform: device
    device_id: 1f8cbca11de7c664ed288495d885eeeb
    entity_id: 7465608af8cbbfc09e942d969b74629a
    domain: sensor
    above: 1
    alias: No pet around
    id: Clear-2
  - platform: numeric_state
    entity_id:
      - sensor.pets_presence_2_pet_presence
    for:
      hours: 0
      minutes: 0
      seconds: 15
    above: 0.1
    below: 0.9
    alias: Pets waiting outside
    id: Detected-2
condition:
  - condition: time
    after: "07:30:00"
    before: "22:00:00"
  - condition: state
    entity_id: binary_sensor.back_door_contact
    state: "off"
  - condition: numeric_state
    entity_id: sensor.pets_presence_2_pet_presence
    above: 0.1
    below: 0.9
action:
  - alias: Let me in
    if:
      - condition: trigger
        id:
          - Detected-2
          - Detected-3
    then:
      - service: notify.mobile_app_sm_s918u
        data:
          message: Пустите Домой, Пожалуйста
          data:
            priority: high
            ttl: 0
            channel: pets
            notification_icon: mdi:paw
      - service: tts.google_translate_say
        data:
          cache: false
          entity_id: media_player.masyanya
          message: Пустите Домой, Пожалуйста
          language: ru
    else: []
mode: single

but It’s same condition in trigger and in conditions
otherwise it’s not let me save this automation.

Sensor is on the floor level(outside on the door jam)