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

Frequent errors in Riemann Sum Integration #71684

Closed
RoboMagus opened this issue May 11, 2022 · 5 comments · Fixed by #71967
Closed

Frequent errors in Riemann Sum Integration #71684

RoboMagus opened this issue May 11, 2022 · 5 comments · Fixed by #71967

Comments

@RoboMagus
Copy link
Contributor

The problem

I've setup a trigger based template sensor for power measurements (see yaml below), which is fed into an integration sensor.
Multiple times a day I see an error pop up where the handling of new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) throws an AttributeError: 'NoneType' object has no attribute 'attributes'

What version of Home Assistant Core has the issue?

Core 2022.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

Integration

Link to integration documentation on our website

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

Diagnostics information

Logger: homeassistant.helpers.event
Source: components/integration/sensor.py:199
First occurred: May 10, 2022, 10:42:52 (5 occurrences)
Last logged: 13:07:28

Error while processing state change for sensor.mhi_ac_ctrl_estimated_power
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 271, in _async_state_change_dispatcher
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 523, in async_run_hass_job
    cast(Callable[..., _R], hassjob.target)(*args)
  File "/usr/src/homeassistant/homeassistant/components/integration/sensor.py", line 199, in calc_integration
    unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
AttributeError: 'NoneType' object has no attribute 'attributes'

Example YAML snippet

template:
- trigger:
  - platform: state
    entity_id: sensor.mhi_ac_ctrl_current_power
  sensor:
    - name: MHI-AC-Ctrl Estimated Power
      icon: mdi:flash
      device_class: power
      unit_of_measurement: W
      state: >-
        {% if is_state('sensor.mhi_ac_ctrl_current_power', 'unavailable') %}
          0
        {% else %}
          {# Standby power of 7W + current * avg.voltage * correction #}
          {{ (states('sensor.mhi_ac_ctrl_current_power')|float * (230 * 0.8) + 7.0) | round(2) }}
        {% endif %}

sensor:
- platform: integration
  source: sensor.mhi_ac_ctrl_estimated_power
  name: "MHI-AC-Ctrl estimated Energy"
  round: 2
  unit_prefix: k
  unit_time: h

Anything in the logs that might be useful for us?

Error while processing state change for sensor.mhi_ac_ctrl_estimated_power
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 271, in _async_state_change_dispatcher
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 523, in async_run_hass_job
    cast(Callable[..., _R], hassjob.target)(*args)
  File "/usr/src/homeassistant/homeassistant/components/integration/sensor.py", line 199, in calc_integration
    unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
AttributeError: 'NoneType' object has no attribute 'attributes'

Additional information

It seems like the error does not throw every time, as this will be executed way more often than is shown in the error occurences count.

My best guess is that the error is introduced by this change.
Something funky is going on with the attributes of the trigger based template sensor and the way attributes are queried by the integration component does propperly handle when those aren't present.

To propperly handle any funkyness it seems the following should be sufficient:

unit = None
with suppress(AttributeError):
    unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)

if unit is not None:
    ....
@probot-home-assistant
Copy link

integration documentation
integration source
(message by IssueLinks)

@probot-home-assistant
Copy link

Hey there @dgomes, mind taking a look at this issue as it has been labeled with an integration (integration) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@RoboMagus
Copy link
Contributor Author

After digging through the sources of trigger templates a bit more, this seems to have to do with the attributes restore logic that is used when reloading the template sensors configuration. Upon restore the status first becomes 'unknown' without any attributes. Those only pop in later.

This would explain why these errors pop up 5 times only. Those must be the times when I reloaded my config changes to some other template sensors.

@dgomes
Copy link
Contributor

dgomes commented May 11, 2022

Hi @RoboMagus

I think you traced the problem correctly

I would nonetheless prefer a solution involving a guard instead of suppressing the exception. Would you like to have a go and open a PR with the fix ?

@RoboMagus
Copy link
Contributor Author

RoboMagus commented May 16, 2022

@dgomes,
Indeed, checking if attributes are present at all before accessing them should suffice as well.
I'll see if I can give this a go this Wednesday and open a PR for it.

Edit:
Was easier than expected to get my old development setup up and running, so the PR is pending.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants