Problem in building a JSON list of data

I’m trying my best. That’s why I try to explain what I mean. No reason to get angry on me. For me, it makes sense. Just not including values from tomorrow is too short-sighted. I think it’s about viewing at a whole day from 0:00 until 23:59.
In general, I want to run - let’s say a heating rod - for the cheapest 2h a day. Or disable the heating pump during the 2 most expensive hours a day.

Right, I get that. But you keep focusing on the data shifting when that won’t impact the result because template searches for the minimum. If the minimum moves position in the list, it’ll still find that minimum.

Here’s it all fixed

Sensor that chops data at midnight.

sensor:
  - platform: rest
    resource: https://api.awattar.de/v1/marketdata
    name: awattar
    json_attributes:
      - data
    scan_interval: 60
    value_template: >
      {% set start = (today_at().timestamp() * 1000) | int %}
      {% set end = ((today_at() + timedelta(days=1)).timestamp() * 1000) | int %}
      {% set values_today = value_json.data | selectattr('start_timestamp', '>=', start) | selectattr('end_timestamp', '<=', end) | list %}
      {% set min_today = values_today | sort(attribute='marketprice') | first %}
      {% set i = values_today.index(min_today) %}
      {% set hours = 4 %}
      {% set search = hours - 1 %}
      {% set s = i - search if i - search >= 0 else 0 %}
      {% set s = value_json.data.index(values_today[s]) %}
      {% set e = i + search if i + search < values_today | length else values_today | length - 1 %}
      {% set e = value_json.data.index(values_today[e]) %}
      {{ s }},{{ e }},{{ value_json.data[s].start_timestamp | multiply(0.001) | timestamp_local }}

Updates whenever the data changes.

