Cannot set certain sensor attributes for mqtt gas meter

Using the config from this topic I am trying to configure my gas meter so it shows up in the energy page. I could add my energy (peak and offpeak) usage to the new energy page, but gas consumption configuration is reporing ‘no matching statistics found’.

I am having problems adding the device_class and state_class attributes to the mqtt sensor.

File configuration.yaml contains:

sensor: !include_dir_merge_list sensors/

File sensors/DSMR-actueel.yaml contains:

- platform: mqtt
  name: "Gas Delivered"
  state_topic: "DSMR-WS/JSON/gas_delivered"
  unit_of_measurement: m³
  value_template: "{{ value_json.gas_delivered[0].value | round(2) }}"

This mqtt sensor was configured sometime ago and is reporting the total gas delivered correctly. The value_template with gas_delivered[0] seems redundant but the MQTT is reporting on this topic as following: DSMR-WS/JSON/gas_delivered {"gas_delivered":119.69,"unit":"m3"}.

When I change the above sensor to:

- platform: mqtt
  name: "Gas Delivered"
  state_topic: "DSMR-WS/JSON/gas_delivered"
  unit_of_measurement: m³
  value_template: "{{ value_json.gas_delivered[0].value | round(2) }}"
  device_class: gas
  state_class: total_increasing

The config is being edited via the Visual Studio Code add-on. I am getting configuration errors on the last two attributes:

  • state_class: Property state class is not allowed
  • device_class: Value is not accepted. Valid values: “battery”, “current”, “energy”, “humidity”, “illuminance”, “power”, “power_factor”, “pressure”, “signal_strength”, “temperature”, “timestamp”, “voltage”

I checked the docs: Sensor Entity | Home Assistant Developer Docs but I could not find a suggestion to properly set the new attributes. Is there something wrong with the configured sensor entity, or could it be this configuration error is related to Visual Studio Code (because the config is checked via the editor)?

My versions: Home Assistant OS 6.3 on core-2021.9.6, Visual Studio Code is on version 3.6.2.

The Visual Studio Code Add-on includes Home Assistant Config Helper which attempts to check the validity of configuration options. However, the Config Helper’s set of validation rules might sometimes be unaware of new features added to Home Assistant such as gas being a valid device_class and state_class is in fact a valid option for MQTT Sensor.

Until Config Helper is updated and the updated version is distributed in an updated version of Visual Studio Code Add-on, it will continue to report false positives.

The true test is to execute Configuration > Server Controls > Check Configuration (because its set of validation rules is up to date with the current version of Home Assistant).

Thanks for the hint regards Visual Studio Code + config helper! I managed to add the gas meter to Energy page, ignoring the errors/warning in Visual Studio Code. On the git repo for the config helper I noticed an open issue regarding the unknown attributes: MQTT Sensor unknown attributes · Issue #1516 · keesschollaart81/vscode-home-assistant · GitHub

1 Like