Created
April 18, 2023 10:56
-
-
Save peyanski/48691397d4cb1acfc4af56c0232342e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esphome: | |
name: soundsensor | |
friendly_name: SoundSensor | |
esp8266: | |
board: d1_mini | |
# Enable logging | |
logger: | |
# Enable Home Assistant API | |
api: | |
ota: | |
wifi: | |
ssid: !secret wifi_ssid | |
password: !secret wifi_password | |
# Enable fallback hotspot (captive portal) in case wifi connection fails | |
ap: | |
ssid: "Soundsensor Fallback Hotspot" | |
password: "subscribe" | |
captive_portal: | |
# Example configuration entry | |
web_server: | |
port: 80 | |
globals: | |
- id: esphome_sensitivity | |
type: float | |
initial_value: '36.5' | |
restore_value: yes | |
- id: esphome_volume | |
type: int | |
sensor: | |
- platform: adc | |
pin: A0 | |
id: esphome_db | |
device_class: signal_strength | |
name: "Db SoundEsp" | |
icon: "mdi:volume-vibrate" | |
unit_of_measurement: "db" | |
update_interval: 2s | |
raw: true | |
filters: | |
- lambda: |- | |
unsigned int sample; | |
unsigned long startMillis= millis(); | |
float peakToPeak = 0; | |
unsigned int signalMax = 0; | |
unsigned int signalMin = 1024; | |
while (millis() - startMillis < 500) { | |
sample = analogRead(A0); | |
if (sample < 1024){ | |
if (sample > signalMax){ | |
signalMax = sample; | |
} | |
else if (sample < signalMin){ | |
signalMin = sample; | |
} | |
} | |
} | |
peakToPeak = map((signalMax - signalMin),1,1024,1.5,1024); | |
id(esphome_volume) = peakToPeak; | |
float state = id(esphome_sensitivity)*log10(peakToPeak)+15; | |
return(state); | |
- platform: template | |
name: "Volume SoundEsp" | |
icon: "mdi:volume-high" | |
unit_of_measurement: "%" | |
update_interval: 2s | |
lambda: return(map((id(esphome_db).state),15,150,0,100)); | |
- platform: template | |
name: "RAW SoundEsp" | |
icon: "mdi:volume-source" | |
unit_of_measurement: "%" | |
update_interval: 2s | |
lambda: return(map(id(esphome_volume),1,1024,0,100)); | |
number: | |
- platform: template | |
id: sensitivity_slider | |
name: "Sensitivity SoundEsp" | |
icon: "mdi:knob" | |
update_interval: 5s | |
initial_value: "36.5" | |
step: 0.1 | |
min_value: 20 | |
max_value: 40 | |
mode: slider | |
set_action: | |
then: | |
- lambda: id(esphome_sensitivity) = x; |
@Lice2 Did you find a solution? Having the same problem.
No, unfortunately not.
I now have an outlet in front of my dryer that I monitor.
Nevertheless, seems to be working. It's a known issue. Thanks for answering.
Yes gladly. I then gave up on the sound sensor.
@Lice2 I assume that error is from the code in the lambda while (millis() - startMillis < 500)
which looks like it will block for half a second. You could try changing that 500 to 20, which will probably get rid of the warning - but it means the sensor would only be listening for 20ms every update_interval
seconds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I have a problem with this script.
I used an ESP32 nodemcu-32s.
And get these messages.
[17:04:12][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s).
[17:04:12][W][component:215]: Components should block for at most 20-30ms.
Is there a solution for that?
Mein Code
The Log:
[17:11:05][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:06][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:06][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:06][W][component:215]: Components should block for at most 20-30ms. [17:11:06][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:07][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:08][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state 15.00000 db with 2 decimals of accuracy [17:11:08][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:08][W][component:215]: Components should block for at most 20-30ms. [17:11:08][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:09][D][sensor:094]: 'Lautstärke BadOben': Sending state 0.00000 % with 1 decimals of accuracy [17:11:10][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:10][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:10][W][component:215]: Components should block for at most 20-30ms. [17:11:10][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:11][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:12][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:12][W][component:214]: Component adc.sensor took a long time for an operation (0.50 s). [17:11:12][W][component:215]: Components should block for at most 20-30ms. [17:11:12][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:13][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:14][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:14][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:14][W][component:215]: Components should block for at most 20-30ms.