TheFes / cheapest-energy-hours

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

Did not matches the lowest prices #128

Closed darius71 closed 7 months ago

darius71 commented 7 months ago

I am new on Home Assitant.

Automation do not work cheapest according cheapest prices:

  1. Triggered by the state of binary_sensor.maziausia_kaina at February 22, 2024 at 10:05:36. Toggle Gyvatukas Ia Socket (switch.gyvatukas_ia_socket) turned on.
  2. Triggered by the state of binary_sensor.maziausia_kaina at February 22, 2024 at 17:52:42. Toggle Gyvatukas Ia Socket (switch.gyvatukas_ia_socket) turned off

After that, sensor did not triggered at all...

Automation:

alias: Jungiam gyvatukus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.maziausia_kaina
    to:
      - "on"
      - "off"
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: switch.gyvatukas_ia_socket
        state: "{{ states(\"binary_sensor.maziausia_kaina\") }}"
action:
  - type: toggle
    device_id: 5bc4d17541b8b9edac6813674d5a95f0
    entity_id: switch.gyvatukas_ia_socket
    domain: switch
mode: single

Template:

binary_sensor:
  name: maziausia kaina
  unique_id: d4d8b8b6-66fa-4be8-872d-5fd0218bc842
  state: >
    {% set n = now() %}
    {% set sensor = 'sensor.energy_prices' -%}
    {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
    {{ cheapest_energy_hours(sensor=sensor, hours=2, split=true, include_tomorrow=false, mode="is_now") }}

Have any ideas?

Thank you

TheFes commented 7 months ago

Please add code tags around the yaml code to make it readable.

TheFes commented 7 months ago

Can you put this in developer tools > template

{% set n = now() %}
{% set sensor = 'sensor.energy_prices' -%}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor=sensor, hours=2, split=true, include_tomorrow=false, mode="all") }}

It will show you when the time blocks are when the price is lowest, and thus, when the binary sensor should be on

darius71 commented 7 months ago

Result of developer tool: [{"start":"2024-02-23T00:00:00+02:00","end":"2024-02-23T01:00:00+02:00","hours":1.0,"prices":[0.0049],"is_now":false},{"total_hours":2.0,"datapoints_per_hour":1,"datapoints":2}

But no action at "2024-02-23T00:00:00+02:00"

TheFes commented 7 months ago

Did you already create this binary sensor yesterday?

TheFes commented 7 months ago

wait, can you have a look at the history of the binary sensor? Was it on between 00:00 and 02:00 last night?

TheFes commented 7 months ago

The problem is most probably in your condition. State conditions don't accept templates. Use a template condition instead

alias: Jungiam gyvatukus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.maziausia_kaina
    to:
      - "on"
      - "off"
condition:
  - condition: state
     value_template: "{{ states('switch.gyvatukas_ia_socket') != states('binary_sensor.maziausia_kaina') }}"
action:
  - service: switch.toggle
    target:
      entity_id: switch.gyvatukas_ia_socket
mode: single
TheFes commented 7 months ago

You can also do it without the condition:

alias: Jungiam gyvatukus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.maziausia_kaina
    to:
      - "on"
      - "off"
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.gyvatukas_ia_socket
mode: single
darius71 commented 7 months ago

No actions on last night. Thank you. I will try.

TheFes commented 7 months ago

Can you show a screenshot of the history of the binary sensor?

darius71 commented 7 months ago
alias: Jungiam gyvatukus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.maziausia_kaina
    to:
      - "on"
      - "off"
condition:
  - condition: state
     value_template: "{{ states('switch.gyvatukas_ia_socket') != states('binary_sensor.maziausia_kaina') }}"
action:
  - service: switch.toggle
    target:
      entity_id: switch.gyvatukas_ia_socket
mode: single

error: Message malformed: extra keys not allowed @ data['condition'][0]['value_template'] I have already try without conditions everything ok.

darius71 commented 7 months ago

Can you show a screenshot of the history of the binary sensor? Piešinys be pavadinimo (1)

darius71 commented 7 months ago

Can you show a screenshot of the history of the binary sensor? Piešinys be pavadinimo (1)

image
TheFes commented 7 months ago

It should have been on between 00:00 and 02:00 last night

TheFes commented 7 months ago

Can you send the result of this (from devtools > template)

{% set n = now() %}
{% set sensor = 'sensor.energy_prices' -%}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor=sensor, hours=24, split=true, include_tomorrow=false, mode="is_now", debug=true) }}
darius71 commented 7 months ago

