I have a REST sensor that is somewhat flaky due to the server being buggy. I would rather have my computed sensor return unknown / None than a bogus value so I can see gaps in the graph rather than incorrect zeros or whatever. I have tried a lot of different approaches to getting a template sensor to correctly return nothing. This is what I’m trying at the moment but I’d like to know if there is an idiom for returning a safe / correct unknown value.
I know that if the value itself is unknown that state_attr() will return ‘unknown’ but there are an essentially infinite number of possible garbage values that could be returned by the server that won’t yield ‘unknown’ e.g., if the server sends back HTML the response could be something like <HTML><BODY>whatever...
so I’m testing the actual value with a regex and only want to return a real value if the regex matches.
sensors:
openevse_session_energy_raw:
friendly_name: "OpenEVSE Session Energy (status)"
icon_template: mdi:gauge
unit_of_measurement: 'kWh'
entity_id:
- sensor.openevse_status
value_template: >-
{%- if state_attr('sensor.openevse_status', 'wattsec') | regex_match('[0-9]+') -%}
{%- set wattsec = state_attr('sensor.openevse_status', 'wattsec') -%}
{{- (wattsec|float / 3600000)|round(2) -}}
{%- else -%}
{{- None -}}
{%- endif -%}
This seems like it ought to work, but I find that if I try to use this template as input into a filter sensor, it fails to initialize.
2019-05-19 14:25:20 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 363, in _async_add_entity
await entity.async_added_to_hass()
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 223, in async_added_to_hass
self._entity, prev_state, state, False)
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 157, in filter_sensor_state_listener
filtered_state = filt.filter_state(copy(temp_state))
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 334, in filter_state
filtered = self._filter_state(FilterState(new_state))
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 409, in _filter_state
if self.states else 0
File "/usr/local/lib/python3.7/statistics.py", line 376, in median
data = sorted(data)
TypeError: '<' not supported between instances of 'str' and 'float'
I’ve tried a few different approaches like returning a string ‘unknown’ or just leaving the else clause completely out so the expression returns nothing.
In all the cases I can come up with the filter still fails as above, of course I can return 0.0 and have the filter function, but I don’t really want to send bogus data to the filter at all.
Ideally the template sensor would eat the bad state change completely rather than sending bad data to listeners.