Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric Trigger-based template sensor fails when state template renders to none #117397

Open
mekaneck opened this issue May 14, 2024 · 8 comments

Comments

@mekaneck
Copy link

mekaneck commented May 14, 2024

The problem

As per the documentation:

If the sensor is numeric, i.e. it has a state_class or a unit_of_measurement, the state template must render to a number or to none.

However, when a trigger-based template sensor contains a state template that renders to none an error will result. The same state template can be used in a state-based template sensor and no errors will occur.

What version of Home Assistant Core has the issue?

2024.5.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/#state

Diagnostics information

No response

Example YAML snippet

  • Create an input number helper named input_number.test
  • Put the code below into your configuration
  • When the input number is any number except 1, the two new template sensors will be zero. When the input helper is changed to 1 then only the state template sensor will go to unknown. The trigger template sensor will cause an error.
template:
  - trigger:
      - platform: state
        entity_id: input_number.test
    sensor:
      - name: Test Trigger Template
        unit_of_measurement: kW
        state: "{{ none if states('input_number.test') | int == 1 else 0 }}"
  - sensor:
      - name: Test State Template
        unit_of_measurement: kW
        state: "{{ none if states('input_number.test') | int == 1 else 0 }}"

Anything in the logs that might be useful for us?

Logger: homeassistant.helpers.event
Source: helpers/event.py:352
First occurred: 9:27:29 PM (1 occurrences)
Last logged: 9:27:29 PM

Error while dispatching event for input_number.test to <Job track state_changed event ['input_number.test'] HassJobType.Callback <function async_attach_trigger.<locals>.state_automation_listener at 0x7fb08da01f80>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 662, 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 "/usr/src/homeassistant/homeassistant/helpers/event.py", line 352, in _async_dispatch_entity_id_event
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 938, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/homeassistant/triggers/state.py", line 190, in state_automation_listener
    call_action()
  File "/usr/src/homeassistant/homeassistant/components/homeassistant/triggers/state.py", line 172, in call_action
    hass.async_run_hass_job(
  File "/usr/src/homeassistant/homeassistant/core.py", line 938, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/template/coordinator.py", line 104, in _handle_triggered
    self.async_set_updated_data(
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 489, in async_set_updated_data
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/components/template/trigger_entity.py", line 56, in _handle_coordinator_update
    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.test_trigger_template has device class 'None', state class 'None' unit 'kW' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'None' (<class 'str'>)

Additional information

I have not confirmed this, but it's possible this error may have begun after this PR was merged: #85605

@home-assistant
Copy link

Hey there @PhracturedBlue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template 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 template 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)


template documentation
template source
(message by IssueLinks)

@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@mekaneck
Copy link
Author

Not stale. Still an issue on 2024.8.2

@mekaneck mekaneck changed the title Numeric Trigger-based template sensor fails when state template returns none Numeric Trigger-based template sensor fails when state template renders to none Aug 18, 2024
@github-actions github-actions bot removed the stale label Aug 18, 2024
@thomluther
Copy link

I see the same issue in 2024.8.3

@wbyoung
Copy link
Contributor

wbyoung commented Sep 12, 2024

I just checked out and ran 2023.2.1 based on the suggestion above of this being related to #85605. I had the intention of trying to run a git bisect and track down where this regression occurred, but even in that version, prior to #85605 being merged, I got the following warning:

2024-09-12 20:34:08.942 WARNING (MainThread) [homeassistant.components.sensor] Sensor sensor.test_trigger_template has device class None, state class None and unit kW thus indicating it has a numeric value; however, it has the non-numeric value: None (<class 'str'>); Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+template%22

And the sensor just used the value of None instead of becoming unknown or unavailable. In fact, it accepted any string value at all. So maybe this never worked as it's documented.

@wbyoung
Copy link
Contributor

wbyoung commented Sep 14, 2024

I believe I have a solution that fixes this issue in #125873. Hopefully it gets approved and merged.

@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@mekaneck
Copy link
Author

Still a problem in 2024.12.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants