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

Unable to use the workday integration for next days values in additional costs. #357

Closed
magnusoverli opened this issue Dec 4, 2023 · 10 comments

Comments

@magnusoverli
Copy link

Version of the custom_component

0.0.14

Homeassistant version

2023.12.0b1

Configuration

- platform: nordpool
  # Country/region to get the energy prices for.
  region: "Bergen"

  # Override HA local currency used to fetch the prices from the API.
  currency: "NOK"

  # Add Value Added Taxes (VAT)?
  VAT: True

  # Energy price rounding precision.
  precision: 5

  # Percentage of average price to set the low price attribute
  # low_price = hour_price < average * low_price_cutoff
  low_price_cutoff: 1

  # Display price in cents in stead of (for example) Euros.
  price_in_cents: false

  # Price displayed for MWh, kWh or Wh
  price_type: kWh

  # Template to specify additional cost to be added to the tariff.
  # The template price is in EUR, DKK, NOK or SEK (not in cents).
  # For example: "{{ current_price * 0.19 + 0.023 | float}}"
  additional_costs: >-
    {% set el_taxes = 0 %}
    {% set workday = states('binary_sensor.workday_sensor') %}
    {% set month = now().month %}
    {% set hour = now().hour %}

    {# January to March - Weekend and holiday tariff #}
    {% if workday == 'off' and (month >= 1 and month <= 3) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Weekend and holiday tariff #}
    {% elif workday == 'off' and month >= 4 %}
      {% set el_taxes = 0.4393 %}

    {# January to March - Day tariff #}
    {% elif (month >= 1 and month <= 3) and (hour >= 6 and hour <= 22) %}
      {% set el_taxes = 0.4738 %}

    {# January to March - Night tariff #}
    {% elif (month >= 1 and month <= 3) and (hour >= 23 or hour <= 5) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Day tariff #}
    {% elif (month >= 4) and (hour >= 6 and hour <= 22) %}
      {% set el_taxes = 0.5573 %}

    {# April to December - Night tariff #}
    {% elif (month >= 4) and (hour >= 23 or hour <= 5) %}
      {% set el_taxes = 0.4393 %}
    {% endif %}


    {% if current_price > 0.875 %}
        {% set subs = (current_price - 0.875) * 0.9 %}
        {% set additional_cost = el_taxes - subs %}
    {% elif current_price <= 0.875 %}
        {% set additional_cost = el_taxes %}
    {% endif %}


    {{ additional_cost }}

Describe the bug

I am using the workday integration to determine if today is a holiday or not, as this defines one level of the additional cost here in Norway. This seems to work as expected in the provided template for additional costs, but I cannot find a way to provide the nordpool integration with an additional workday sensor for tomorrows prices.

Debug log


Add your logs here.

@Hellowlol
Copy link
Collaborator

The easiest way i can think is to check if utcnow and now is the same date. The integration patches now so it always in the context of the current hour. So if the day isnt the same its tomorrow.

@magnusoverli
Copy link
Author

Does that mean that the additional cost template is evaluated per hour and takes date into account?

If that is the case I suppose my template should work as intended?

@magnusoverli
Copy link
Author

Ah, no, it does not work.
To summarise… We receive prices for tomorrow at 13:00 here. This means that we need a way to check if tomorrow is a workday, which I currently am unable to see how is done. The workday status defines the level of taxes on electricity, so it is crucial to get a correct reading of the next day price level.

@magnusoverli
Copy link
Author

Or does now() always relate to the hour being evaluated? If that is the case, could it work to compare as_timestamp(now()) | timestamp_custom("%F") against a list of known holidays?

@Hellowlol
Copy link
Collaborator

https://github.com/custom-components/nordpool#additional-costs

Yes, its patched so it always refer to the hour for the price.

@Hellowlol
Copy link
Collaborator

Instead if a static list of holydays you can use https://github.com/partofthething/next-holiday-sensor

@magnusoverli
Copy link
Author

Oh, the list is not static, it is dynamically provided by the Holidays integration (https://github.com/bruxy70/Holidays). It seems it does much of the same as the one you suggest?

@Hellowlol
Copy link
Collaborator

Yeah. All you need just a sensor that has a holiday attribut. The example i provided has dict of all holydays so you can format the datetime to a str and use a .get and check if the value is not None.

@Hellowlol
Copy link
Collaborator

Please post your solution so other can reuse it if they face the same issue.

@magnusoverli
Copy link
Author

magnusoverli commented Dec 7, 2023

Sure! :-)
Within a "it works on my machine" scope, here is my template:

    {# January to March - Weekend and holiday taxes #}
    {% if (as_timestamp(now()) | timestamp_custom("%F")) in (state_attr('calendar.holidays','holidays') | list) and (now().month >= 1 and now().month <= 3) %}
      {% set el_taxes = 0.3558 %}
    {# April to December - Weekend and holiday taxes #}
    {% elif (as_timestamp(now()) | timestamp_custom("%F")) in (state_attr('calendar.holidays','holidays') | list) and (now().month >= 4) %}
      {% set el_taxes = 0.4393 %}
    {# January to March - Dyatime taxes #}
    {% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 6 and now().hour <= 22) %}
      {% set el_taxes = 0.4738 %}
    {# January to March - Night taxes #}
    {% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 23 or now().hour <= 5) %}
      {% set el_taxes = 0.3558 %}
    {# April to December - Day taxes #}
    {% elif (now().month >= 4) and (now().hour >= 6 and now().hour <= 22) %}
      {% set el_taxes = 0.5573 %}
    {# April to December - Night taxes #}
    {% elif (now().month >= 4) and (now().hour >= 23 or now().hour <= 5) %}
      {% set el_taxes = 0.4393 %}
    {% endif %}
    {% if current_price > 0.875 %}
        {% set subs = (current_price - 0.875) * 0.9 %}
        {% set additional_cost = el_taxes - subs %}
    {% elif current_price <= 0.875 %}
        {% set additional_cost = el_taxes %}
    {% endif %}
    {{ additional_cost | round(4) }}

I've also posted it in the HA community forums (HA Foums) and at Hjemmeautomasjon.no (Hjemmeautomasjon)

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