Create template with attributes

I have a value template that gives me a time {{ ns.expensiveHour }}. I’d like to change so that I get two attributes in the value template.

  - platform: template
    sensors:
      dyraste_energi_2_halva_dagen:

        device_class: timestamp

        friendly_name: Dyraste elpriset eftermiddagen

        value_template: >

          {%- set numberOfSequentialHours = 1 -%}

          {%- set lastHour = 24 -%}

          {%- set firstHour = 12 -%}

            {%- set ns = namespace(counter=0, list=[], expensiveHour=today_at("00:00") + timedelta( hours = (24)), expensivePrice= 0) -%}

            {%- for i in range(firstHour + numberOfSequentialHours, lastHour + 1) -%}

              {%- set ns.counter = 0.0 -%}

              {%- for j in range(i-numberOfSequentialHours, i) -%}

                {%- set ns.counter = ns.counter + state_attr('sensor.el_spotpris', 'today')[j] -%}

              {%- endfor -%}

              {%- set ns.list = ns.list + [ns.counter] -%}

              {%- if ns.counter > ns.expensivePrice -%}

                {%- set ns.expensivePrice = ns.counter -%}

                {%- set ns.expensiveHour = today_at("00:00") + timedelta( hours = (i - numberOfSequentialHours )) -%}

              {%- endif -%}

            {%- endfor -%}

            {{ ns.expensiveHour }}

            {%- set ns.expensivePrice = ns.expensivePrice / numberOfSequentialHours -%}

So I have tried

    dyraste_test_2_halva_dagen:

        device_class: timestamp

        friendly_name: Dyraste elpriset eftermiddagen

        value_template: >

          {%- set numberOfSequentialHours = 1 -%}

          {%- set lastHour = 24 -%}

          {%- set firstHour = 12 -%}

            {%- set ns = namespace(counter=0, list=[], expensiveHour=today_at("00:00") + timedelta( hours = (24)), expensivePrice= 0) -%}

            {%- for i in range(firstHour + numberOfSequentialHours, lastHour + 1) -%}

              {%- set ns.counter = 0.0 -%}

              {%- for j in range(i-numberOfSequentialHours, i) -%}

                {%- set ns.counter = ns.counter + state_attr('sensor.el_spotpris', 'today')[j] -%}

              {%- endfor -%}

              {%- set ns.list = ns.list + [ns.counter] -%}

              {%- if ns.counter > ns.expensivePrice -%}

                {%- set ns.expensivePrice = ns.counter -%}

                {%- set ns.expensiveHour = today_at("00:00") + timedelta( hours = (i - numberOfSequentialHours )) -%}

              {%- endif -%}

            {%- endfor -%}

            {%- set ns.expensivePrice = ns.expensivePrice / numberOfSequentialHours -%}

        attribute_templates: >

          hour: "{{ ns.expensiveHour }}"

          price: "{{ ns.expensivePrice }}"

And get the following error “Invalid config for [sensor.template]: expected a dictionary for dictionary value @ data[‘sensors’][‘dyraste_test_2_halva_dagen’][‘attribute_templates’]. Got ‘hour: “{{ ns.expensiveHour }}” price: “{{ ns.expensivePrice }}”\n’. (See ?, line ?).”

Any one that can point me in a direction towards a solution?

You should not have the “>” at the end of “attribute_templates”. This indicates a line continuation, which is not what you want.

However, then I think you will find “ns” is undefined. I don’t think you can “carry over” a variable from value to attributes, or between attributes. I could be wrong though.

You’re also using old format templates. Now might be the time to update it to the new format so you don’t have to do all together when the old format is deprecated (can’t remember when, but it’s not that far away - months not years).

1 Like

What’s the difference between old and new format. Where can i find an explanation of how to translate?