fix HA exception when prometheus query fails #18
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, thanks for making this project - I get some good use out of it!
I'm seeing this exception when my prometheus query returns no result.
As per the error, I believe it's an issue that we're setting
_attr_native_value
to"problem"
viaSTATE_PROBLEM
since in most cases a prometheus query will return a number, and most device classes probably don't support both numbers and strings as values (such as temperature here).I've made a change so we instead set the
available
property tofalse
if there's an error, and the native value toNone
- which seems to be allowed as per the sensor.py code.This has the added benefit of showing up in the HA UI when there's an issue (instead of breaking all prometheus sensors with the current exception):
I'm new to home assistant code, so I have no idea if this is right or wrong, I'm just fixing issues I ran into 🙏
For context, here's a snippet of the configuration yaml I'm using, which causes the error (when paired with a prometheus query which yields no value)