How to switch buzzer on if ultrasonic sensor value is less than x for y minutes within 1 hour

Hi,

I’m new to Home Assistant, ESP8266 and ESPHome. I have connected an ultrasonic sensor “HC-SR04” and a 2-pin-buzzer to my ESP8266. I can see correct values in the dashboard. The buzzer works, too.

But how can I use both in an automation?

First of all the config:

esphome:
  name: esp8266-ezig

esp8266:
  board: nodemcuv2

# Enable logging
logger:

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

ota:
  password: "gwergfrweg"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp8266-Ezig Fallback Hotspot"
    password: "rgerge"

captive_portal:

sensor:
  # HC-SR04
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: "E-Zig ESP8266 Ultrasonic Sensor"
    update_interval: 5s
    timeout: 4.0m

switch:
  - platform: gpio
    pin: D2
    name: "E-Zig Buzzer"

The automation should turn the buzzer switch on if the ultrasonic value is less then 0,2m for 5 minutes within one hour. Or in other words: The buzzer should peep if something is missing more than 5 minutes each hour.

Would much appreciate if I got code snippets or inspiration.

Edit:
If you ask why:
I vaporise too much e-cigarette when I’m sitting at my PC, namely every few minutes. The buzzer is to remind me that I can smoke for a maximum of 5 minutes per hour instead of 55 minutes per hour as before. So I just put the e-cigarette in front of the sensor whenever I’m not using it.
I will develop the project further and add a “snooze button” and also check whether I am at home at all so that the buzzer doesn’t beep non-stop.

There’s probably a way to do more of this locally on the ESP, but I would do it as follows.

Add a threshold sensor to your ESPHome config:

esphome:
  name: esp8266-ezig

esp8266:
  board: nodemcuv2

# Enable logging
logger:

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

ota:
  password: "gwergfrweg"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp8266-Ezig Fallback Hotspot"
    password: "rgerge"

captive_portal:

sensor:
  # HC-SR04
  - platform: ultrasonic
    id: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: "E-Zig ESP8266 Ultrasonic Sensor"
    update_interval: 5s
    timeout: 4.0m

switch:
  - platform: gpio
    pin: D2
    name: "E-Zig Buzzer"

binary_sensor:
  - platform: analog_threshold
    name: "E-Zig Resting"
    sensor_id: ultrasonic
    threshold: 0.2
    invert: true

Create a History stats sensor in HA

sensor:
  - platform: history_stats
    name: E-Zig Use
    entity_id: binary_sensor.e_zig_resting
    state: "off"
    type: time
    start: "{{ now() - timedelta(hours=1) }}"
    end: "{{ now() }}"

Time-based History Stats sensors return hours so your threshol is 0.083 hours…

trigger:
  - platform: numeric_state
    entity_id: sensor.e_zig_use
    above: 0.083
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.e_zig_buzzer

Thank you. I had to change some lines because of errors.

But nothing happens after one hour testing.

ezig.yaml:

esphome:
  name: esp8266-ezig

esp8266:
  board: nodemcuv2

# Enable logging
logger:

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

ota:
  password: "rgerg"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp8266-Ezig Fallback Hotspot"
    password: "rgerg"

captive_portal:

sensor:
  # HC-SR04
  - platform: ultrasonic
    id: ezig_ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: "E-Zig ESP8266 Ultrasonic Sensor"
    update_interval: 5s
    timeout: 4.0m

switch:
  - platform: gpio
    pin: D2
    name: "E-Zig Buzzer"

binary_sensor:
  - platform: analog_threshold
    name: "E-Zig Resting"
    sensor_id: ezig_ultrasonic
    threshold: 0.2
    filters:
      - invert

configuration.yaml:

sensor:
    # E-Zig History Stats
  - platform: history_stats
    name: E-Zig Use
    entity_id: binary_sensor.e_zig_resting
    state: "off"
    type: time
    start: "{{ now() - timedelta(hours=1) }}"
    end: "{{ now() }}"

Automation:

alias: Buzzer if I smoke too much
description: Buzzer if I smoke too much
trigger:
  - platform: numeric_state
    entity_id: binary_sensor.e_zig_use
    above: 0.083
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.e_zig_buzzer
mode: single

I had a typo in the automation… the trigger entity should be sensor.e_zig_use.

alias: Buzzer if I smoke too much
description: Buzzer if I smoke too much
trigger:
  - platform: numeric_state
    entity_id: sensor.e_zig_use
    above: 0.083
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.e_zig_buzzer
mode: single

No difference. :frowning: Can I provide some screenshots?

Sure, make sure to include traces from the automation if there are any and the info from the States tool for the history stats sensor.

The sensor was disconnected 2 days because the automation wasn’t working. I connected it this tomorrow, restarted HA and the automation worked once. I think that’s the problem. The automation is running once the threshold is reached and never again.

Current version of the automation:

alias: Buzzer if I smoke too much
description: Buzzer if I smoke too much
trigger:
  - platform: numeric_state
    entity_id: sensor.e_zig_use
    above: 0.083
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.e_zig_buzzer
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.e_zig_buzzer
mode: single

Would be nice if the automation would run every 10 second to check the values?! And the buzzer should stop immediately as I put the ecig in front of the sensor again.

I’m not allowed to post more than one embedded file per post because I’m a new user.