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.
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.
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
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.
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.