TheFes / cheapest-energy-hours

Jinja macro to find the cheapest energy prices
GNU General Public License v3.0
85 stars 9 forks source link

BUG? #83

Closed rhgiter closed 1 year ago

rhgiter commented 1 year ago

Hi again

mabye I do something wrong, or mabye a bug?

This never change to True. All day is has been False. But if I set hours=2 it change to true

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M:%S",start="6:00", end="17:00", look_ahead=true, look_ahead_minutes=5, mode="is_now") }}

TheFes commented 1 year ago

I'm already working on this. With the current version it will only return true in the first 5 minutes of the current hour.

rhgiter commented 1 year ago

ok, but mine never return TRUE (only tested it for 10 to 17 today)

TheFes commented 1 year ago

well anyway, in the new version it will work differently

TheFes commented 1 year ago

if you want you can try https://github.com/TheFes/cheapest-energy-hours/releases/tag/v5.0.0b0 It should also be available in HACS if you enable beta for this repository

rhgiter commented 1 year ago

This is with the beta version

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} Mellem 21-06, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=3, time_format="%H:%M", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true) }} Mellem 06-17, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true, mode="is_now") }}

image

TheFes commented 1 year ago

are you sure that sensor is working right now? Can you uddate to v5.0.0b1?

If the problem persists, can you give me the output of

{{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true, mode="is_now", debug=true) }}
rhgiter commented 1 year ago

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} Mellem 21-06, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=3.5, time_format="%H:%M", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true) }} Mellem 06-17, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true) }}

I get this error UndefinedError: 'day' is undefined

image

output of this {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true, mode="is_now", debug=true) }}

I get this 'cheapest_energy_hours' is undefined'

image

TheFes commented 1 year ago

I assumed you'd understand you need to import the macro in the last case. I will look into the missing undefined day.

rhgiter commented 1 year ago

hi

I dont know how to import it :-(

/René.

Den man. 13. nov. 2023 kl. 15.48 skrev Martijn van der Pol < @.***>:

I assumed you'd understand you need to import the macro in the last case. I will look into the missing undefined day.

— Reply to this email directly, view it on GitHub https://github.com/TheFes/cheapest-energy-hours/issues/83#issuecomment-1808304514, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOR4UGF4FFQV43FTSNX2P3TYEIXLJAVCNFSM6AAAAAA7IEBDVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBYGMYDINJRGQ . You are receiving this because you authored the thread.Message ID: @.***>

TheFes commented 1 year ago

you are doing that every time...

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}

I just assumed you would understand that that was also needed for the line I requested you to provide the output from.

rhgiter commented 1 year ago

ahh ok get it

same result

UndefinedError: 'day' is undefined

/René.

Den man. 13. nov. 2023 kl. 15.56 skrev Martijn van der Pol < @.***>:

you are doing that every time...

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}

I just assumed you would understand that that was also needed for the line I requested you to provide the output from.

— Reply to this email directly, view it on GitHub https://github.com/TheFes/cheapest-energy-hours/issues/83#issuecomment-1808320162, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOR4UGD2NEXMM6TL4GF4PZ3YEIYLBAVCNFSM6AAAAAA7IEBDVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBYGMZDAMJWGI . You are receiving this because you authored the thread.Message ID: @.***>

TheFes commented 1 year ago

The day thing is fixed in v5.0.0b2

Can you check again, and if it's not working give me the result of:

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true, mode="is_now", debug=true) }}

Please provide as text, not in a screenshot.

rhgiter commented 1 year ago

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true, mode="is_now", debug=true) }}

Mellem 21-06, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=3, time_format="%H:%M", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true) }} Mellem 06-17, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M",start="6:00", end="17:00", look_ahead=true) }}

output is this

{"non_default_user_input":{"sensor":"sensor.energi_data_service","hours":1.0,"start":"6:00","end":"17:00","mode":"is_now","time_format":"%H:%M","look_ahead":true},"set_by_macro":{"start":"2023-11-13T16:10:00+01:00","end":"2023-11-13T17:00:00+01:00","attr_today":"attr_today_not_found","attr_all":"attr_all_not_found","time_key":"hour","value_key":"price","no_weight_points":6,"weight":[1,1,1,1,1,1]},"defaults_used":{"value_on_error":null,"attr_tomorrow":"raw_tomorrow","include_today":true,"include_tomorrow":false,"lowest":true,"precision":5,"price_factor":1,"program":null,"plot_sensor":"sensor.energy_plots","plot_attr":"energy_plots","split":false},"data_used":{"today_count":0,"tomorrow_count":24,"all_count":0,"data_count":24,"values_count":0,"datapoints_source":{"datapoints":1,"datapoints_hour":1},"datapoints_used":{"datapoints":6,"datapoints_hour":6.0}},"output":"Error: No data within current selection"}

Mellem 21-06, start kl. 1 error: No valid data in sensor.energi_data_service Mellem 06-17, start kl. Error: No data within current selection

TheFes commented 1 year ago

how does sensor.energi_data_service look like in developer tools > states (screenshot is fine now)

rhgiter commented 1 year ago

data from sensor.energi_data_service works. And your script works in 4.2.3. (except the 1 hour problem)

image

TheFes commented 1 year ago

seems to be something wrong with the attribute finder, please try:

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours("sensor.energi_data_service", time_key="hour", value_key="price", hours=1, start="6:00", end="17:00", look_ahead=true, mode="is_now", debug=true) }}
rhgiter commented 1 year ago

{"non_default_user_input":{"sensor":"sensor.energi_data_service","hours":1.0,"start":"6:00","end":"17:00","time_key":"hour","value_key":"price","mode":"is_now","look_ahead":true},"set_by_macro":{"start":"2023-11-13T16:20:00+01:00","end":"2023-11-13T17:00:00+01:00","attr_today":"attr_today_not_found","attr_all":"attr_all_not_found","no_weight_points":3,"weight":[1,1,1]},"defaults_used":{"value_on_error":null,"attr_tomorrow":"raw_tomorrow","time_format":null,"include_today":true,"include_tomorrow":false,"lowest":true,"precision":5,"price_factor":1,"program":null,"plot_sensor":"sensor.energy_plots","plot_attr":"energy_plots","split":false},"data_used":{"today_count":0,"tomorrow_count":24,"all_count":0,"data_count":24,"values_count":0,"datapoints_source":{"datapoints":1,"datapoints_hour":1},"datapoints_used":{"datapoints":3,"datapoints_hour":3.0}},"output":"Error: No data within current selection"}

TheFes commented 1 year ago

I'll look into this.

TheFes commented 1 year ago

which integration are you using?

rhgiter commented 1 year ago

https://github.com/MTrab/energidataservice

TheFes commented 1 year ago

can you try again with v5.0.0b3

rhgiter commented 1 year ago

yeah ! it seems like something work :-), but the time start 6:00 to 17:00 shows 04:00

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} Mellem 21-06, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=3, time_format="%H:%M", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true) }} Mellem 06-17, start kl. {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M", start="6:00", end="17:00", look_ahead=true,include_tomorrow=true) }}

output

Mellem 21-06, start kl. 02:00 Mellem 06-17, start kl. 04:00

(the lines breaks)

image

TheFes commented 1 year ago

that will be 4:00 tomorrow. As you strip of the date, you will be unable to see if it's today or tomorrow.

TheFes commented 1 year ago

Does this version also solve your issue with is_now?

rhgiter commented 1 year ago

I will test tonight (cheap hour 4:00)

is it safe to remove 'look_ahead_minutes=5'

TheFes commented 1 year ago

yes, it doesn't do anything anymore.

rhgiter commented 1 year ago

sorry, but it did not work

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M:%S", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true, mode="is_now") }}

and this either

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=3.5, time_format="%H:%M:%S", start="21:00", end="06:00", include_tomorrow=true, look_ahead=true, mode="is_now") }}

but this started at 6:00

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=3.5, time_format="%H:%M:%S",start="6:00", end="17:00", look_ahead=true, mode="is_now") }}

rhgiter commented 1 year ago

But I can see the time stamp change with 5 minutes, so that is ok :-)

TheFes commented 1 year ago

Yes, I see what your issue is. You need to dynamically change several of the parameters.

If you set start="21:00" that will be 21:00 today after midnight. So as soon as it's passed midnight, this will move one day. That's why it didn't change to true at 4:00 this night.

This should work.

{%- set day = timedelta(days=1) -%}
{%- set start = today_at("21:00") -%}
{%- set end = today_at("06:00") -%}
{%- set after_mn = now() < end -%}
{%- set start = start - day if after_mn else start -%}
{%- set end = end of after_mn else end + day -%}
{%- set sensor = "sensor.energi_data_service" -%}
{%- from "cheapest_energy_hours.jinja" import cheapest_energy_hours -%}
{{ cheapest_energy_hours(sensor, hours=3.5, start=start, end=end, look_ahead=true, mode="is_now") }}

BTW you keep adding the time format in your parameters, which is basically useless with mode="is_now" as it will return true or false and not a time.

Note that this does start a new period of 3.5 hours after midnight, so if the cheapest period starts at 23:00 it will have 1 hour before midnight and 3.5 hours after. I will give a better solution later this day.

Also note that this should have turned true before 4:00 last night, as there are only 2 hours left until 6:00 then.

TheFes commented 1 year ago

maybe this will help https://github.com/TheFes/cheapest-energy-hours/blob/docs_update/documentation/6a-dishwasher_overnight.md

rhgiter commented 1 year ago

Thanks, I'll check it out.

/René.

Den tirs. 14. nov. 2023 kl. 11.57 skrev Martijn van der Pol < @.***>:

maybe this will help

https://github.com/TheFes/cheapest-energy-hours/blob/docs_update/documentation/6a-dishwasher_overnight.md

— Reply to this email directly, view it on GitHub https://github.com/TheFes/cheapest-energy-hours/issues/83#issuecomment-1809984947, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOR4UGCDCHK6RAADX2PDDO3YENFCHAVCNFSM6AAAAAA7IEBDVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBZHE4DIOJUG4 . You are receiving this because you authored the thread.Message ID: @.***>