template:
- trigger:
  - platform: state
    entity_id: sensor.awattar
    variables:
      the_state: >
        {% if trigger.to_state and trigger.to_state.state not in ['unknown', 'unavailable'] %}
          {% set s, e = trigger.to_state.state.split(',') | map('int', none) | reject('none') %}
          {{ {'s': s, 'e': e, 'data': trigger.to_state.attributes.data } }}
        {% else %}
          {'s': 0, 'e': 0, 'data': []}
        {% endif %}
      time_span: >
        {% set s, e, data = the_state.s, the_state.e, the_state.data %}
        {% set hours = 4 %}
        {% set ns = namespace(windows=[], out=[]) %}
        {% for span in range(e - s) %}
          {% set roll = (e - s) - span %}
          {% set sub = namespace(window=[]) %}
          {% for i in range(roll) %}
            {% set total = data[s+i:s+span+i+1] | selectattr('marketprice', 'defined') | map(attribute='marketprice') | sum %}
            {% set sub.window = sub.window + [ {'timestamp':(data[s+i].start_timestamp//1000) | timestamp_local, 'total':total} ] %}
          {% endfor %}
          {% set item = sub.window | sort(attribute='total') | first %}
          {% set ns.windows = ns.windows + [ (span+1, item) ] %}
        {% endfor %}
        {% set items = dict.from_keys(ns.windows) %}
        {% for i in range(1, hours+1) %}
          {% set ns.out = ns.out + [ (i, items[i]) if i in items else (i,{'timestamp': None, 'total': None}) ] %}
        {% endfor %}
        {{ dict.from_keys(ns.out) }}
  sensor:
  - name: Awatter One Hour Min
    state: "{{ time_span.1.total * 100/1000 * 1.20 }}"
  - name: Awatter One Hour Timestamp
    state: "{{ time_span.1.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Two Hour Min
    state: "{{ time_span.2.total * 100/1000 * 1.20 }}"
  - name: Awatter Two Hour Timestamp
    state: "{{ time_span.2.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Three Hour Min
    state: "{{ time_span.3.total * 100/1000 * 1.20 }}"
  - name: Awatter Three Hour Timestamp
    state: "{{ time_span.3.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Three Hour Min
    state: "{{ time_span.4.total * 100/1000 * 1.20 }}"
  - name: Awatter Three Hour Timestamp
    state: "{{ time_span.4.timestamp | as_datetime }}"
    device_class: timestamp

Updates once at midnight for the day

template:
- trigger:
  - platform: time
    at: "00:00:01"
    variables: &awatter_variables
      the_state: >
        {% set to_state = expand('sensor.awattar') | first %}
        {% if to_state and to_state.state not in ['unknown', 'unavailable'] %}
          {% set s, e = to_state.state.split(',') | map('int', none) | reject('none') %}
          {{ {'s': s, 'e': e, 'data': to_state.attributes.data } }}
        {% else %}
          {'s': 0, 'e': 0, 'data': []}
        {% endif %}
      time_span: >
        {% set s, e, data = the_state.s, the_state.e, the_state.data %}
        {% set hours = 4 %}
        {% set ns = namespace(windows=[], out=[]) %}
        {% for span in range(e - s) %}
          {% set roll = (e - s) - span %}
          {% set sub = namespace(window=[]) %}
          {% for i in range(roll) %}
            {% set total = data[s+i:s+span+i+1] | selectattr('marketprice', 'defined') | map(attribute='marketprice') | sum %}
            {% set sub.window = sub.window + [ {'timestamp':(data[s+i].start_timestamp//1000) | timestamp_local, 'total':total} ] %}
          {% endfor %}
          {% set item = sub.window | sort(attribute='total') | first %}
          {% set ns.windows = ns.windows + [ (span+1, item) ] %}
        {% endfor %}
        {% set items = dict.from_keys(ns.windows) %}
        {% for i in range(1, hours+1) %}
          {% set ns.out = ns.out + [ (i, items[i]) if i in items else (i,{'timestamp': None, 'total': None}) ] %}
        {% endfor %}
        {{ dict.from_keys(ns.out) }}
  - platform: homeassistant
    event: start
    variables: *awatter_variables
  sensor:
  - name: Awatter One Hour Min
    state: "{{ time_span.1.total * 100/1000 * 1.20 }}"
  - name: Awatter One Hour Timestamp
    state: "{{ time_span.1.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Two Hour Min
    state: "{{ time_span.2.total * 100/1000 * 1.20 }}"
  - name: Awatter Two Hour Timestamp
    state: "{{ time_span.2.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Three Hour Min
    state: "{{ time_span.3.total * 100/1000 * 1.20 }}"
  - name: Awatter Three Hour Timestamp
    state: "{{ time_span.3.timestamp | as_datetime }}"
    device_class: timestamp
  - name: Awatter Three Hour Min
    state: "{{ time_span.4.total * 100/1000 * 1.20 }}"
  - name: Awatter Three Hour Timestamp
    state: "{{ time_span.4.timestamp | as_datetime }}"
    device_class: timestamp

Keep in mind, the updates once at midnight will update on restart as well. It will not update on template reload. So you have to wait until midnight to see the state or restart the system.

Thanks for the help. In the meantime I don’t have any glue anymore on what the algorithm does ;-). Runs now and delivers data.
One slight issue: When updated during the day (restart) the api being called without startdate only returns values from now on. That means it show the cheapest hour for today in 13 hours, but it was 6 hours ago (HOURLY Preis Chart - mehr Wind und Sonne, mehr sparen!).
Is it possible to pass today, 00:00 as epochseconds to the script? Syntax is

https://api.awattar.at/v1/marketdata?start=<startdatetime in epochseconds>

I think I have it.
URL call needs to be (instead of resource):

resource_template: 'https://api.awattar.at/v1/marketdata\start={{today_at().timestamp() * 1000 }}'

yep

To be honest, I switched back to the original solution. That is maybe less efficient, but for me better to understand and to extend.
I extended that now with:

  • API is called with the current day at 0:00 to always get the values for the current day as a full.
  • A new sensor is added for the average price of the day
  • A new sensor is added for the current price (This is why it still runs every 60 sec)
  • New sensors for the expensive hours a day
  • Unfortuntately not solved: Median price of the day
    Attention: I am using the Austrian API (extension .at) and Austrian VAT (20% insteady of 19% in DE)

Here is my current sensor.yaml

#aWattar Strompreise
  - platform: rest
    resource_template: 'https://api.awattar.at/v1/marketdata?start={{today_at().timestamp() * 1000 }}'
    name: awattar
    json_attributes:
      - data
    scan_interval: 60
    value_template: "{{ (value_json.data[0]) }}"
  - platform: template
    sensors:
      awattar_count:
        friendly_name: "Count of available prices"
        value_template: >-
          {% set count = state_attr('sensor.awattar', 'data') | list | length %}
          {{ count }}
      awattar_average:
        friendly_name: "Average of available prices"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {{ awattar_list | average | float * 100/1000 * 1.20 | round(2) }}
      awattar_current:
        friendly_name: "aWattar current price"
        value_template: >-
          {% set h = now().hour | int %}
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {{ awattar_list[h] | float * 100/1000 * 1.20 | round(2) }}
      awattar_values:
        friendly_name: "All aWattar values"
        value_template: >-
          {%- set awattar_all_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values_all = namespace(all=[]) %}
          {% for i in range(awattar_all_list | length) %}
           {%- set v = (awattar_all_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values_all.all = values_all.all + [ v ] %}
          {%- endfor %}
          {{ values_all.all }}
      awattar_one_hour_min_index:
        friendly_name: "aWattar one hour minimum value index in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.min = values.min + [ v ] %}
          {%- endfor %}
          {{ values.min.index(values.min | min) }}
      awattar_one_hour_min_value:
        friendly_name: "aWattar one hour minimum value in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.min = values.min + [ v ] %}
          {%- endfor %}
          {{ values.min | min }}
      awattar_one_hour_min_timestamp:
        friendly_name: "aWattar one hour minimum value timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_one_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_two_hour_min_index:
        friendly_name: "aWattar two hour minimum start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 1 < awattar_list | length %}
              {%- set v = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set next = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.min = values.min + [ v + next ] %}
            {% endif %}
          {%- endfor %}
          {{ values.min.index(values.min | min)  }}
      awattar_two_hour_min_timestamp:
        friendly_name: "aWattar two hour minimum value start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_two_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_four_hour_min_index:
        friendly_name: "aWattar four hour minimum start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 3 < awattar_list | length %}
              {%- set v1 = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v2 = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v3 = (awattar_list[i+2] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v4 = (awattar_list[i+3] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.min = values.min + [ v1 + v2 + v3 + v4 ] %}
            {% endif %}
          {%- endfor %}
          {{ values.min.index(values.min | min)  }}
      awattar_four_hour_min_timestamp:
        friendly_name: "aWattar four hour minimum value start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_four_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_one_hour_max_index:
        friendly_name: "aWattar one hour max value index in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.max = values.max + [ v ] %}
          {%- endfor %}
          {{ values.max.index(values.max | max) }}
      awattar_one_hour_max_value:
        friendly_name: "aWattar one hour max value in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.max = values.max + [ v ] %}
          {%- endfor %}
          {{ values.max | max }}
      awattar_one_hour_max_timestamp:
        friendly_name: "aWattar one hour max value timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_one_hour_max_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_two_hour_max_index:
        friendly_name: "aWattar two hour max start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 1 < awattar_list | length %}
              {%- set v = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set next = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.max = values.max + [ v + next ] %}
            {% endif %}
          {%- endfor %}
          {{ values.max.index(values.max | max)  }}
      awattar_two_hour_max_timestamp:
        friendly_name: "aWattar two hour max value start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_two_hour_max_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
1 Like

Just keep in mind the original solution doesn’t find the lowest. It finds the lowest point and then chooses the 4 after. What I do is find the lowest 4 in a row, 3 in a row, 2 in a row. EDIT: Well maybe it does now that I look at it.

EDIT2: I forgot to add the conversions, so I added it to that post

EDIT3: FYI your ‘count’, ‘average’, and ‘current’ sensors and all that remain unchanged if that’s what you were unsure about. The updates only affect the selection and rolling.

Also, fyi your rounds(2) are doing nothing. You need to round the whole result, not just 1.20

anyways, here’s median

{% set values = state_attr('sensor.awattar', 'data')| map(attribute='marketprice') | list | sort %}
{% set total = values | length %}
{% set ns = namespace(half= total // 2, divisor = 1) %}
{% if not (total - ns.half * 2) % 2 %}
  {% set ns.half = ns.half - 1 %}
  {% set ns.divisor = 2 %}
{% endif %}
{{ (values[ns.half:-ns.half] | sum / ns.divisor * 0.1 * 1.20) | round(2) }}

Hey there, I need to chime in quickly - love your templates and am now trying to get only the price for the current hour, so HA can use it for the energy dashboard.

Well, I fail miserably at it … could you help me?!

Includes now the additions (Median, round correction) by Petro (thanks, one more time) and current price (by me) as well as the end timestamps that may be needed for automations to be able to trigger the end times for turning devices off.

#aWattar Strompreise
  - platform: rest
    resource_template: 'https://api.awattar.at/v1/marketdata?start={{today_at().timestamp() * 1000 }}'
    name: awattar
    json_attributes:
      - data
    scan_interval: 60
    value_template: "{{ (value_json.data[0]) }}"
  - platform: template
    sensors:
      awattar_count:
        friendly_name: "Count of available prices"
        value_template: >-
          {% set count = state_attr('sensor.awattar', 'data') | list | length %}
          {{ count }}
      awattar_average:
        friendly_name: "Average of available prices"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {{ (awattar_list | average | float * 100/1000 * 1.20) | round(2) }}
      awattar_current:
        friendly_name: "Current price"
        value_template: >-
          {% set h = now().hour | int %}
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {{ (awattar_list[h] | float * 100/1000 * 1.20) | round(2) }}
      awattar_median:
        friendly_name: "Median of available prices"
        value_template: >-
          {% set values = state_attr('sensor.awattar', 'data')| map(attribute='marketprice') | list | sort %}
          {% set total = values | length %}
          {% set ns = namespace(half= total // 2, divisor = 1) %}
          {% if not (total - ns.half * 2) % 2 %}
            {% set ns.half = ns.half - 1 %}
            {% set ns.divisor = 2 %}
          {% endif %}
          {{ (values[ns.half:-ns.half] | sum / ns.divisor * 0.1 * 1.20) | round(2) }}
      awattar_values:
        friendly_name: "All aWattar values"
        value_template: >-
          {%- set awattar_all_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values_all = namespace(all=[]) %}
          {% for i in range(awattar_all_list | length) %}
           {%- set v = (awattar_all_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values_all.all = values_all.all + [ v ] %}
          {%- endfor %}
          {{ values_all.all }}
      awattar_one_hour_min_index:
        friendly_name: "One hour minimum index in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.min = values.min + [ v ] %}
          {%- endfor %}
          {{ values.min.index(values.min | min) }}
      awattar_one_hour_min_value:
        friendly_name: "One hour minimum value in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.min = values.min + [ v ] %}
          {%- endfor %}
          {{ values.min | min }}
      awattar_one_hour_min_starttimestamp:
        friendly_name: "One hour minimum start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_one_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_one_hour_min_endtimestamp:
        friendly_name: "One hour min end timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {{ (as_timestamp(states('sensor.awattar_one_hour_min_starttimestamp')) + 1 * 3600) | timestamp_local }}
      awattar_two_hour_min_index:
        friendly_name: "Two hour minimum start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 1 < awattar_list | length %}
              {%- set v = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set next = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.min = values.min + [ v + next ] %}
            {% endif %}
          {%- endfor %}
          {{ values.min.index(values.min | min)  }}
      awattar_two_hour_min_starttimestamp:
        friendly_name: "Two hour minimum start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_two_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_two_hour_min_endtimestamp:
        friendly_name: "Two hour min end timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {{ (as_timestamp(states('sensor.awattar_two_hour_min_starttimestamp')) + 2 * 3600) | timestamp_local }}
      awattar_four_hour_min_index:
        friendly_name: "Four hour minimum start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(min=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 3 < awattar_list | length %}
              {%- set v1 = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v2 = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v3 = (awattar_list[i+2] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set v4 = (awattar_list[i+3] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.min = values.min + [ v1 + v2 + v3 + v4 ] %}
            {% endif %}
          {%- endfor %}
          {{ values.min.index(values.min | min)  }}
      awattar_four_hour_min_starttimestamp:
        friendly_name: "Four hour minimum start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_four_hour_min_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_four_hour_min_endtimestamp:
        friendly_name: "Four hour min end timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {{ (as_timestamp(states('sensor.awattar_four_hour_min_starttimestamp')) + 4 * 3600) | timestamp_local }}
      awattar_one_hour_max_index:
        friendly_name: "One hour max index in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.max = values.max + [ v ] %}
          {%- endfor %}
          {{ values.max.index(values.max | max) }}
      awattar_one_hour_max_value:
        friendly_name: "One hour max value in available dataset"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
           {%- set v = (awattar_list[i] | float * 100/1000 * 1.20) | round(2) %}
            {%- set values.max = values.max + [ v ] %}
          {%- endfor %}
          {{ values.max | max }}
      awattar_one_hour_max_starttimestamp:
        friendly_name: "One hour max start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_one_hour_max_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_one_hour_max_endtimestamp:
        friendly_name: "One hour max end timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {{ (as_timestamp(states('sensor.awattar_one_hour_max_starttimestamp')) + 1 * 3600) | timestamp_local }}
      awattar_two_hour_max_index:
        friendly_name: "Two hour max start index"
        value_template: >-
          {%- set awattar_list = state_attr('sensor.awattar', 'data') | map(attribute='marketprice') | list %}
          {%- set values = namespace(max=[]) %}
          {% for i in range(awattar_list | length) %}
            {% if i + 1 < awattar_list | length %}
              {%- set v = (awattar_list[i] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set next = (awattar_list[i+1] | float * 100/1000 * 1.20 ) | round(2) %}
              {%- set values.max = values.max + [ v + next ] %}
            {% endif %}
          {%- endfor %}
          {{ values.max.index(values.max | max)  }}
      awattar_two_hour_max_starttimestamp:
        friendly_name: "Two hour max start timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {% set index = states('sensor.awattar_two_hour_max_index') | int %}
          {% set table = state_attr('sensor.awattar', 'data') %}
          {{ ( table[index].start_timestamp | int / 1000 ) | timestamp_local}}
      awattar_two_hour_max_endtimestamp:
        friendly_name: "Two hour max end timestamp in available dataset"
        device_class: timestamp
        value_template: >-
          {{ (as_timestamp(states('sensor.awattar_two_hour_max_starttimestamp')) + 2 * 3600) | timestamp_local }}
1 Like

Great!

two questions:

I kind of would like to use:

https://api.awattar.at/v1/marketdata?start={{(today_at().timestamp() * 1000 ) | round(0, default=“not available”)}}&end={{(((today_at().timestamp()) + 172800) * 1000) | round(0, default=“not available”) }}

so it gives me all values (as soon as available) for the next day also.
This seems to work fine, except for the awattar_values sensor, which shows unknown

secondly, I assume when you multiply by 1.20, this is because of taxes, right?! but doesn’t aWattar also add 3% for their profit?! at least that’s what it says here HOURLY - mehr Wind und Sonne, mehr sparen! when you scroll down to “Tarifübersicht” : “Energieverbrauchspreis EPEX Spot AT ® * 1.03 (3%) Cent/kWh netto”
Or is this already included in the values from the API call?! :thinking:

Regarding 3%: I don’t think it’s included, but you can anyways only narrow the real price. There is also a basic charge per month and then network costs from your infrastructure provider.
Maybe it’s even better to keep the net price without VAT.