{% set n = now() %} {% set sensor = 'sensor.energy_prices' -%} {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %} {{ cheapest_energy_hours(sensor=sensor, hours=24, split=true, include_tomorrow=false, mode="is_now", debug=true) }} This is result: {"version":"5.0.2","output":false,"error":false,"non_default_user_input":{"sensor":"sensor.energy_prices","hours":24.0,"mode":"is_now","split":true},"set_by_macro":{"sensor":"sensor.nordpool_kwh_lt_eur_4_08_021","start":"2024-02-23T00:00:00+02:00","end":"2024-02-24T00:00:00+02:00","no_weight_points":1},"defaults_used":{"value_on_error":null,"attr_today":"raw_today","attr_tomorrow":"raw_tomorrow","attr_all":"prices","time_key":"start","value_key":"value","time_format":null,"include_today":true,"include_tomorrow":false,"lowest":true,"look_ahead":false,"precision":5,"price_factor":1,"weight":null,"program":null,"plot_sensor":"sensor.energy_plots","plot_attr":"energy_plots"},"data_used":{"today_count":24,"tomorrow_count":24,"all_count":0,"data_count":48,"values_count":24,"datapoints_source":{"datapoints":24,"datapoints_hour":1},"datapoints_used":{"datapoints":24,"datapoints_hour":1}}}

TheFes commented 7 months ago

You are on version 5.0.2 There was a bug in is_now mode in combination with split=true which is fixed in v5.3.0

https://github.com/TheFes/cheapest-energy-hours/releases/tag/v5.3.0

darius71 commented 7 months ago

You are on version 5.0.2 There was a bug in is_now mode in combination with split=true which is fixed in v5.3.0

https://github.com/TheFes/cheapest-energy-hours/releases/tag/v5.3.0

Ok I will try to update. But if I will use "is_now" without "split=true", then everything will be ok?

TheFes commented 7 months ago

Just update, did you install it using HACS?

You can also use it without split=true, but that might not give you the best results

darius71 commented 7 months ago

Just update, did you install it using HACS?

You can also use it without split=true, but that might not give you the best results

Not sure. Do not remember. Maybe not.

TheFes commented 7 months ago

well, if not, just copy the contents for the cheapest_energy_hours.jinja file on this repository, and replace yours with that

darius71 commented 7 months ago

Thank you very much. Have a nice weekend!

darius71 commented 7 months ago

Hi, one more time. I found out, that hours=2 and split=true do not work like expected. It is developers tool-templates: {% set n = now() %} {% set sensor = 'sensor.energy_prices' -%} {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %} {{ cheapest_energy_hours(sensor=sensor, hours=2, split=true, include_tomorrow=false, mode="all") }}

Result: [ { "start": "2024-02-26T00:00:00+02:00", "end": "2024-02-26T01:00:00+02:00", "hours": 1, "prices": [ 0.06 ], "is_now": false }, { "total_hours": 2, "datapoints_per_hour": 1, "datapoints": 2 } ]

2 hours in different times expected to return, am I right?

But everything ok with 3 hours: [ { "start": "2024-02-26T00:00:00+02:00", "end": "2024-02-26T01:00:00+02:00", "hours": 1, "prices": [ 0.06 ], "is_now": false }, { "start": "2024-02-26T04:00:00+02:00", "end": "2024-02-26T06:00:00+02:00", "hours": 2, "prices": [ 0.0627, 0.0644 ], "is_now": false }, { "total_hours": 3, "datapoints_per_hour": 1, "datapoints": 3 } ]

TheFes commented 7 months ago

Hmm, I can't reproduce that, with the same template I get

[
  {
    "start": "2024-02-26T03:00:00+01:00",
    "end": "2024-02-26T05:00:00+01:00",
    "hours": 2,
    "prices": [
      0.063,
      0.064
    ],
    "is_now": false
  },
  {
    "total_hours": 2,
    "datapoints_per_hour": 1,
    "datapoints": 2
  }
]

