HA says configuration error

Stop using YT videos and don’t pay for code… This is the example code.

- sensor:
    - name: "Nordpool Energy Prices"
      unique_id: "nordpool_energyprices"
      icon: mdi:currency-eur
      unit_of_measurement: "€"
      state: >
        {{ states(' sensor.nordpool ')}}
      attributes:
        times: >
          {% set ns = namespace(times=[]) -%}
          {%- set today = state_attr(' sensor.nordpool ','raw_today') -%}
          {%- for hours in today -%}
            {%- set ns.times = ns.times + [as_local((hours.start)).strftime("%Y-%m-%d %H:%M:%S")] -%}
          {%- endfor -%}
          {%- set tomorrow = state_attr(' sensor.nordpool ','raw_tomorrow') -%}
          {%- for hours in tomorrow -%}
            {%- set ns.times = ns.times + [as_local((hours.start)).strftime("%Y-%m-%d %H:%M:%S")] -%}
          {%- endfor -%}
          {{ ns.times }}
        prices: >
          {% set ns = namespace(prices=[]) -%}
          {%- set today = state_attr(' sensor.nordpool ','raw_today') -%}
          {%- for hours in today -%}
            {%- set ns.prices = ns.prices + [hours.value + states(' input_number.nordpool_additional_costs ') | float] -%}
          {%- endfor -%}
          {%- set tomorrow = state_attr(' sensor.nordpool ','raw_tomorrow') -%}
          {%- for hours in tomorrow -%}
            {%- set ns.prices = ns.prices + [hours.value + states(' input_number.nordpool_additional_costs ') | float] -%}
          {%- endfor -%}
          {{ ns.prices }}

When I started with home assistant it semd logical to start with some tutorial videos… Now i can see that these “ready” solutions are not what I need and this i have been modifying them for my purpose and the original code is not exactly what i need…

So Yes now I try to make my own code…

And from this to a question…

If I have calculated some sensor value in STATE
can I move some values to sensor attributes without having to calculate everything again under the attribute? I can think of ways but they all depend on some helpers or so…

Don’t take my comments the wrong way…you have found the right place to ask questions.

1 Like

it’s been about 20 years since last time I have done any coding… I am abit rusty…

1 Like

There are quite a few options and others will chime in…

And yes, you can template attributes in the config or via the helper.

whats the easiest way…

I have some timestaps and values calculated under STATE: and would want to move these under ATTRIBUTES:

should I set some global variable?? how?

as said - i am rusty…

Can you post the state code?

Hers the sensor code from state onwards…
state: >
{% 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 %}
{{ ns.expensive_time | timestamp_custom(‘%H:%M’, true) }}
attributes:
most expencive
{{ ns.highest_price }}
most expencive at
{{ ns.expensive_time | timestamp_custom(‘%H:%M’, true) }}
second most expencive
{{ ns.second_highest_price }}
second most expencive at
{{ ns.second_expensive_time | timestamp_custom(‘%H:%M’, true) }}

In the end I tried to set some attributes but this results NONE in the sensor attrebutes

Check out #11 here for posting code

just to clarify I am trying to find 2 highest price hours from nordpool sensor - this is for selling power from my batteries - I do have same for 2 lowest hours - this is for controlling hi consumption devices like water heaters etc…

deffinietly understand this… I just do not have any friends playing around HA…

openAI suggests the use of platform: template

sensor:

  • platform: template
    sensors:
    nordpool_most_expensive_times:
    friendly_name: “NordPool Most Expensive Times”
    unique_id: a27dbebc-12a3-4658-93cf-3f61a8fc6035
    icon: mdi:clock
    value_template: >
    “state calculation here”
    attribute_templates:
    most_expensive: “{{ ns.highest_price }}”
    most_expensive_at: “{{ ns.expensive_time | timestamp_custom(‘%H:%M’, true) }}”
    second_most_expensive: “{{ ns.second_highest_price }}”
    second_most_expensive_at: “{{ ns.second_expensive_time | timestamp_custom(‘%H:%M’, true) }}”

I tried this also but HA didn’t like it …

To post code on the forum, use the back tick ` in block form

your code

image

  - name: "NordPool Most Expensive Times"
    unique_id: a27dbebc-12a3-4658-93cf-3f61a8fc6035
    icon: mdi:clock
    state: >
      {% 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 %}
      {{ ns.expensive_time | timestamp_custom('%H:%M', true) }}
    attributes:
        most expencive        
          {{ ns.highest_price }}
        most expencive at       
          {{ ns.expensive_time | timestamp_custom('%H:%M', true) }}
        second most expencive 
          {{ ns.second_highest_price }}
        second most expencive at
          {{ ns.second_expensive_time | timestamp_custom('%H:%M', true) }}
sensor:
  - platform: template
    sensors:
      nordpool_most_expensive_times:
        friendly_name: "NordPool Most Expensive Times"
        unique_id: a27dbebc-12a3-4658-93cf-3f61a8fc6035
        icon: mdi:clock
        value_template: >
          {% 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 %}
          {{ ns.expensive_time | timestamp_custom('%H:%M', true) }}
    attribute_templates:
      most_expensive: "{{ ns.highest_price }}"
      most_expensive_at: "{{ ns.expensive_time | timestamp_custom('%H:%M', true) }}"
      second_most_expensive: "{{ ns.second_highest_price }}"
      second_most_expensive_at: "{{ ns.second_expensive_time | timestamp_custom('%H:%M', true) }}"

and this was Open AI suggestion…HA didn’t perticularily like it…

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 :wink:

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 }}"

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…

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?

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.