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

TypeError: 'datetime.datetime' object is not callable #259

Closed
woopstar opened this issue Feb 10, 2023 · 38 comments · Fixed by #262
Closed

TypeError: 'datetime.datetime' object is not callable #259

woopstar opened this issue Feb 10, 2023 · 38 comments · Fixed by #262
Labels
bug Something isn't working

Comments

@woopstar
Copy link

The latest release v1.2.2 gives me this issue. I think it's because of the template used.

  File "/config/custom_components/energidataservice/sensor.py", line 619, in _calculate
    template_value = self._cost_template.async_render(
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 459, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: 'datetime.datetime' object is not callable

Template:

{% set costs = {   
    "nettarif_c_time_sommer_lav": 0.1212,   
    "nettarif_c_time_sommer_hoj": 0.1818,   
    "nettarif_c_time_sommer_spids": 0.4726,   
    "nettarif_c_time_vinter_lav": 0.1212,   
    "nettarif_c_time_vinter_hoj": 0.3635,   
    "nettarif_c_time_vinter_spids": 1.0905,   
    "abonnement_net": 0.046,   
    "spotpris_tillaeg": 0.012,   
    "transmission_nettarif": 0.05800,   
    "systemtarif": 0.05400,   
    "abonnement_el_leverandor": 0, 
} %}  

{% if states("sensor.hovedforsyning_energy_total_current_month") | float < 336 %}   
    {% if now().month >= 1 and now().month < 7 %}     {% set elafgift = 0.008 %}   {% else %}     {% set elafgift = 0.680 %}   {% endif %} {% else %}   {% set elafgift = 0.008  %} {% endif %}  {% set sum_price = costs.transmission_nettarif +        costs.spotpris_tillaeg +        costs.systemtarif +         elafgift +        costs.abonnement_net +        costs.abonnement_el_leverandor  %}  {% if now().month >= 10 or now().month < 4 %}   {% if now().hour >= 0 and now().hour < 6 %}     {{ sum_price + costs.nettarif_c_time_vinter_lav }}   {% endif %}   {% if now().hour >= 6 and now().hour < 17 %}     {{ sum_price + costs.nettarif_c_time_vinter_hoj }}   {% endif %}   {% if now().hour >= 17 and now().hour < 21 %}     {{ sum_price + costs.nettarif_c_time_vinter_spids }}   {% endif %}   {% if now().hour >= 21 and now().hour <= 23 %}     {{ sum_price + costs.nettarif_c_time_vinter_hoj }}   {% endif %} {% else %}   {% if now().hour >= 0 and now().hour < 6 %}     {{ sum_price + costs.nettarif_c_time_sommer_lav }}   {% endif %}   {% if now().hour >= 6 and now().hour < 17 %}     {{ sum_price + costs.nettarif_c_time_sommer_hoj }}   {% endif %}   {% if now().hour >= 17 and now().hour < 21 %}     {{ sum_price + costs.nettarif_c_time_sommer_spids }}   {% endif %}   {% if now().hour >= 21 and now().hour <= 23 %}     {{ sum_price + costs.nettarif_c_time_sommer_hoj }}   {% endif %} {% endif %}

The template works in Developer Tools.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

There was some changes in the templates I had to make.
Could you please try removing () after your now vars? Perhaps that could be the issue.

@woopstar
Copy link
Author

Removing () in now vars throws this error:

image

Throwing it into developer tools gives this error, as expected:

image

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Okay - will look into this.

You are aware that you can just enable automatic tariff data, right?

@MTrab MTrab added the bug Something isn't working label Feb 10, 2023
@woopstar
Copy link
Author

Yes. It's super nice, but it does not work for me after 1/7 when the energy charge changes again. I have a heat pump and hence reduced energy charge prices. So I need to calculate that the price changes every month when I have consumed 334 kWh.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

No need to miss out on auto tariffs just for that.

You have the tariff sum as a variable in the templates.

@woopstar
Copy link
Author

No need to miss out on auto tariffs just for that.

You are right. So with Vindstød VE Strøm, and looking into https://fm.dk/media/26367/faktaark_lempelse-af-elafgift-til-minimumssats-i-seks-maaneder.pdf

The template should look like this:

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 336 and now().month > 6 %}   
  {% set elafgift_reduction = (69.7 * 0.8 - 0.8 * 0.8) | float %}
{% endif %}

{{ 0.012 - elafgift_reduction|default(0) | float(0) }}

So it subtracts the diff between the regular energy price of 69.7 incl VAT and the reduced one incl VAT.

I multiply with 0.8 to remove VAT as I believe you add VAT automatically ?

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Don't know what the 69.7 you have is, but yes - all prices in the template is WITHOUT VAT, as the integration calculates this automatically based on your settings.

@woopstar
Copy link
Author

woopstar commented Feb 10, 2023

According to FM, the energy price will be 69,7 øre kr/kWh in H2 2023. But ofc, it's øre, so the above needs to be reduced by 100 :)

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 336 and now().month > 6 %}   
  {% set elafgift_reduction = (0.697 * 0.8 - 0.008 * 0.8) | float %}
{% endif %}

{{ 0.012 - elafgift_reduction|default(0) | float(0) }}

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

0.008 * 0.8 ??

Shouldn't it just be 0.008?
Perhaps I should just add the elafgift as a variable as well if auto tariff is enabled, then you can just use this variable

@woopstar
Copy link
Author

So ... is the 0.008 with or without VAT ... hmm

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

0.008 is without. Current elafgift is 0.008 without VAT and 0.01 with VAT

@woopstar
Copy link
Author

