Need help integrating MQTT sensors

Hello
The latest version of HA announces a new integration of MQTT sensors. I still don’t understand how it is done.
Here is my old binding. Can you help me with the new integration of the MQTT sensors?

sensor Gartenwasser2:
  - platform: mqtt
    state_topic: "Pumpenschacht/Steuerung/Temperatur"
    name: "Pumpenschacht - Temperatur im Schaltkasten - Hauswand-Kies"
    unique_id: pumpenschacht_temp_schaltkasten
    unit_of_measurement: "°C"
    device_class: power
  - platform: mqtt
    state_topic: "Pumpenschacht/Wasser/Volumen"
    name: "Pumpenschacht - Wasservolumen im Schacht"
    unique_id: pumpenschacht_wasservolumen_im_schacht
    unit_of_measurement: "Liter"
    value_template: '{{ value | round(0) }}'
    device_class: power
  - platform: mqtt
    state_topic: "Pumpenschacht/Switch1/Switch1"
    name: "Pumpenschacht - Schalter 1"
    unique_id: pumpenschacht_schalter1
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power

sensor Testumgebung:
  - platform: mqtt
    state_topic: "Testumgebung/BMP280/Pressure"
    name: "Testumgebung - Druck"
    unique_id: testumgebung_druck
    unit_of_measurement: "hPa"
    value_template: '{{ value | round(1) }}'
    device_class: power

It should look something like this:

# Example configuration.yaml entry
mqtt:
  sensor:
    - state_topic: "home/bedroom/temperature"

But what does it have to look like if you have several sensors.

Like this:

mqtt:
  sensor:
  - state_topic: "Pumpenschacht/Steuerung/Temperatur"
    name: "Pumpenschacht - Temperatur im Schaltkasten - Hauswand-Kies"
    unique_id: pumpenschacht_temp_schaltkasten
    unit_of_measurement: "°C"
    device_class: power
  - state_topic: "Pumpenschacht/Wasser/Volumen"
    name: "Pumpenschacht - Wasservolumen im Schacht"
    unique_id: pumpenschacht_wasservolumen_im_schacht
    unit_of_measurement: "Liter"
    value_template: '{{ value | round(0) }}'
    device_class: power
  - state_topic: "Pumpenschacht/Switch1/Switch1"
    name: "Pumpenschacht - Schalter 1"
    unique_id: pumpenschacht_schalter1
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power
  - state_topic: "Testumgebung/BMP280/Pressure"
    name: "Testumgebung - Druck"
    unique_id: testumgebung_druck
    unit_of_measurement: "hPa"
    value_template: '{{ value | round(1) }}'
    device_class: power

Many thanks for the quick response.
I have integrated a switch as a normal sensor. Isn’t it better to include it as a binary sensor? “1” and “0” are reported via MQTT.
Here is my integration so far:

 - state_topic: "Pumpenschacht/Switch1/Switch1"
    name: "Pumpenschacht - Schalter 1"
    unique_id: pumpenschacht_schalter1
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power

How is the switch integrated as a binary sensor?

Should all sensors be collected under mqtt: sensor: or does it make sense to group them?

The new mqtt format in general for a sensor would look something like:

mqtt:
  binary_sensor:
    - binary sensor 1 stuff here
    - binary sensor 2 stuff here
  sensor:
    - sensor 1 stuff here
    - sensor 2 stuff here

In your case where you want you sensor turned into a binary_sensor, I’ll give you an example of one of my mqtt binary sensors:

mqtt:
  binary_sensor:
    #FortrezZ Water Leak Detector
    - name: "Attic Water Leak"
      device_class: 'problem'  
      state_topic: "z2mqtt/attic_sensor/water_leak"
      payload_on: "active"
      payload_off: "inactive"
      value_template: '{% if value == "2" -%}active{%- else -%}inactive{%- endif %}'

As you can see, if the mqtt payload is 2 I declare the binary sensor as active, otherwise declare it as inactive.

1 Like

I tried to incorporate your hints into my configuration.yaml. (Only the one with the binary sensor is still missing)
My system is still quite small and still under construction.
I would like to ask you guys to check if everything is up to date.

My configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate


automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


mqtt:
  sensor:
  - state_topic: "Pumpenschacht/Steuerung/Temperatur"
    name: "Pumpenschacht - Temperatur im Schaltkasten - Hauswand-Kies"
    unique_id: pumpenschacht_temp_schaltkasten
    unit_of_measurement: "°C"
    device_class: power
  - state_topic: "Pumpenschacht/Steuerung/Druck"
    name: "Pumpenschacht - Druck im Schaltkasten - Hauswand-Kies"
    unique_id: pumpenschacht_druck_im_schaltkasten
    unit_of_measurement: "hPa"
    device_class: power
  - state_topic: "Pumpenschacht/Steuerung/Spannung"
    name: "Pumpenschacht - Spannung im Schaltkasten - Hauswand-Kies"
    unique_id: pumpenschacht_spannung_im_schaltkasten
    unit_of_measurement: "Volt"
    device_class: power
  - state_topic: "Pumpenschacht/Steuerung/Temperatur_Schacht"
    name: "Pumpenschacht - Temperatur im Schacht"
    unique_id: pumpenschacht_temperatur_im_schacht
    unit_of_measurement: "°C"
    device_class: power
  - state_topic: "Pumpenschacht/Wasser/Hoehe"
    name: "Pumpenschacht - Wasserstand im Schacht"
    unique_id: pumpenschacht_wasserstand_im_schacht
    unit_of_measurement: "Meter"
    device_class: power
  - state_topic: "Pumpenschacht/Wasser/Volumen"
    name: "Pumpenschacht - Wasservolumen im Schacht"
    unique_id: pumpenschacht_wasservolumen_im_schacht
    unit_of_measurement: "Liter"
    value_template: '{{ value | round(0) }}'
    device_class: power
  - state_topic: "Pumpenschacht/Switch1/Switch1"
    name: "Pumpenschacht - Schalter 1"
    unique_id: pumpenschacht_schalter1
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power
  - state_topic: "Pumpenschacht/Switch2/Switch2"
    name: "Pumpenschacht - Schalter 2"
    unique_id: pumpenschacht_schalter2
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power
  - state_topic: "Pumpenschacht/Switch3/Switch3"
    name: "Pumpenschacht - Schalter 3"
    unique_id: pumpenschacht_schalter3
    unit_of_measurement: " "
    value_template: '{{ value | round(0) }}'
    device_class: power    

  - state_topic: "Testumgebung/BMP280/Pressure"
    name: "Testumgebung - Druck"
    unique_id: testumgebung_druck
    unit_of_measurement: "hPa"
    value_template: '{{ value | round(1) }}'
    device_class: power

sensor SensorMinimum:
  - platform: statistics
    name: "Wasserstand Minimum über 4 Stunden"
    entity_id: sensor.pumpenschacht_wasservolumen_im_schacht   #wasserstand_minimum
    state_characteristic: value_min
    sampling_size: 1440


input_number:
  slider1:
    name: Slider
    initial: 100
    min: 50
    max: 400
    step: 10
  box1:
    name: Numeric Input Box
    initial: 30
    min: -20
    max: 35
    step: 1
    mode: box
  number1:
    min: 0 
    max: 100
    step: 10
    unit_of_measurement: "%"
    mode: slider



climate:
- platform: generic_thermostat
  name: Test-pumpe-wasser-halten
  heater: switch.switch_pumpe_seite_strasse_deaktivieren
  target_sensor: sensor.pumpenschacht_wasservolumen_im_schacht
  min_temp: 150
  max_temp: 400
  target_temp: 300
  hot_tolerance: 20
  initial_hvac_mode: "off"
  precision: 1
  ac_mode: false

 
input_select:
  pumpenmodus_test:
    name: Pumpenmodus_test
    options:
      - Winter
      - Automatische Bewässerung
      - Wasser sammeln

  pumen_modus:
    name: Pumpenmodus
    options:
      - Normalzustand
      - Automatische Bewässerung der Beete oder Vorgarten
      - Wasser sammeln
    initial: Normalzustand
    icon: mdi:water-pump  


I’ll just add, for your sensors, there are lots of device_class to choose from.