VIA
(Ville)
April 24, 2024, 1:09pm
21
I created helpers for this and tried this to the end of the sensor:
{{ ns.highest_price }}
service: input_number.set_value
data:
value: "{{ ns.highest_price | float(0)}}"
target:
entity_id: input_number.nord_highest_cost
service: input_number.set_value
data:
value: "{{ ns.second_highest_price | float(0)}}"
target:
entity_id: input_number.nord_2nd_highest_cost
service: input_datetime.set_datetime
data:
time: "{{ ns.expensive_time | timestamp_custom('%H:%M', true)}}"
target:
entity_id: input_datetime.nord_highest
service: input_datetime.set_datetime
data:
time: "{{ ns.second_expensive_time | timestamp_custom('%H:%M', true)}}"
target:
entity_id: input_datetime.nord_2nd_highest
This aint working either… all helpers are still null
I am finding a lot of ways this is NOT working
petro
(Petro)
April 24, 2024, 3:21pm
22
This is why AI is bad, it lies to you. This will create the sensor.
template:
- trigger:
- platform: state
entity_id: sensor.nordpool_kwh_fi_eur_3_10_0
action:
- variables:
calculation: >
{% set hours_ahead = states('input_number.hours_ahead') | int %}
{% set current_time = as_timestamp(now()) %}
{% set ns = namespace(times=[], prices=[], highest_price=0, second_highest_price=0, expensive_time=now(), second_expensive_time=now(), hour_counter=0) %}
{% set today = state_attr('sensor.nordpool_kwh_fi_eur_3_10_0','raw_today') %}
{% for hours in today -%}
{% if ns.hour_counter < hours_ahead -%}
{% set retrieved_time = as_timestamp(as_local(hours.start)) %}
{% if retrieved_time > current_time - 3600 %}
{% set retrieved_price = (hours.value | float(0) + states('input_number.nordpool_additional_cost') | float(0)) %}
{% if retrieved_price > ns.highest_price %}
{% set ns.second_highest_price = ns.highest_price %}
{% set ns.second_expensive_time = ns.expensive_time %}
{% set ns.highest_price = retrieved_price %}
{% set ns.expensive_time = retrieved_time %}
{% elif retrieved_price > ns.second_highest_price %}
{% set ns.second_highest_price = retrieved_price %}
{% set ns.second_expensive_time = retrieved_time %}
{% endif %}
{% set ns.hour_counter = ns.hour_counter + 1 %}
{% endif %}
{% endif %}
{% endfor %}
{% if ns.hour_counter < hours_ahead -%}
{% set tomorrow = state_attr('sensor.nordpool_kwh_fi_eur_3_10_0','raw_tomorrow') %}
{% for hours in tomorrow %}
{% if ns.hour_counter < hours_ahead %}
{% set retrieved_time = as_timestamp(as_local(hours.start)) %}
{% if retrieved_time > current_time - 3600 %}
{% set retrieved_price = (hours.value | float(0) + states('input_number.nordpool_additional_costs') | float(0)) %}
{% if retrieved_price > ns.highest_price %}
{% set ns.second_highest_price = ns.highest_price %}
{% set ns.second_expensive_time = ns.expensive_time %}
{% set ns.highest_price = retrieved_price %}
{% set ns.expensive_time = retrieved_time %}
{% elif retrieved_price > ns.second_highest_price %}
{% set ns.second_highest_price = retrieved_price %}
{% set ns.second_expensive_time = retrieved_time %}
{% endif %}
{% set ns.hour_counter = ns.hour_counter + 1 %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{{ dict(times=ns.times, prices=ns.prices, highest_price=ns.highest_price, second_highest_price=ns.second_highest_price, expensive_time=ns.expensive_time.isoformat(), hour_counter=ns.hour_counter, second_expensive_time=ns.second_expensive_time.isoformat()) }}
sensor:
- name: NordPool Most Expensive Times
unique_id: a27dbebc-12a3-4658-93cf-3f61a8fc6035
icon: mdi:clock
device_class: timestamp
state: "{{ calculation.expensive_time }}"
attributes:
most_expensive: "{{ calculation.highest_price }}"
most_expensive_at: "{{ calculation.expensive_time }}"
second_most_expensive: "{{ calculation.second_highest_price }}"
second_most_expensive_at: "{{ calculation.second_expensive_time }}"
VIA
(Ville)
April 24, 2024, 6:56pm
23
Thank You Petro, how should I implement this into my HA?
currently I have in my configuration file
template: !include templates.yaml
and all the sensors are built in temlates.yaml starting with:
- sensor:
##### Consuming NordPool Energyprices vat 24% ##################################
- name: "NordPool Energyprices"
unique_id: 472d8ec19-5db5-459b-8366-c0557c864c06
icon: mdi:currency-eur
unit_of_measurement: "€"
state: >
{{ states('sensor.nordpool_hinta')}}
attributes:
......
- name: "next sensor"
- name: "next sensor2"
- name: "next sensor3"
...
Now I can not include that as is to templates.yaml since it includes only sensors
and sorry - i might be too newbie for what i am trying…
VIA
(Ville)
April 28, 2024, 6:50am
24
Hey, I take it this would go to configutations files - tell me how can i still use templates.yaml with this? I have in my configuration file
template: !include templates.yaml
I cant have multiple template: lines…
Should I move all sensors from template.yaml to configurations.yaml or how should i modify this in order to run it in template.yaml file with the rest of the sensors?
petro
(Petro)
April 29, 2024, 10:48am
25
put it in your templates.yaml file and remove the first template:
line from the example I posted. Also, make sure the spacing lines up with the other contents in your template file.