Comments (6)
Hello, thank you for the reference to the other issue. Based on the information discussed in #99942, I believe that the current implementation of the availability and value templates is not optimal and shall be improved. ...
By the way @litinoveweedle, there is a closed/stale PR "Manual trigger template handle faulty state #100079" that references issue 99942. I don't know why that PR was not progressed.
from core.
rest documentation
rest source
from core.
Base on the quick look at the code I do not see any place where availability template is being evaluated!
core/homeassistant/components/rest/sensor.py:
def _update_from_rest_data(self) -> None:
"""Update state from the rest data."""
try:
value = self.rest.data_without_xml()
except ExpatError as err:
_LOGGER.warning(
"REST xml result could not be parsed and converted to JSON: %s", err
)
value = self.rest.data
if self._json_attrs:
self._attr_extra_state_attributes = parse_json_attributes(
value, self._json_attrs, self._json_attrs_path
)
raw_value = value
if value is not None and self._value_template is not None:
value = self._value_template.async_render_with_possible_json_value(
value, None
)
if value is None or self.device_class not in (
SensorDeviceClass.DATE,
SensorDeviceClass.TIMESTAMP,
):
self._attr_native_value = value
self._process_manual_data(raw_value)
self.async_write_ha_state()
return
self._attr_native_value = async_parse_date_datetime(
value, self.entity_id, self.device_class
)
self._process_manual_data(raw_value)
self.async_write_ha_state()
If availability template should be evaluated it has to be before this:
if value is not None and self._value_template is not None:
value = self._value_template.async_render_with_possible_json_value(
value, None
)
so this is obvious mistake as documentation clearly mentions availability template in the rest sensor configuration, but it is not implemented!
https://www.home-assistant.io/integrations/sensor.rest#configuration-variables
availability template (optional)
Defines a template if the entity state is available or not.
from core.
Roughly something like this could do the trick (+ config option for availability)
def _update_from_rest_data(self) -> None:
"""Update state from the rest data."""
try:
value = self.rest.data_without_xml()
except ExpatError as err:
_LOGGER.warning(
"REST xml result could not be parsed and converted to JSON: %s", err
)
value = self.rest.data
+ if self._availability is not None:
+ if not self._availability.async_render_with_possible_json_value(
+ value, None
+ ):
+ self._attr_native_value = STATE_UNAVAILABLE
+ self.async_write_ha_state()
+ return
if self._json_attrs:
self._attr_extra_state_attributes = parse_json_attributes(
value, self._json_attrs, self._json_attrs_path
)
raw_value = value
if value is not None and self._value_template is not None:
value = self._value_template.async_render_with_possible_json_value(
value, None
)
if value is None or self.device_class not in (
SensorDeviceClass.DATE,
SensorDeviceClass.TIMESTAMP,
):
self._attr_native_value = value
self._process_manual_data(raw_value)
self.async_write_ha_state()
return
self._attr_native_value = async_parse_date_datetime(
value, self.entity_id, self.device_class
)
self._process_manual_data(raw_value)
self.async_write_ha_state()
from core.
This issue appears to be the same as "Rest sensor not updating (non-numeric value: 'None') #99942". As discussed there, the availability template is evaluated but not until after the state is evaluated. The value_template needs to deal with data that does not conform with what it expects (unless/until changes such as suggested by @litinoveweedle above). To put it another way, the availability template does not filter the data for the benefit of value_template; rather, it tells the state machine and hence the rest of HA whether the entity should be considered available.
from core.
Hello, thank you for the reference to the other issue. Based on the information discussed in #99942, I believe that the current implementation of the availability and value templates is not optimal and shall be improved.
Reasoning:
-
To prevent for errors caused outside of end user responsibility (device not producing always coherent JSON data) BOTH value and availability templates needs to include very heavy weight checks - see example in the configuration above. This is very complex for users to implement and as well taxing for state machine as the same conditions needs to be evaluated twice.
-
As value template doesn't handle None output as either STATE_UNKNOWN or STATE_UNAVAILABLE handling state only from the value template is not possible. This therefore requires both templates with a complete set of JSON validation checks to be always implemented. One evaluates potential value and second evaluates even if the value is already known if the entity should be available. This is a very confusing implementation from how users are using real devices. (When device is not available I don't care about its value) Therefore the confusion in this and referenced issue.
The proposal would be to either evaluate availability template before value template to prevent from needs to duplicate all checks and only evaluate value template if True is returned by availability template - I can provide PR for such proposal for all affected rest entities.
It is also possible to implement better handling for None value returned by value template to be interpreted as a either STATE_UNKNOWN or STATE_UNAVAILABLE.
from core.
Related Issues (20)
- Raspberry Pi 4: can't run 2024.7 docker image: exec /init: exec format error HOT 1
- Error setting up entry
- Izone - climate entities show null current temperature - Myzone 3 CMOCB bridge HOT 4
- UtilityMeter is using state class 'total' which is impossible considering device class ('volume_storage') HOT 1
- TP-Link HS220 Failed Setup Error 'Unable to Query the device:" HOT 8
- Failed to to call /backups/reload when Storage for Backups is defined HOT 1
- Meteoalarm doesn't get information from feed HOT 1
- Airly integration timeouts HOT 10
- Google Calendar issue Run into an unknown error HOT 1
- EDL21: Entities unavailable after restart of HA. Need to reboot the whole Server. HOT 1
- Integration Tibber, changes from grid rewards interfere with sensor values HOT 2
- NodeRed stopped monitoring the nodes, Error connecting to dbus: HOT 5
- RESTful sensor stops working after some time HOT 1
- Missing additional entities on HomeWizard meter HOT 8
- TP-Link P110 going offline/online repeatedly HOT 32
- Remote Keyboard Doesnt Process 4+ Codes for 1 Button Press HOT 1
- Xiaomi Smart Air Purifier 4 Lite. Unknown model. Unable to discover HOT 2
- Config entry selector does not show integrations classified as helpers
- xbox integration, failed to setup HOT 2
- Error: Unable to find notify service for webhook ID HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.