Code Monkey home page Code Monkey logo

Comments (6)

timr49 avatar timr49 commented on July 24, 2024 1

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.

home-assistant avatar home-assistant commented on July 24, 2024

rest documentation
rest source

from core.

litinoveweedle avatar litinoveweedle commented on July 24, 2024

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.

litinoveweedle avatar litinoveweedle commented on July 24, 2024

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.

timr49 avatar timr49 commented on July 24, 2024

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.

litinoveweedle avatar litinoveweedle commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.