HA says configuration error

in my templates.yaml I have following code: (line 10 is times: >)

attributes:
  times: >
    {% set ns = namespace(times=[]) -%}
    {%- set today = state_attr('sensor.nordpool_kwh_fi_eur_3_10_0','raw_today') -%}
    {%- for hours in today -%}
      {%- set ns.times = ns.times + [as_local((hours.start)).strftime("%Y-%m-%d %H:%M:%S")] -%} 
    {%- endfor -%}

and HA says:

  • Invalid config for ‘template’ at templates.yaml, line 12: expected a dictionary for dictionary value ‘sensor->0->attributes’, got None Invalid config for ‘template’ at templates.yaml, line 13: ‘times’ is an invalid option for ‘template’, check: sensor->0->times
  • Invalid config for ‘template’ at templates.yaml, line 13: expected a dictionary for dictionary value ‘sensor->0->attributes’, got None Invalid config for ‘template’ at templates.yaml, line 14: ‘times’ is an invalid option for ‘template’, check: sensor->0->times
  • Invalid config for ‘template’ at templates.yaml, line 10: ‘times’ is an invalid option for ‘template’, check: sensor->0->times Invalid config for ‘template’ at templates.yaml, line 9: expected a dictionary for dictionary value ‘sensor->0->attributes’, got None

If I run this in developer tool template - the code works and gives me the list of times like this:
times: >
[‘2024-04-22 00:00:00’, ‘2024-04-22 01:00:00’, ‘2024-04-22 02:00:00’, ‘2024-04-22 03:00:00’, ‘2024-04-22 04:00:00’, ‘2024-04-22 05:00:00’, ‘2024-04-22 06:00:00’, ‘2024-04-22 07:00:00’, ‘2024-04-22 08:00:00’, ‘2024-04-22 09:00:00’, ‘2024-04-22 10:00:00’, ‘2024-04-22 11:00:00’, ‘2024-04-22 12:00:00’, ‘2024-04-22 13:00:00’, ‘2024-04-22 14:00:00’, ‘2024-04-22 15:00:00’, ‘2024-04-22 16:00:00’, ‘2024-04-22 17:00:00’, ‘2024-04-22 18:00:00’, ‘2024-04-22 19:00:00’, ‘2024-04-22 20:00:00’, ‘2024-04-22 21:00:00’, ‘2024-04-22 22:00:00’, ‘2024-04-22 23:00:00’]
to me this is weird…

Your yaml format is incorrect. Post everything, what you’ve posted so far doesn’t show the whole picture.

Secondly, your template isn’t outputting anything.

sorry for not posting the whole sensor - as it is not my ow code i felt it was more prudent to paste a part… However i used openAI and after 5 iterations with openAI i managed to get a code that worked both with development tool/template and did not cause any errors in actual yaml parsing.

So Thank You for now - I am ok with the code…

Except, nobody using the faulty code above will know the answer.

tell me what’s the best way? should I paste the code which I am only allowed to use?
It’s SmatHome junkies code and he is selling the code… So if I paste it here it’s not prudent.

I would like to show how i made it work but there is a conflict…

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…