Simple Noise / Sound sensor from USB microphone

My goal is to monitor baseline sound levels for an office space situated on a main road, and I decided to use a Respeaker microphone array I bought a while ago but didn’t have time to use.

The microphone is attached to the Raspberry Pi HASS is running on, and mounted into the Docker container by specifying devices: to include - /dev/snd:/dev/snd. You will want to turn off automatic gain control for your microphone.

I opted to use the command_line sensor, arecord, and sox to extract RMS level/peak into Home Assistant. You will need to replace default:CARD=ArrayUAC10 with the appropriate device via arecord -L, and you will need to expose arecord and sox to Home Assistant. In this case, I had to install the packages inside the HASS container with apk add alsa-utils sox, which will be lost when the container is recreated (future problem).

command_line:
  - sensor:
      name: "RMS level"
      command: arecord -q -f cd -c 5 -d 9 -D default:CARD=ArrayUAC10 - | sox -t .wav - -n stats 2>&1 | grep 'RMS lev' | awk '{print $4}'
      unit_of_measurement: "dB"
      scan_interval: 10
      state_class: measurement
  - sensor:
      name: "RMS peak"
      command: arecord -q -f cd -c 5 -d 9 -D default:CARD=ArrayUAC10 - | sox -t .wav - -n stats 2>&1 | grep 'RMS Pk' | awk '{print $4}'
      unit_of_measurement: "dB"
      scan_interval: 10
      state_class: measurement

Note that this output only retuns RMS values, which are negative values, not the positive dB/SPL values that we generally expect from measuring environmental sound levels. You will need to calibrate the microphone to known levels, and I will attempt to do so later today.

3 Likes