Create mqqt sensor

Hi all !

I have a volume meter and I’m running it on a rpi using a script. That script publishes its reported values to my mqtt server.

It works as show in this screenshot :

As you can see, the mqtt topic is the following :

homeassistant/sensors/db_level

I am using “sensor: !include sensors.yaml” in my configuration.yaml to make things a little more readable.

Problem is, I can’t figure out how to create a sensor for this in my sensors.yaml file.

Can someone here help me out writing the right thing to add ? Goal is to get a history of volume levels around my house.

here is my full sensors.yaml file content so far : - platform: template sensors: current_temperature: unique_id: "cu - Pastebin.com

i know it might sound stupid, but while it wrks when simply adding this in my configuration file :

mqtt:
  sensor:
    - name: "noise_levels"
      state_topic: "homeassistant/sensors/db_level"
      unit_of_measurement: "dB"

it then works…

Can someone let me know how to format this to put it in my sensors.yaml file ?

Thanks !!!

You can’t, as it’s under mqtt: at the top level, not sensor:. I have this in my configuration.yaml:

mqtt:
  sensor: !include_dir_merge_list mqtt/sensors
  binary_sensor: !include_dir_merge_list mqtt/binary_sensors
  switch: !include_dir_merge_list mqtt/switches

and I would then put your sensor in mqtt/sensors/noise.yaml, for example, with the top two lines removed as mqtt: / sensor: are in the declaration above. So:

- name: "noise_levels"
  state_topic: "homeassistant/sensors/db_level"
  unit_of_measurement: "dB"

OK got it ! I was trying to add this in the wrong place. As I only have one manually added sensor. I believe i’ll keep this in my configuration.yaml for now then.

Another little question as I have you here :wink:

I’ve added this to my sensors :

- platform: history_stats
  name: number of illegal traffic noise levels last 24 hours
  entity_id: sensor.noise_levels
  type: count
  state: ">80"
  start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
  end: "{{ now() }}"

Idea is to count each event that gets reported for which the level is above 80dB. For some reason, I’ve added the sensor to my dashboard, but it doesn’t get incremented, for some reason.

also, very last one, is there a way to avoid having “flat horizontal” lines between two evens in the graph ? I’d rather see it drop back to 0 and only see the real events (the script only do report events above 80db to my mqtt server as you can see here:

def update():
    ret = dev.ctrl_transfer(0xC0, 4, 0, 0, 200)
    global dB
    dB = (ret[0] + ((ret[1] & 3) * 256)) * 0.1 + 30
    if dB >=80 :
        logging.info('%s, %.1f dB', datetime.now().strftime("%d-%m-%Y %H:%M:%S"), round(dB, 1))

I guess I simply want to display a graph of the events, and not a graph of the values, then I should be seeing “lines” when an event happens, that goes above 80dB at each time?

thanks again, you are always here to help, I appreciate this soooo much ! :slight_smile: