Skip to content

Rest sensor not updating (non-numeric value: 'None') #99942

@flo-wer

Description

The problem

The rest sensor is still raising the non-numeric value: 'None' (<class 'str'>) error despite using the availability config to filter any non-numeric values (see the is_number condition). I included example API responses for both cases. In the not available case, the availability condition evaluates to false and the value_template equals null/None. This produces the below exception.

What version of Home Assistant Core has the issue?

core-2024.10

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

rest

Link to integration documentation on our website

https://www.home-assistant.io/integrations/sensor.rest/

Diagnostics information

No response

Example YAML snippet

rest:
  - resource: http://192.168.1.110:3001/api/status-page/heartbeat/default
    scan_interval: 30
    sensor:
      - unique_id: rest_uptime_ping1
        name: uptime_ping1
        value_template: '{% set v = value_json.heartbeatList["1"][-1] %}{{ v.ping }}'
        availability: '{% set v = value_json.heartbeatList["1"][-1] %}{{ v.status == 1 and is_number(v.ping) }}'
        unit_of_measurement: 'ms'
        state_class: measurement

Anything in the logs that might be useful for us?

2023-09-08 18:27:10.124 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/sensor/__init__.py", line 591, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'None'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/update_coordinator.py", line 235, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.async_update_listeners()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/update_coordinator.py", line 173, in async_update_listeners
    update_callback()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/rest/entity.py", line 51, in _handle_coordinator_update
    self._update_from_rest_data()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/rest/sensor.py", line 180, in _update_from_rest_data
    self._process_manual_data(raw_value)
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/trigger_template_entity.py", line 242, in _process_manual_data
    self.async_write_ha_state()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 730, in async_write_ha_state
    self._async_write_ha_state()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 830, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 771, in _async_generate_attributes
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/entity.py", line 736, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/sensor/__init__.py", line 593, in state
    raise ValueError(
ValueError: Sensor sensor.uptime_ping1 has device class 'None', state class 'measurement' unit 'ms' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'None' (<class 'str'>)

Additional information

REST API response
available:

{
  "heartbeatList": {
    "1": [
      {
        "status": 1,
        "ping": 21.4
      }
    ]
  }
}

not available:

{
  "heartbeatList": {
    "1": [
      {
        "status": 0,
        "ping": null
      }
    ]
  }
}

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions