home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.46k stars 30.33k forks source link

Command line sensor not observing availability template #127960

Open tomlut opened 4 days ago

tomlut commented 4 days ago

The problem

I have a command line sensor that occasionally fails. So I added an availability template to account for this:

command_line:
  - sensor:
      name: UV Index
      unique_id: c2d041f0-05b6-46e2-af8b-685c7fd0a580
      command: "curl -k --silent 'https://uvdata.arpansa.gov.au/xml/'|sed -n '77p'|cut -c 12-14"
      availability: "{{ value|is_number}}"
      unit_of_measurement: " "
      state_class: measurement 

However when the sensor fails I still get the following error:

Logger: homeassistant
Source: components/sensor/__init__.py:664
First occurred: 03:05:16 (4 occurrences)
Last logged: 03:31:46

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 657, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 660, in state
    numerical_value = float(value)
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: ''

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 142, in _update_entity_state
    await self._async_update()
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 190, in _async_update
    self._process_manual_data(value)
  File "/usr/src/homeassistant/homeassistant/helpers/trigger_template_entity.py", line 234, in _process_manual_data
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 664, in state
    raise ValueError(
ValueError: Sensor sensor.uv_index has device class 'None', state class 'measurement' unit ' ' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '' (<class 'str'>)

The availability template should be checked before generating this error.

What version of Home Assistant Core has the issue?

core-2024.10.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

https://www.home-assistant.io/integrations/command_line/

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 4 days ago

Hey there @gjohansson-st, mind taking a look at this issue as it has been labeled with an integration (command_line) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `command_line` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign command_line` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


command_line documentation command_line source (message by IssueLinks)