Ah. I see. then 0.697 is also without VAT.

Exposing the elafgift could work. So you could just override it when you have used more than 336 kWh and set it fixed to 0.008

@woopstar
Copy link
Author

With #261 would the use case then be like this?

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 336 and now().month > 6 %}   
  {% set el_afgift = 0.008 %}
{% endif %}
{{ 0.012 | float(0) }}

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

More like this - without having done any tests to confirm yet

{% set reduction = 0
{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 336 and now().month > 6 %}   
  {% reduction = el_afgift %}
{% endif %}
{{ 0.012 - reduction | float(0) }}

It's not possible to send variable changes back from templates, as far as I can see.

@woopstar
Copy link
Author

I'm pretty sure if you do not do anything in your template, the calculated price will be using the regular charged price of 0.697, but with reduced charge pricing due to heat pumps, you'd want to reduce it to 0.008 when you have consumed 334 kWh during the month. Current rules are that you get reduced charge prices when you have consumed 4.000 kWh a year, which is normally divided into the months hence 4.000 / 12 = 334 kWh in the current month.

So I believe you must subtract 0.689 (0.697-0.008) from the current calculated price. This means this should be correct:

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 334 and now().month > 6 %}   
  {% set reduction = ( el_afgift - 0.008 ) | float(0) %}
{% endif %}

{{ 0.012 - reduction|default(0) | float(0) }}

This would remove the difference between the current charged price el_afgift, which is currently 0.697 in H2 2023, and the reduced pricing of 0.008

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Not into the reduction rules anymore, but you are right - my example reduced by the full amount.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

I'll release the patch later today, and if you then could confirm the template is working, I think I'll add the example to the wiki, if that is okay for you.

@woopstar
Copy link
Author

If you release a beta release, I can test it out right away.

Yes, let's add it to the wiki. I think a lot of people could benefit from the example if they monitor their usage and have heat pumps giving them reduced charged prices. But ofc it requires a sensor.

We could also possibly look into that you can provide a sensor entity to EDS during config with the current month's power consumption, and then it auto-calculates.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Just pushed 1.2.3 as a pre-release, please do test and let me know if it works

@woopstar
Copy link
Author

woopstar commented Feb 10, 2023

Just pushed 1.2.3 as a pre-release, please do test and let me know if it works

I can confirm 1.2.3 works with the regular template again, as mentioned in the issue text.

Also tested with auto tariff and the following template:

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 334 and now().month > 6 %}   
  {% set reduction = ( el_afgift - 0.008 ) | float(0) %}
{% endif %}

{{ 0.012 + (402/365/24/100) - reduction|default(0) | float(0) }}

Also works.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

I'll mark it as a normal release at once and get that template in the wiki - thanks :)

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Can you explain this?

(402/365/24/100)

@woopstar
Copy link
Author

Ah. Yes.

Vores Elnet which is my provider has a yearly fee of 402 kr ex VAT (abonnement). So I added it to the kW price too in my case to get my total of "what I'm paying to get power".

You should leave it out in the wiki.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

So just to confirm - this should be the one for the wiki?

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 334 and now().month > 6 %}   
  {% set reduction = ( el_afgift - 0.008 ) | float(0) %}
{% endif %}

{{ 0.012 - reduction | default(0) | float(0) }}

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

And another thing - isn't the reduction for all the months of the year, and not just for the last 6 months?

@woopstar
Copy link
Author

So just to confirm - this should be the one for the wiki?

{% if states("sensor.hovedforsyning_energy_total_current_month") | float > 334 and now().month > 6 %}   
  {% set reduction = ( el_afgift - 0.008 ) | float(0) %}
{% endif %}

{{ 0.012 - reduction | default(0) | float(0) }}

And another thing - isn't the reduction for all the months of the year, and not just for the last 6 months?

You are right. Which means we could remove and now().month > 6 from the if clause, because the reduction would just be 0 then, since the current charge price is 0.008.

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Right :)

@woopstar
Copy link
Author

Using that template something odd happens. It gives me:

image

But it works in Developer Tools.

@woopstar
Copy link
Author

2023-02-10 15:38:26.268 ERROR (MainThread) [custom_components.energidataservice.config_flow] UndefinedError: 'el_afgift' is undefined
2023-02-10 15:38:51.340 ERROR (MainThread) [custom_components.energidataservice.config_flow] UndefinedError: 'el_afgift' is undefined
2023-02-10 15:39:54.901 ERROR (MainThread) [custom_components.energidataservice.config_flow] UndefinedError: 'el_afgift' is undefined

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Need to check to source - that doesn't sound right

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Strange - the source dictates el_afgift
image

@woopstar
Copy link
Author

It does not like to use it inside the template.

Seems it did not test the el_afgift variable because of the month checking ofc. 🗡️ 🦤

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

I've updated the wiki to reflect the working variable

@woopstar
Copy link
Author

hmm.. none of them seem to work

2023-02-10 15:48:33.497 ERROR (MainThread) [custom_components.energidataservice.config_flow] UndefinedError: 'el_afgift' is undefined
2023-02-10 15:50:21.998 ERROR (MainThread) [custom_components.energidataservice.config_flow] UndefinedError: 'elafgift' is undefined

Do they work for you?

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Nope - but I think I know why ;)

@MTrab
Copy link
Owner

MTrab commented Feb 10, 2023

Should be fixed now - and the correct variable is el_afgift ;)

@woopstar
Copy link
Author

Should be fixed now - and the correct variable is el_afgift ;)

Yes. Works now.

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

Successfully merging a pull request may close this issue.

2 participants