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

ValueError: month must be in 1..12 #456

Closed
f22raptorroland opened this issue Dec 1, 2024 · 3 comments
Closed

ValueError: month must be in 1..12 #456

f22raptorroland opened this issue Dec 1, 2024 · 3 comments

Comments

@f22raptorroland
Copy link

Version of the custom_component

0.0.16

Homeassistant version

Core
2024.11.3
Supervisor
2024.11.4
Operating System
Frontend
20241106.2

Configuration

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

Describe the bug

The integration stopped working. The device, and entity are "Unavailable"
Last entry is Nov 30 14:00
Reading, and restarting does not resolve the issue

I have additional_costs configuration, but I don't remember what it was exactly, and don't know how it is stored

I have tried to debug it but I'm not a python expert, and wasn't able to find the source of the issue.
Considering the log error message, and that it just turned to December I assume it gets somehow out of the 1 .. 12 bounds

Also according to Nordpool FI data electricity will be 0,01 at from Midnight to 1pm. May be related to: #428

Debug log


2024-12-01 12:32:35.874 DEBUG (MainThread) [custom_components.nordpool] Added nordpool to hass.data
2024-12-01 12:32:35.882 DEBUG (MainThread) [custom_components.nordpool.sensor] Dumping config mappingproxy({'VAT': False, 'additional_costs': '{% set nge = {"base": 5.97,"tax": 0.255,"cut": 0.0242, } %} {% set helen = {"base": 5.51,"transfer": 0.0407,"excise": 0.027776,"security_fee": 0.000161, } %} {% set current_month = now().month %} {% set current_year = now().year %} {% set days_in_month = (now().replace(month=current_month + 1, day=1) - now().replace(day=1)).days %} {% set hours_in_month = days_in_month * 24 %} {% set base_fee_per_hour = ((nge.base + helen.base) / hours_in_month) %} {{ (nge.cut + helen.transfer + helen.excise + helen.security_fee + (current_price * nge.tax) + base_fee_per_hour) | float }}', 'currency': 'EUR', 'low_price_cutoff': 1.0, 'precision': 3, 'price_in_cents': False, 'price_type': 'MWh', 'region': 'FI'})
2024-12-01 12:32:35.882 DEBUG (MainThread) [custom_components.nordpool.sensor] timezone set in ha 'Europe/Helsinki'
2024-12-01 12:32:35.882 DEBUG (MainThread) [custom_components.nordpool.sensor] Template {% set nge = {"base": 5.97,"tax": 0.255,"cut": 0.0242, } %} {% set helen = {"base": 5.51,"transfer": 0.0407,"excise": 0.027776,"security_fee": 0.000161, } %} {% set current_month = now().month %} {% set current_year = now().year %} {% set days_in_month = (now().replace(month=current_month + 1, day=1) - now().replace(day=1)).days %} {% set hours_in_month = days_in_month * 24 %} {% set base_fee_per_hour = ((nge.base + helen.base) / hours_in_month) %} {{ (nge.cut + helen.transfer + helen.excise + helen.security_fee + (current_price * nge.tax) + base_fee_per_hour) | float }}
2024-12-01 12:32:35.883 DEBUG (MainThread) [custom_components.nordpool.sensor] called async_added_to_hass nordpool_mwh_fi_eur_3_10_0
2024-12-01 12:32:35.884 DEBUG (MainThread) [custom_components.nordpool.sensor] handle_new_hr
2024-12-01 12:32:35.884 DEBUG (MainThread) [custom_components.nordpool] Updating today's prices.
2024-12-01 12:32:35.884 DEBUG (MainThread) [custom_components.nordpool] calling _update today None ['FI']
2024-12-01 12:32:35.932 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-11-30 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-11-30'}
2024-12-01 12:32:35.935 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-12-01 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-12-01'}
2024-12-01 12:32:35.945 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-12-02 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-12-02'}
2024-12-01 12:32:35.952 WARNING (MainThread) [custom_components.localtuya.common] Missing device configuration for device_id bfd233e3417c047c51oynr
2024-12-01 12:32:36.009 DEBUG (MainThread) [custom_components.nordpool] Updating tomorrows prices.
2024-12-01 12:32:36.009 DEBUG (MainThread) [custom_components.nordpool] calling _update tomorrow 2024-12-02 12:32:36.009835+02:00 ['FI']
2024-12-01 12:32:36.059 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-11-30 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-11-30'}
2024-12-01 12:32:36.062 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-12-01 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-12-01'}
2024-12-01 12:32:36.079 DEBUG (MainThread) [custom_components.nordpool.aio_price] requested https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?currency=EUR&market=DayAhead&deliveryArea=FI&date=2024-12-02 {'currency': 'EUR', 'market': 'DayAhead', 'deliveryArea': 'FI', 'date': '2024-12-02'}
2024-12-01 12:32:36.119 DEBUG (MainThread) [custom_components.nordpool.sensor] handle_new_hr
2024-12-01 12:32:36.149 ERROR (MainThread) [homeassistant.util.logging] Exception in handle_new_hr when dispatching 'nordpool_update_hour': ()
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: month must be in 1..12
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/config/custom_components/nordpool/sensor.py", line 488, in handle_new_hr
    self._update()
  File "/config/custom_components/nordpool/sensor.py", line 330, in _update
    today = self.today
            ^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 379, in today
    self._calc_price(i["value"], fake_dt=i["start"])
  File "/config/custom_components/nordpool/sensor.py", line 291, in _calc_price
    template_value = self._ad_template.async_render(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 635, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: month must be in 1..12
2024-12-01 12:32:36.149 DEBUG (MainThread) [custom_components.nordpool.sensor] handle_new_hr
2024-12-01 12:32:36.179 ERROR (MainThread) [homeassistant.util.logging] Exception in handle_new_hr when dispatching 'nordpool_update_hour': ()
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: month must be in 1..12
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/config/custom_components/nordpool/sensor.py", line 488, in handle_new_hr
    self._update()
  File "/config/custom_components/nordpool/sensor.py", line 330, in _update
    today = self.today
            ^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 379, in today
    self._calc_price(i["value"], fake_dt=i["start"])
  File "/config/custom_components/nordpool/sensor.py", line 291, in _calc_price
    template_value = self._ad_template.async_render(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 635, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: month must be in 1..12
2024-12-01 12:32:36.180 DEBUG (MainThread) [custom_components.nordpool.sensor] handle_new_hr
2024-12-01 12:32:36.211 ERROR (MainThread) [homeassistant.util.logging] Exception in handle_new_hr when dispatching 'nordpool_update_hour': ()
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: month must be in 1..12
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/config/custom_components/nordpool/sensor.py", line 488, in handle_new_hr
    self._update()
  File "/config/custom_components/nordpool/sensor.py", line 330, in _update
    today = self.today
            ^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 379, in today
    self._calc_price(i["value"], fake_dt=i["start"])
  File "/config/custom_components/nordpool/sensor.py", line 291, in _calc_price
    template_value = self._ad_template.async_render(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 635, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: month must be in 1..12
2024-12-01 12:32:36.212 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.nordpool_mwh_fi_eur_3_10_0 for domain sensor with platform nordpool
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 633, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: month must be in 1..12
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 599, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 918, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1367, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/config/custom_components/nordpool/sensor.py", line 514, in async_added_to_hass
    await self.handle_new_hr()
  File "/config/custom_components/nordpool/sensor.py", line 488, in handle_new_hr
    self._update()
  File "/config/custom_components/nordpool/sensor.py", line 330, in _update
    today = self.today
            ^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 379, in today
    self._calc_price(i["value"], fake_dt=i["start"])
  File "/config/custom_components/nordpool/sensor.py", line 291, in _calc_price
    template_value = self._ad_template.async_render(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 635, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: month must be in 1..12

@f22raptorroland
Copy link
Author

I realized the log dumps the custom config so this is it for me:

Also, this is the source of the issue
month=current_month + 1 is a silly thing to do at this time of the year...

script:
{% set nge = {
        "base": 5.97,
        "tax": 0.255,
        "cut": 0.0242,
    } %
} {% set helen = {
        "base": 5.51,
        "transfer": 0.0407,
        "excise": 0.027776,
        "security_fee": 0.000161,
    } %
} {% set current_month = now().month %
} {% set current_year = now().year %
} {% set days_in_month = (now().replace(month=current_month + 1, day=1) - now().replace(day=1)).days %
} {% set hours_in_month = days_in_month * 24 %
} {% set base_fee_per_hour = ((nge.base + helen.base) / hours_in_month) %
} {
    { (nge.cut + helen.transfer + helen.excise + helen.security_fee + (current_price * nge.tax) + base_fee_per_hour) | float
    }
}

@f22raptorroland
Copy link
Author

@Hellowlol Is there some way to update the script without deleting the configuration, or alternatively, is there some way to re-configure without changing the entity ID?
I would love not to have to reconfigure my cards, and other automation that reply on this entity.

@Hellowlol
Copy link
Collaborator

Not really. Alternatively you can use yaml but they would require restart

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

No branches or pull requests

2 participants