#Create template sensor with attributes from output (day hour, price) as "Attribute: price"

1 messages · Page 1 of 1 (latest)

sick prairie
#

My template outputs a list like this:
[(0, 3.24982), (1, 3.53869), (2, 3.53869), (3, 3.53869), (4, 3.53869), (5, 3.53869), (6, 5.71141), (7, 5.12623), (8, 4.91464), (9, 4.71206), (10, 4.28238), (11, 3.82824), (12, 4.02046), (13, 3.91338), (14, 4.40057), (15, 4.733689999999999), (16, 4.89811), (17, 4.55356), (18, 4.20885), (19, 4.2646), (20, 4.28789), (21, 4.38104), (22, 4.36476), (23, 3.7743200000000003)]
First column (or value in duple) is hour of the day and second price.

  1. I need to convert the hour of the day to the timestamp like this "2024-12-17T00:00:00+01:00" - so the current day will be attached to the hour
  2. I need to create template sensor attributes from template output like this on the picture

Here is how I get my output, but I have no idea with the template creation:

{% set data = state_attr('binary_sensor.nizky_tarif','response_json').data[0] %}
{% set ns = namespace(tarif_times=[]) %}

{% for i in range(1, 11) %}

  {% if data['CAS_ZAP_'+ i |string] is not none %}
    {% set start_hour = int(data['CAS_ZAP_' + i |string].split(':')[0]) %}
    {% set end_hour = int(data['CAS_VYP_' + i |string].split(':')[0]) %}
    {% for j in range(start_hour, end_hour + 1) %}
      {% if j == start_hour %}
        {% set ns.tarif_times = ns.tarif_times + [(j, states('sensor.distribuce_nt'))] %}
      {% else %}
        {% set ns.tarif_times = ns.tarif_times + [(j, states('sensor.distribuce_vt'))] %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endfor %}

{% set data = namespace(prices=[]) %}
{# Gather prices for each hour #}
{% for i in range(0, 24) %}
  {% set time_dt = now() %}
  {% set hour_dt = time_dt.replace(hour=i, minute=0, second=0, microsecond=0).isoformat() %}
  {% set price = state_attr('sensor.current_spot_electricity_price', hour_dt) %}
  {% if price is not none %}
    {% set data.prices = data.prices + [(i, price + ns.tarif_times[i][1] | float )] %}
  {% endif %}
{% endfor %}
{{ data.prices}}
#

Why I do this - The output from the template shows sum of spot electricity price + tariff price. I want to show the daily graph in Apex charts so my mother knows where is the cheapest/most expensive eletricity

onyx rock
#

In a template sensor you can't dynamically create attributes; but you can create a single attribute that contains a list of dictionaries

#

Changing this line should give you a tuple with the datetime instead of the hour

    {% set data.prices = data.prices + [(hour_dt, price + ns.tarif_times[i][1] | float )] %}