Can you send the full content of the prices attribute of your sensor, so I can test with the same data?

darius71 commented 7 months ago

Hmm, I can't reproduce that, with the same template I get

[
  {
    "start": "2024-02-26T03:00:00+01:00",
    "end": "2024-02-26T05:00:00+01:00",
    "hours": 2,
    "prices": [
      0.063,
      0.064
    ],
    "is_now": false
  },
  {
    "total_hours": 2,
    "datapoints_per_hour": 1,
    "datapoints": 2
  }
]

Can you send the full content of the prices attribute of your sensor, so I can test with the same data?

state_class: total
average: 0.0911625
off_peak_1: 0.0678375
off_peak_2: 0.10085
peak: 0.10348333333333333
min: 0.06
max: 0.1156
mean: 0.09395
unit: kWh
currency: EUR
country: Lithuania
region: LT
low_price: false
price_percent_to_average: 1.032222679281503
today:
- 0.06
- 0.0683
- 0.0671
- 0.0658
- 0.0627
- 0.0644
- 0.0701
- 0.0843
- 0.0963
- 0.1113
- 0.1114
- 0.0879
- 0.1114
- 0.1113
- 0.1113
- 0.1114
- 0.0992
- 0.0872
- 0.0941
- 0.109
- 0.1151
- 0.1156
- 0.0938
- 0.0789
tomorrow:
- 0.0735
- 0.0697
- 0.0696
- 0.0694
- 0.0688
- 0.0694
- 0.0819
- 0.1106
- 0.1164
- 0.1345
- 0.1259
- 0.113
- 0.0968
- 0.1093
- 0.0716
- 0.078
- 0.0863
- 0.0966
- 0.1333
- 0.1359
- 0.1273
- 0.1021
- 0.1178
- 0.0909
tomorrow_valid: true
raw_today:
- start: "2024-02-26T00:00:00+02:00"
end: "2024-02-26T01:00:00+02:00"
value: 0.06
- start: "2024-02-26T01:00:00+02:00"
end: "2024-02-26T02:00:00+02:00"
value: 0.0683
- start: "2024-02-26T02:00:00+02:00"
end: "2024-02-26T03:00:00+02:00"
value: 0.0671
- start: "2024-02-26T03:00:00+02:00"
end: "2024-02-26T04:00:00+02:00"
value: 0.0658
- start: "2024-02-26T04:00:00+02:00"
end: "2024-02-26T05:00:00+02:00"
value: 0.0627
- start: "2024-02-26T05:00:00+02:00"
end: "2024-02-26T06:00:00+02:00"
value: 0.0644
- start: "2024-02-26T06:00:00+02:00"
end: "2024-02-26T07:00:00+02:00"
value: 0.0701
- start: "2024-02-26T07:00:00+02:00"
end: "2024-02-26T08:00:00+02:00"
value: 0.0843
- start: "2024-02-26T08:00:00+02:00"
end: "2024-02-26T09:00:00+02:00"
value: 0.0963
- start: "2024-02-26T09:00:00+02:00"
end: "2024-02-26T10:00:00+02:00"
value: 0.1113
- start: "2024-02-26T10:00:00+02:00"
end: "2024-02-26T11:00:00+02:00"
value: 0.1114
- start: "2024-02-26T11:00:00+02:00"
end: "2024-02-26T12:00:00+02:00"
value: 0.0879
- start: "2024-02-26T12:00:00+02:00"
end: "2024-02-26T13:00:00+02:00"
value: 0.1114
- start: "2024-02-26T13:00:00+02:00"
end: "2024-02-26T14:00:00+02:00"
value: 0.1113
- start: "2024-02-26T14:00:00+02:00"
end: "2024-02-26T15:00:00+02:00"
value: 0.1113
- start: "2024-02-26T15:00:00+02:00"
end: "2024-02-26T16:00:00+02:00"
value: 0.1114
- start: "2024-02-26T16:00:00+02:00"
end: "2024-02-26T17:00:00+02:00"
value: 0.0992
- start: "2024-02-26T17:00:00+02:00"
end: "2024-02-26T18:00:00+02:00"
value: 0.0872
- start: "2024-02-26T18:00:00+02:00"
end: "2024-02-26T19:00:00+02:00"
value: 0.0941
- start: "2024-02-26T19:00:00+02:00"
end: "2024-02-26T20:00:00+02:00"
value: 0.109
- start: "2024-02-26T20:00:00+02:00"
end: "2024-02-26T21:00:00+02:00"
value: 0.1151
- start: "2024-02-26T21:00:00+02:00"
end: "2024-02-26T22:00:00+02:00"
value: 0.1156
- start: "2024-02-26T22:00:00+02:00"
end: "2024-02-26T23:00:00+02:00"
value: 0.0938
- start: "2024-02-26T23:00:00+02:00"
end: "2024-02-27T00:00:00+02:00"
value: 0.0789
raw_tomorrow:
- start: "2024-02-27T00:00:00+02:00"
end: "2024-02-27T01:00:00+02:00"
value: 0.0735
- start: "2024-02-27T01:00:00+02:00"
end: "2024-02-27T02:00:00+02:00"
value: 0.0697
- start: "2024-02-27T02:00:00+02:00"
end: "2024-02-27T03:00:00+02:00"
value: 0.0696
- start: "2024-02-27T03:00:00+02:00"
end: "2024-02-27T04:00:00+02:00"
value: 0.0694
- start: "2024-02-27T04:00:00+02:00"
end: "2024-02-27T05:00:00+02:00"
value: 0.0688
- start: "2024-02-27T05:00:00+02:00"
end: "2024-02-27T06:00:00+02:00"
value: 0.0694
- start: "2024-02-27T06:00:00+02:00"
end: "2024-02-27T07:00:00+02:00"
value: 0.0819
- start: "2024-02-27T07:00:00+02:00"
end: "2024-02-27T08:00:00+02:00"
value: 0.1106
- start: "2024-02-27T08:00:00+02:00"
end: "2024-02-27T09:00:00+02:00"
value: 0.1164
- start: "2024-02-27T09:00:00+02:00"
end: "2024-02-27T10:00:00+02:00"
value: 0.1345
- start: "2024-02-27T10:00:00+02:00"
end: "2024-02-27T11:00:00+02:00"
value: 0.1259
- start: "2024-02-27T11:00:00+02:00"
end: "2024-02-27T12:00:00+02:00"
value: 0.113
- start: "2024-02-27T12:00:00+02:00"
end: "2024-02-27T13:00:00+02:00"
value: 0.0968
- start: "2024-02-27T13:00:00+02:00"
end: "2024-02-27T14:00:00+02:00"
value: 0.1093
- start: "2024-02-27T14:00:00+02:00"
end: "2024-02-27T15:00:00+02:00"
value: 0.0716
- start: "2024-02-27T15:00:00+02:00"
end: "2024-02-27T16:00:00+02:00"
value: 0.078
- start: "2024-02-27T16:00:00+02:00"
end: "2024-02-27T17:00:00+02:00"
value: 0.0863
- start: "2024-02-27T17:00:00+02:00"
end: "2024-02-27T18:00:00+02:00"
value: 0.0966
- start: "2024-02-27T18:00:00+02:00"
end: "2024-02-27T19:00:00+02:00"
value: 0.1333
- start: "2024-02-27T19:00:00+02:00"
end: "2024-02-27T20:00:00+02:00"
value: 0.1359
- start: "2024-02-27T20:00:00+02:00"
end: "2024-02-27T21:00:00+02:00"
value: 0.1273
- start: "2024-02-27T21:00:00+02:00"
end: "2024-02-27T22:00:00+02:00"
value: 0.1021
- start: "2024-02-27T22:00:00+02:00"
end: "2024-02-27T23:00:00+02:00"
value: 0.1178
- start: "2024-02-27T23:00:00+02:00"
end: "2024-02-28T00:00:00+02:00"
value: 0.0909
current_price: 0.0941
additional_costs_current_hour: 0
price_in_cents: false
unit_of_measurement: EUR/kWh
device_class: monetary
icon: mdi:flash
friendly_name: nordpool_kwh_lt_eur_4_08_021
TheFes commented 7 months ago

Fixed in 5.4.1