homeassistant-blitzortung icon indicating copy to clipboard operation
homeassistant-blitzortung copied to clipboard

Addon improperly sets sensor state value directly causing unit confusion

Open cryptk opened this issue 6 months ago • 0 comments

My Home Assistant install is configured to operate in miles and I have the Blitzortung integration configured for a lightning detection radius of 100, which in theory should be 100 miles (I don't see a way to specify, so I assume it runs with the system units).

The issue is that the sensor.blitzortung_lightning_distance entity regularly shows lightning strikes well over 100 miles away. the furthest distance that it tracks seems to indicate that while it is operating in miles, the sensor is reporting it's number in km (despite saying mi).

For example, the furthest lightning strikes seem to be "160 mi" away, but 100 mi is 160.9 km, so while the integration is claiming that it is showing me the number in mi, it's actually showing the number in km, but labeling it as mi.

I believe this is because at https://github.com/mrk-its/homeassistant-blitzortung/blob/master/custom_components/blitzortung/sensor.py#L159 you are setting the sensor state directly, which should not be done. Instead you should set the native_unit_of_measurement (which you are doing) and then set the native_value attribute. Home Assistant will then look at the native_unit_of_measurement and native_value, determine if any conversion factor needs to be applied and finally update the state itself.

This would be a good issue to resolve at the same time as #79

cryptk avatar Jan 02 '24 23:01 cryptk