personally, i’d change your main sensor to out put the index of the 2 minimum. Then just simply grab the values in another template sensor.
- platform: rest
resource: https://api.awattar.de/v1/marketdata
name: awattar
value_template: >
{%- set awattar_list = value_json.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.19 + 19.22) | round(2) %}
{%- set next = (awattar_list[i+1] | float * 100/1000 * 1.19 + 19.22) | round(2) %}
{%- set values.min = values.min + [ v + next ] %}
{% endif %}
{%- endfor %}
{{ values.min.index(values.min | min) }}
json_attributes:
- data
scan_interval: 60
- platform: template
sensors:
lowest_awattar:
friendly_name: "Lowest awattar"
value_template: >-
{% set index = states('sensor.awattar_table') | int %}
{% set table = state_attr('sensor.awattar', 'data') %}
{{ (table[index].marketprice | float * 100/1000 * 1.19 + 19.22) | round(2) }}
lowest_awattar_timestamp:
friendly_name: "Lowest awattar timestamp"
device_class: timestamp
value_template: >-
{% set index = states('sensor.awattar_table') | int %}
{% set table = state_attr('sensor.awattar', 'data') %}
{{ table[index].start_timestamp | int / 1000 }}
lowest_awattar_2:
friendly_name: "Second lowest awattar"
value_template: >-
{% set index = states('sensor.awattar_table') | int %}
{% set table = state_attr('sensor.awattar', 'data') %}
{{ (table[index+1].marketprice | float * 100/1000 * 1.19 + 19.22) | round(2) }}
lowest_awattar_timestamp_2:
friendly_name: "Second lowest awattar timestamp"
device_class: timestamp
value_template: >-
{% set index = states('sensor.awattar_table') | int %}
{% set table = state_attr('sensor.awattar', 'data') %}
{{ table[index+1].start_timestamp | int / 1000 }}
Breakdown:
Takes data (which is a list of dictionaries) and filters it to a list marketprice values.
{%- set awattar_list = value_json.data | map(attribute='marketprice') | list %}
Creates an empty list that we can use to store values, it’s named values.min
{%- set values = namespace(min=[]) %}
Loop through your data, using i, where i is a indexer (position in the list)
{% for i in range(awattar_list | length) %}
check if we have an position in the list after the current position
{% if i + 1 < awattar_list | length %}
get our current math value
{%- set v = (awattar_list[i] | float * 100/1000 * 1.19 + 19.22) | round(2) %}
get our next math value
{%- set next = (awattar_list[i+1] | float * 100/1000 * 1.19 + 19.22) | round(2) %}
add the 2 values together and add it to the list
{%- set values.min = values.min + [ v + next ] %}
end the if statement and the for loop
{% endif %}
{%- endfor %}
Get the lowest par, then find it’s position in the list, output that position
{{ values.min.index(values.min | min) }}
The template sensors just use that position to output the data:
calculates the first item in the lowest pair (using the index)
{{ (table[index].marketprice | float * 100/1000 * 1.19 + 19.22) | round(2) }}
calculates the first items timestamp in the lowest pair (using the index)
{{ table[index].start_timestamp | int / 1000 }}
calculates the second item in the lowest pair (using the index)
{{ (table[index+1].marketprice | float * 100/1000 * 1.19 + 19.22) | round(2) }}
calculates the second items timestamp in the lowest pair (using the index)
{{ table[index+1].start_timestamp | int / 1000 }}