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

Cannot select unit conversion in HomeAssistant #3333

Closed
caco3 opened this issue Oct 12, 2024 Discussed in #3218 · 8 comments · Fixed by #3332
Closed

Cannot select unit conversion in HomeAssistant #3333

caco3 opened this issue Oct 12, 2024 Discussed in #3218 · 8 comments · Fixed by #3332
Assignees

Comments

@caco3
Copy link
Collaborator

caco3 commented Oct 12, 2024

Discussed in #3218

Originally posted by Lackmake September 1, 2024
I have a few smartplugs where i can manually select how HA should display the power values, like this:
image

For the AI-on-the-edge entities however i cannot. The option is completely missing:
image

What i found is that the rate-sensor is missing the "device_class" attribute. That would have to be added here for both "rates", i think "power" would be correct as thats what my smartplugs report:

// Group | Field | User Friendly Name | Icon | Unit | Device Class | State Class | Entity Category
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "value", "Value", "gauge", valueUnit, meterType, "total_increasing", "", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "raw", "Raw Value", "raw", "", "", "", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "error", "Error", "alert-circle-outline", "", "", "", "diagnostic", qos);
/* Not announcing "rate" as it is better to use rate_per_time_unit resp. rate_per_digitalization_round */
// allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "rate", "Rate (Unit/Minute)", "swap-vertical", "", "", "", ""); // Legacy, always Unit per Minute
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "rate_per_time_unit", "Rate (" + rateUnit + ")", "swap-vertical", rateUnit, "", "measurement", "", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "rate_per_digitalization_round", "Change since last digitalization round", "arrow-expand-vertical", valueUnit, "", "measurement", "", qos); // correctly the Unit is Unit/Interval!
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "timestamp", "Timestamp", "clock-time-eight-outline", "", "timestamp", "", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "json", "JSON", "code-json", "", "", "", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic(group, "problem", "Problem", "alert-outline", "", "problem", "", "", qos); // Special binary sensor which is based on error topic
}

But i am not 100% sure if that is the only reason why home assistant doesnt let me choose the unit....i also found this old thread about the "native_unit_of_measurement": https://community.home-assistant.io/t/unit-of-measurement-2022-4/409860/16

@caco3 caco3 self-assigned this Oct 12, 2024
@caco3 caco3 linked a pull request Oct 12, 2024 that will close this issue
@caco3
Copy link
Collaborator Author

caco3 commented Oct 12, 2024

@Lackmake I updated the Device Class according your suggestions in #3332

Can you please test and see if it is ok now?
Please test the latest (most top) build from https://github.com/jomjol/AI-on-the-edge-device/actions?query=branch%3Asupport-homeassistant-2024.10

@Lackmake
Copy link

I now have a device_class of "volume_flow_rate" for my rate-sensors, thats not correct. Should be "power"

image

@Lackmake
Copy link

Lackmake commented Oct 13, 2024

Ok, so i played around a bit more and changed the mqtt-discovery values with a mqtt client and noticed that AIOTE does report a "unit_of_meas" and not "unit_of_measurement". See here:

if (unit != "") {
payload += "\"unit_of_meas\": \"" + unit + "\",";
}

When i set the device_class to "power" and fix the "unit_of_measurement" then it works as i hoped and i can select the unit in home assistant. Both fixes are needed, either one alone is not enough

@Lackmake
Copy link

The wrong device_class probably comes from here:

/* Energy meters need a different Device Class, see https://github.com/jomjol/AI-on-the-edge-device/issues/3333 */
std::string rate_device_class = "volume_flow_rate";
if (meterType == "energy") {
value_state_class = "power";
}

I think in the if condition you wanted to set "rate_device_class" not value_state_class. Maybe a copypaste error from a few lines above :D

@caco3
Copy link
Collaborator Author

caco3 commented Oct 13, 2024

AIOTE does report a "unit_of_meas" and not "unit_of_measurement"

This is also correct according to the specification, see Supported abbreviations in https://www.home-assistant.io/integrations/mqtt/#discovery-messages
grafik

@caco3
Copy link
Collaborator Author

caco3 commented Oct 13, 2024

I think in the if condition you wanted to set "rate_device_class" not value_state_class. Maybe a copypaste error from a few lines above :D

You are right.
it is now fixed in the latest version.

@Lackmake
Copy link

I can select the unit now, perfect, thank you very much :)
image

@caco3
Copy link
Collaborator Author

caco3 commented Oct 13, 2024

Fixed in #3332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants