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

decimal.InvalidOperation #914

Closed
ColinRobbins opened this issue Jul 28, 2022 · 14 comments
Closed

decimal.InvalidOperation #914

ColinRobbins opened this issue Jul 28, 2022 · 14 comments
Labels
bug Something isn't working utility-meter

Comments

@ColinRobbins
Copy link

I am seeing the followng error in my logs.

I am running HA 2022.8.0b0.
PowerCalc 0.24.3.

I think the error was probably hapening before I upgraded from 2022.7.7, but cannot be certain.
Alas, I can't (easily) roll back to verify to database schema changes in 22.22.8.0b0

The error is ocurring at Startup time, so maybe because the status of a sensor is "Unavailable" or "Unknown" as it has not fully started yet. Thus the conversion to decimal fails.

Logger: homeassistant
Source: custom_components/powercalc/sensors/power.py:447
Integration: Powercalc (documentation, issues)
First occurred: 14:49:00 (1 occurrences)
Last logged: 14:49:00

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 326, in appliance_state_listener
    await self._update_power_sensor(self._source_entity, new_state)
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 386, in _update_power_sensor
    self._power = await self.calculate_power(state)
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 447, in calculate_power
    return Decimal(power)
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
@bramstroker bramstroker added the bug Something isn't working label Jul 28, 2022
@bramstroker
Copy link
Owner

I have added extra logging for the next release so we can see which entity is the culprit. And what the value is it is trying to convert to a decimal.

@jfrux
Copy link

jfrux commented Jul 29, 2022

Getting this as well!

Using template and doing some math so not sure if it's an issue with my template.
My template does render properly in Developer Tools > Template

Example of what I'm using:

{% set power_switch = states('switch.pool_filter_pump') %}
{% set current_speed = states('sensor.pool_filter_pump_speed')|float %}
{% set max_power_draw = 1100 %}
{% if power_switch == "on" %}
{% set current_power_draw = (max_power_draw * (current_speed / 100))|float %}
{% else %}
{% set current_power_draw = 0.0|float %}
{% endif %}
{{ current_power_draw }}

Maybe there is a better way to do it to ensure it's safer but it should at least work for now I'd think.

@jfrux
Copy link

jfrux commented Jul 29, 2022

I added the |float to everything to ensure it was typed but dunno if that is even necessary.

@bramstroker
Copy link
Owner

I have found the issue, the problem is with sensors having a template as the value and created with new config flow.
The template is not correctly recognized by powercalc as a template so it is trying to convert the full template string to a decimal (which causes this exception).

Will have some time to write some tests and make the fix tomorrow.

@ColinRobbins did you also use a power sensor with templates?

@ColinRobbins
Copy link
Author

I do.

@ColinRobbins
Copy link
Author

I may be barking up the wrong tree, but I only see the issue at start up.
In general the template appears to be working.
Is it possible, during HA startup the template is returning a state of "unavailable" or "unknown", which causes the error.

@bramstroker
Copy link
Owner

I may be barking up the wrong tree, but I only see the issue at start up. In general the template appears to be working. Is it possible, during HA startup the template is returning a state of "unavailable" or "unknown", which causes the error.

hmm I am pretty sure that this bug I reproduced for specifically the config flow created entities will never work if you use a template.
Did you define the template sensor with configuration.yaml or the GUI flow?
Seems you guys have 2 different issues.
@ColinRobbins for the conversion error stack trace you posted I already made some changes yesterday to provide a better error message. When you install 0.24.3 it should give information about the entity and the value which cause the decimal conversion error.

@ColinRobbins
Copy link
Author

Hi,
I use configuration.yaml.
I have 0.24.3, and do not see any addiitonal error information:

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/powercalc/sensors/power.py:447
Integration: Powercalc (documentation, issues)
First occurred: 16:48:35 (1 occurrences)
Last logged: 16:48:35

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 326, in appliance_state_listener
    await self._update_power_sensor(self._source_entity, new_state)
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 386, in _update_power_sensor
    self._power = await self.calculate_power(state)
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 447, in calculate_power
    return Decimal(power)
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

I have several entries with template sensors, and this only throws 1 error.

@bramstroker
Copy link
Owner

bramstroker commented Jul 29, 2022

Ah ok configuration.yaml, that's what I expected. So your issue is not similar to @jfrux.
I am sorry, the better error handling was not in v0.24.3 yet. Just released v0.24.4 which contains this.

@ColinRobbins
Copy link
Author

No problem - by trial an error, I have tracked it to the following...

- platform: powercalc
  entity_id: sensor.ev_charge_power
  name: EV
  fixed:
    power: "{{states('sensor.ev_power_w')}}"

The sensor sensor.ev_power_w is itself a template sensor, converts kWh to w.

 - name: EV Power W
    unique_id: cjr_46
    state: >
      {% if is_number(states('sensor.ev_charge_power')) %}
        {{ states('sensor.ev_charge_power')|float * 1000 }}
      {% else %}
        0
      {% endif %}
    state_class: measurement
    icon: 'mdi:flash'
    unit_of_measurement: W
    device_class: power

My guess is, that during the startup sequence, sensor.ev_power_w is unavailable, as its one of the last things HA starts.

If I change it to

- platform: powercalc
  entity_id: sensor.electra_charge_power
  name: Electra
  fixed:
    power: >
      {% if is_number(states('sensor.ev_charge_power')) %}
        {{ states('sensor.ev_charge_power')|float * 1000 }}
      {% else %}
        0
      {% endif %}

The error goes away.

@bramstroker
Copy link
Owner

Ok that's nice. Could you still check what value is in the logs when you use your old simple template? I suspect it is "unknown". When I know this I can have a look into making this a littlte more failsafe so no error will be logged in the error log and you can still use your old template.

@bramstroker
Copy link
Owner

@ColinRobbins I have found the best solution to your problem.
Currently the initial state of the powercalc power sensor is calculated during setup of powercalc. This could cause issues like you had where one of the template entities is not setup yet.
I have now changed that to calculate the initial state after setup. I am pretty sure this will also solve your problem.
Will be in next release.

@jfrux You issue with the template not being evaluated when using the config flow is also fixed, see #918, this will also be in the upcoming release.

@ColinRobbins
Copy link
Author

I've run v0.24.4 and reverted my template.
I now get the error (as anticiapted)

This error originated from a custom integration.

Logger: custom_components.powercalc.sensors.power
Source: custom_components/powercalc/sensors/power.py:326
Integration: Powercalc (documentation, issues)
First occurred: 09:20:18 (1 occurrences)
Last logged: 09:20:18

sensor.ev_charge_power: Could not convert value 'unknown' to decimal

I can now resolve this locally. Thank you.

@bramstroker
Copy link
Owner

Just released v0.24.5 with proposed fixes mentioned above.
Closing this issue. Let me know if you still experience any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working utility-meter
Projects
None yet
Development

No branches or pull requests

3 participants