Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing device_class and state_class in "sub-"power of energy-meter #26028

Closed
xs400dohc opened this issue Jan 27, 2025 · 1 comment
Closed

Missing device_class and state_class in "sub-"power of energy-meter #26028

xs400dohc opened this issue Jan 27, 2025 · 1 comment
Labels
problem Something isn't working

Comments

@xs400dohc
Copy link
Contributor

xs400dohc commented Jan 27, 2025

What happened?

I have an energy meter integrated via Zigbee2mqtt (type: Tuya PC311-Z-TY).

The "main"-power-entity (object_id: <friendly_name>_power) has "device_class: power"and "state_class: measurement". The "sub"-power-entityties (object_id: <friendly_name>_power_a and <friendly_name>_power_b) does not. Same issue with current ("main"-current with device_class and state_class, "sub"-current without).

The energy-entities ("main"- and "sub"-entities of energy and energy_produced) have all "device_class: energy" and "state_class: total_increasing".

Following is from settings->devices->devices->device-info->MQTT INFO:
Topic: homeassistant/sensor/<unique_id>/power/config shows payload:

availability:
  - topic: zigbee2mqtt/bridge/state
    value_template: '{{ value_json.state }}'
  - topic: zigbee2mqtt/<friendly_name>/availability
    value_template: '{{ value_json.state }}'
availability_mode: all
device:
  hw_version: 1
  identifiers:
    - zigbee2mqtt_<unique_id>
  manufacturer: Tuya
  model: Bidirectional energy meter with 80A current clamp
  model_id: PC311-Z-TY
  via_device: zigbee2mqtt_bridge_<unique_id>
  name: <friendly_name>
device_class: power
enabled_by_default: true
entity_category: diagnostic
object_id: <friendly_name>_power
origin:
  sw_version: 2.0.0
  name: Zigbee2MQTT
  support_url: https://www.zigbee2mqtt.io
state_class: measurement
state_topic: zigbee2mqtt/<friendly_name>
unique_id: <unique_id>_power_zigbee2mqtt
unit_of_measurement: W
value_template: '{{ value_json.power }}'

Topic: homeassistant/sensor/<unique_id>/power_a/config shows:

availability:
  - topic: zigbee2mqtt/bridge/state
    value_template: '{{ value_json.state }}'
  - topic: zigbee2mqtt/<friendly_name>/availability
    value_template: '{{ value_json.state }}'
availability_mode: all
device:
  hw_version: 1
  identifiers:
    - zigbee2mqtt_<unique_id>
  manufacturer: Tuya
  model: Bidirectional energy meter with 80A current clamp
  model_id: PC311-Z-TY
  via_device: zigbee2mqtt_bridge_<unique_id>
  name: <friendly_name>
                                                                                             <-----NO DEVICE_CLASS HERE
enabled_by_default: true
object_id: <friendly_name>_power_a
origin:
  sw_version: 2.0.0
  name: Zigbee2MQTT
  support_url: https://www.zigbee2mqtt.io
                                                                                             <-----NO STATE_CLASS HERE
state_topic: zigbee2mqtt/<friendly_name>
unique_id: <unique_id>_power_a_zigbee2mqtt
unit_of_measurement: W
value_template: '{{ value_json.power_a }}'
name: Power a

I'm not sure, if it's an issue for MQTT or for ZIGBEE2MQTT/zigbee-herdsman, so I will open an issue in both.
If the issue is wrong here, please close it. If you are having the same problem and this issue is closed, have a look on the other github.

Found this issue while creating a statistics-graph on a dashboard and only 1 entity (helper of sum of three "sub"-entities (line1+line2+line3)) was displayed but not the "sub"-entities directly, due to missing statistic data.

What did you expect to happen?

I expect that "sub"-power and "sub"-current have device_class and stat_class, so statistics are recordable/displayable.

How to reproduce it (minimal and precise)

No response

Zigbee2MQTT version

2.0.0-2

Adapter firmware version

7.4.1 [GA]

Adapter

EmberZNet

Setup

Add-on on HA OS on Fujitsu Thinclient

Debug log