TheFes commented 1 year ago

Were you already able to get it working?

rhgiter commented 1 year ago

hi

I will check it soon, not home right now

rhgiter commented 1 year ago

hi I shall only use this https://github.com/TheFes/cheapest-energy-hours/blob/docs_update/documentation/6a-dishwasher_overnight.md

when the time is between today and tomorrow ex. today 21::00 to next day 06:00

or does this also works {% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=4, time_format="%H:%M:%S",start="21:00", end="06:00", look_ahead=true, mode="is_now") }}

It seems like the 1 hour template works now {% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %} {{ cheapest_energy_hours("sensor.energi_data_service", hours=1, time_format="%H:%M:%S",start="16:21", end="17:30", look_ahead=true, mode="is_now") }}

TheFes commented 1 year ago

What are you trying to achieve? With look_ahead=true it will move with the current time, so it will be true during multiple hours in the time period between 21:00 today and 7:00 tomorrow

rhgiter commented 1 year ago

I will test over night.

I got confused by this https://github.com/TheFes/cheapest-energy-hours/blob/docs_update/documentation/6a-dishwasher_overnight.md

TheFes commented 1 year ago

Please explain what you are trying to achieve and where you are using the template

So:

  1. What is your goal (eg find the best moment to turn on device X between time Y and Z)
  2. Explain where you are trying to use this template (eg automation trigger, or template sensor)

I think you also missed that I'm proposing you use the trigger based template sensor as explained in that example, for the reasons mentioned there.

rhgiter commented 1 year ago

yes

  1. this is what I want : to find the best moment to turn on device X between time Y and Z
  2. I use it in a automation

I know the trigger should be time_pattern ex. platform: time_pattern minutes: /15

but for now I test it this way

alias: opvaskemaskine billigste periode nat (4) description: "" trigger:

TheFes commented 1 year ago

Please use code formatting on your code, the indentation gets lost like this. But no, you should not use this trigger, and also not a time patter trigger.

You should use what I describe in that example.

Assume the following, the prices are

21:00 -  0.11
22:00 - 0.14
23:00 - 0.12
00:00 - 0.16
01:00 - 0.15
02:00 - 0.18
03:00 - 0.18
04:00 - 0.17
05:00 - 0.17
06:00 - 0.17

With your current trigger, it will trigger at 21:00, but after 22:00 is passed, it will look for the best price again, as you are using look_ahead=true so it will trigger again at 23:00.

Then, after midnight, it won't trigger again before 21:00 of that day, because that's the new start time. A time pattern trigger won't fix that as well.

To avoid these issues, I've set up the example I linked to. You need to create a template sensor with a timestamp, and use that in a time trigger in your automation. That example is exactly the use case you are trying to use it for.

rhgiter commented 1 year ago

ok, I will try it... but I dont need to make the template sensor if its on the same day - right?

TheFes commented 1 year ago

No, but usage of look_ahead can also cause issues there. I didn't really realize that myself until now.

rhgiter commented 1 year ago

my appliances started last night as they should. :-) using template.

I will test the template sensor in the weekend.

TheFes commented 1 year ago

Ah great! Good to hear!

rhgiter commented 1 year ago

I get this error

image

when I use the code in configuration.yaml

image

template:

TheFes commented 1 year ago

again, please sue code formatting on your code, if you don't do that it becomes jibberish. You can do that with the < > button

And please share the actual error as shown in the log by pressing on the Show logs button

TheFes commented 1 year ago

there is a quote missing after '8:00

rhgiter commented 1 year ago

sorry , but with version 5

this was triggered at 04:00, I should have been 02:00 ` platform: template value_template: > {% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}

{{ cheapest_energy_hours("sensor.energi_data_service", hours=4, time_format="%H:%M:%S", start="00:00", end="06:00", include_tomorrow=true, look_ahead=true,mode="is_now") `}} ``

image

rhgiter commented 1 year ago

there is a quote missing after '8:00

okay, I just copy/paste code from https://github.com/TheFes/cheapest-energy-hours/blob/docs_update/documentation/6a-dishwasher_overnight.md

(link dont work a the moment) image

the code work ok now :-) but I need the automation code for testing.

edit: new link https://github.com/TheFes/cheapest-energy-hours/blob/main/documentation/6a-dishwasher_overnight.md

TheFes commented 1 year ago

The branch was merged with the main branch, so it was indeed moved.

But you found it again.

What you fail to understand with your own template is that your end time moves a day after midnight. So before last midnight it would be 6:00 this mornings, but after midnight this moved to 6:00 tomorrow morning.

rhgiter commented 1 year ago

The branch was merged with the main branch, so it was indeed moved.

But you found it again.

What you fail to understand with your own template is that your end time moves a day after midnight. So before last midnight it would be 6:00 this mornings, but after midnight this moved to 6:00 tomorrow morning.

ahh I see now - I have to change 'include_tomorrow=true' to 'include_tomorrow=false' or remove it ---- Right?