How to add mqtt sensor?

I’m trying to add mqtt power sensor, however noting I tried works.
Homeassistant docs is garbage and suggests to add this:

mqtt:
  - sensor:
      state_topic: "obk5541802A/power"

this does not even pass config check
I found another way too add thins via sensor config

sensor:
  - platform: mqtt
    name: "Laptop Power"
    state_topic: "obk5541802A/power" 

But that does not appear to do anything, nothing appears in anywhere in home assistant

mqtt broker looks fine, and data is published:


Nothing interesting in the logs either.

What’s the proper way to add energy monitoring sensor via mqtt in HA?

Indentation is important

mqtt:
  - sensor:
    state_topic: "obk5541802A/power"

and probably you should use this

mqtt:
  - sensor:
    state_topic: "obk5541802A/power/get"

You found an outdated and no longer supported way.

The documentation is correct. As Francis says, your indention is not correct.

Please try to be more constructive. If you have suggestions for improvements to the documentation you can make them using the links at the bottom of every documentation page.

1 Like

But that’s the indentation in the documentation, so that should be fixed then, but anyway,
It does not work, config check says

Configuration invalid!
Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'sensor': None, 'state_topic': 'obk5541802A/power/get'}]. (See /config/configuration.yaml, line 36).

My full configuration.yaml would look like this:


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

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

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

influxdb:
  host: a0d7b954-influxdb
  port: 8086
  database: homeassistant
  username: homeassistant
  password: test
  max_retries: 3
  default_measurement: state
  include:
    entity_globs:
      - sensor.pzem*
  ignore_attributes:
    - device_class
    - device_class_str
    - friendly_name
    - friendly_name_str
    - state_class
    - state_class_str

mqtt:
  - sensor:
    state_topic: "obk5541802A/power/get"

But I won’t try to run with this config, if it fails to boot.
So I don’t know whats wrong here.

And that’s what I get for not checking. Apologies. It’s not the indentation.

What version of Home Assistant are you on?

I’m still running 2023.7.3, waiting for the end of the month to update.
Found proper syntex, it should be like this:

mqtt:
  sensor:
    name: "Laptop Power"
    state_topic: "obk5541802A/power"
    unit_of_measurement: "W"

However, now can’t figure out how multiple sensors cant be added, you cant duplicate sensor: tag and using different names seems to fail validation

Like this:

mqtt:
  sensor:
    - name: "Laptop Power"
      state_topic: "obk5541802A/power"
      unit_of_measurement: "W"

    - name: "Some Other Power Sensor"
      state_topic: "some/other/topic"
      unit_of_measurement: "W"

no, thats does not pass config check

I edited my post.

Still it’s not correct, full correct config for my case Energy,Power,Voltage current, ended like this:

mqtt:
  sensor:
    - name: "Laptop Power"
      state_topic: "obk5541802A/power/get"
      unit_of_measurement: "W"
      state_class: "measurement"
    - name: "Laptop Energy"
      state_topic: "obk5541802A/energycounter/get" 
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: "total_increasing"
    - name: "Laptop Voltage"
      state_topic: "obk5541802A/voltaget/get" 
      unit_of_measurement: "V"
      state_class: "measurement"
    - name: "Laptop Current"
      state_topic: "obk5541802A/current/get" 
      unit_of_measurement: "A"
      state_class: "measurement"

One dash made for such suffering :slight_smile:
I need to group those…