No response

@ViscountHermit
Copy link

ViscountHermit commented Feb 8, 2025

I have pretty much the same issue. It seems for some reason that Z2m is not sending the state_class for some entities. I've looked but can't seem to find how and where the class is set. BTW, mine is a _TZE204_81yrt3lo PJ1203A

I know there's so much to do, but can this issue pretty please not be forgotten.

xs400dohc added a commit to xs400dohc/zigbee2mqtt that referenced this issue Feb 12, 2025
Made some investigation:
- as written in my post, energy has a device_class and state_class power and current don't.
- searched for the reason why energy behaves different to power and current
- in [homeassistant.ts](https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/extension/homeassistant.ts) I found this code in line 1018:
```
                // If a variable includes Wh, mark it as energy
                if (firstExpose.unit && ['Wh', 'kWh'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'energy', state_class: 'total_increasing'});
                }

```
I think a possible solution could be to add following
```
                // If a variable includes A or mA, mark it as energy
                if (firstExpose.unit && ['A', 'mA'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'current', state_class: 'measurement'});
                }

                // If a variable includes mW, W, kW, MW, GW, TW, mark it as energy
                if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'power', state_class: 'measurement'});
                }

```
But I don't know how to test the changes in my homeassistant if that would fix the problem. Didn't found homeassistant.ts in my installation (using VS-Code-extension).
Perhaps it's also way for other sensors with other units, e.g. Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s or GiB/s (see [here](https://www.home-assistant.io/integrations/sensor#device-class)).
xs400dohc added a commit to xs400dohc/zigbee2mqtt that referenced this issue Feb 12, 2025
Made some investigation:
- as written in my post, energy has a device_class and state_class power and current don't.
- searched for the reason why energy behaves different to power and current
- in [homeassistant.ts](https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/extension/homeassistant.ts) I found this code in line 1018:
```
                // If a variable includes Wh, mark it as energy
                if (firstExpose.unit && ['Wh', 'kWh'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'energy', state_class: 'total_increasing'});
                }

```
I think a possible solution could be to add following
```
                // If a variable includes A or mA, mark it as energy
                if (firstExpose.unit && ['A', 'mA'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'current', state_class: 'measurement'});
                }

                // If a variable includes mW, W, kW, MW, GW, TW, mark it as energy
                if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'power', state_class: 'measurement'});
                }

```
But I don't know how to test the changes in my homeassistant if that would fix the problem. Didn't found homeassistant.ts in my installation (using VS-Code-extension).
Perhaps it's also way for other sensors with other units, e.g. Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s or GiB/s (see [here](https://www.home-assistant.io/integrations/sensor#device-class)).
xs400dohc added a commit to xs400dohc/zigbee2mqtt that referenced this issue Feb 12, 2025
Made some investigation:
- as written in my post, energy has a device_class and state_class power and current don't.
- searched for the reason why energy behaves different to power and current
- in [homeassistant.ts](https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/extension/homeassistant.ts) I found this code in line 1018:
```
                // If a variable includes Wh, mark it as energy
                if (firstExpose.unit && ['Wh', 'kWh'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'energy', state_class: 'total_increasing'});
                }

```
I think a possible solution could be to add following
```
                // If a variable includes A or mA, mark it as energy
                if (firstExpose.unit && ['A', 'mA'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'current', state_class: 'measurement'});
                }

                // If a variable includes mW, W, kW, MW, GW, TW, mark it as energy
                if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) {
                    Object.assign(extraAttrs, {device_class: 'power', state_class: 'measurement'});
                }

```
But I don't know how to test the changes in my homeassistant if that would fix the problem. Didn't found homeassistant.ts in my installation (using VS-Code-extension).
Perhaps it's also way for other sensors with other units, e.g. Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s or GiB/s (see [here](https://www.home-assistant.io/integrations/sensor#device-class)).
xs400dohc added a commit to xs400dohc/zigbee2mqtt that referenced this issue Feb 12, 2025
implemented the suggestion of @Koenkk
xs400dohc added a commit to xs400dohc/zigbee2mqtt that referenced this issue Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants