I would like to get an attribute from a sensor which has date-time and under that power prices per hour

Hi,

I am looking for a solution to graph and use the data that I get from a power pricing provider. I am looking for the lowest price that day and I would like to graph the hourly prices for that day.

I can’t get my head around how to get the specific prices per hour in individual sensors.

the sensor.stroomprijs has the following values:

I am looking to get only the price for the attribute: prijsBE per hour, to see which value is the lowest that day and to graph the prijsBE attribute per hour for that day.

So the logic would be: look for the date today per hour, look for the value of prijsBE under that date-time, store those in a variable or sensor per hour and compare all the hourly values to find the lowest, put the lowest in a lowest sensor.

Use the rest of the hourly values to create a graph for that day.

Any sample ninja I could try is appreciated!
thx.

data:
  - datum: "2024-01-07 00:00:00"
    prijs: "0.084080"
    prijsAA: "0.254587"
    prijsAIP: "0.263637"
    prijsANWB: "0.254567"
    prijsBE: "0.254377"
    prijsEE: "0.264247"
    prijsEN: "0.255707"
    prijsEVO: "0.254567"
    prijsEZ: "0.254567"
    prijsFR: "0.258787"
    prijsGSL: "0.254567"
    prijsMDE: "0.254567"
    prijsNE: "0.255478"
    prijsTI: "0.255167"
    prijsVDB: "0.255264"
    prijsVON: "0.253352"
    prijsWE: "0.258787"
    prijsZG: "0.254567"
    prijsZP: "0.253387"
  - datum: "2024-01-07 01:00:00"
    prijs: "0.079820"
    prijsAA: "0.249433"
    prijsAIP: "0.258482"
    prijsANWB: "0.249413"
    prijsBE: "0.249222"
    prijsEE: "0.259092"
    prijsEN: "0.250552"
    prijsEVO: "0.249413"
    prijsEZ: "0.249413"
    prijsFR: "0.253632"
    prijsGSL: "0.249413"
    prijsMDE: "0.249413"
    prijsNE: "0.250323"
    prijsTI: "0.250012"
    prijsVDB: "0.250109"
    prijsVON: "0.248197"
    prijsWE: "0.253633"
    prijsZG: "0.249413"
    prijsZP: "0.248232"
  - datum: "2024-01-07 02:00:00"
    prijs: "0.076760"
    prijsAA: "0.245730"
    prijsAIP: "0.254780"
    prijsANWB: "0.245710"
    prijsBE: "0.245520"
    prijsEE: "0.255390"
    prijsEN: "0.246850"
    prijsEVO: "0.245710"
    prijsEZ: "0.245710"
    prijsFR: "0.249930"
    prijsGSL: "0.245710"
    prijsMDE: "0.245710"
    prijsNE: "0.246620"
    prijsTI: "0.246310"
    prijsVDB: "0.246407"
    prijsVON: "0.244495"
    prijsWE: "0.249930"
    prijsZG: "0.245710"
    prijsZP: "0.244530"
  - datum: "2024-01-07 03:00:00"
    prijs: "0.073460"
    prijsAA: "0.241737"
    prijsAIP: "0.250787"
    prijsANWB: "0.241717"
    prijsBE: "0.241527"
    prijsEE: "0.251397"
    prijsEN: "0.242857"
    prijsEVO: "0.241717"
    prijsEZ: "0.241717"
    prijsFR: "0.245937"
    prijsGSL: "0.241717"
    prijsMDE: "0.241717"
    prijsNE: "0.242627"
    prijsTI: "0.242317"
    prijsVDB: "0.242414"
    prijsVON: "0.240502"
    prijsWE: "0.245937"
    prijsZG: "0.241717"
    prijsZP: "0.240537"
  - datum: "2024-01-07 04:00:00"
    prijs: "0.071860"
    prijsAA: "0.239801"
    prijsAIP: "0.248851"
    prijsANWB: "0.239781"
    prijsBE: "0.239591"
    prijsEE: "0.249461"
    prijsEN: "0.240921"
    prijsEVO: "0.239781"
    prijsEZ: "0.239781"
    prijsFR: "0.244001"
    prijsGSL: "0.239781"
    prijsMDE: "0.239781"
    prijsNE: "0.240691"
    prijsTI: "0.240381"
    prijsVDB: "0.240478"
    prijsVON: "0.238566"
    prijsWE: "0.244001"
    prijsZG: "0.239781"
    prijsZP: "0.238601"
  - datum: "2024-01-07 05:00:00"
    prijs: "0.072080"
    prijsAA: "0.240067"
    prijsAIP: "0.249117"
    prijsANWB: "0.240047"
    prijsBE: "0.239857"
    prijsEE: "0.249727"
    prijsEN: "0.241187"
    prijsEVO: "0.240047"
    prijsEZ: "0.240047"
    prijsFR: "0.244267"
    prijsGSL: "0.240047"
    prijsMDE: "0.240047"
    prijsNE: "0.240958"
    prijsTI: "0.240647"
    prijsVDB: "0.240744"
    prijsVON: "0.238832"
    prijsWE: "0.244267"
    prijsZG: "0.240047"
    prijsZP: "0.238867"
  - datum: "2024-01-07 06:00:00"
    prijs: "0.074900"
    prijsAA: "0.243479"
    prijsAIP: "0.252529"
    prijsANWB: "0.243460"
    prijsBE: "0.243269"
    prijsEE: "0.253139"
    prijsEN: "0.244599"
    prijsEVO: "0.243460"
    prijsEZ: "0.243460"
    prijsFR: "0.247679"
    prijsGSL: "0.243460"
    prijsMDE: "0.243460"
    prijsNE: "0.244370"
    prijsTI: "0.244059"
    prijsVDB: "0.244156"
    prijsVON: "0.242244"
    prijsWE: "0.247679"
    prijsZG: "0.243460"
    prijsZP: "0.242279"
  - datum: "2024-01-07 07:00:00"
    prijs: "0.077690"
    prijsAA: "0.246855"
    prijsAIP: "0.255905"
    prijsANWB: "0.246835"
    prijsBE: "0.246645"
    prijsEE: "0.256515"
    prijsEN: "0.247975"
    prijsEVO: "0.246835"
    prijsEZ: "0.246835"
    prijsFR: "0.251055"
    prijsGSL: "0.246835"
    prijsMDE: "0.246835"
    prijsNE: "0.247746"
    prijsTI: "0.247435"
    prijsVDB: "0.247532"
    prijsVON: "0.245620"
    prijsWE: "0.251055"
    prijsZG: "0.246835"
    prijsZP: "0.245655"
  - datum: "2024-01-07 08:00:00"
    prijs: "0.081790"
    prijsAA: "0.251816"
    prijsAIP: "0.260866"
    prijsANWB: "0.251796"
    prijsBE: "0.251606"
    prijsEE: "0.261476"
    prijsEN: "0.252936"
    prijsEVO: "0.251796"
    prijsEZ: "0.251796"
    prijsFR: "0.256016"
    prijsGSL: "0.251796"
    prijsMDE: "0.251796"
    prijsNE: "0.252707"
    prijsTI: "0.252396"
    prijsVDB: "0.252493"
    prijsVON: "0.250581"
    prijsWE: "0.256016"
    prijsZG: "0.251796"
    prijsZP: "0.250616"
  - datum: "2024-01-07 09:00:00"
    prijs: "0.084860"
    prijsAA: "0.255531"
    prijsAIP: "0.264581"
    prijsANWB: "0.255511"
    prijsBE: "0.255321"
    prijsEE: "0.265191"
    prijsEN: "0.256651"
    prijsEVO: "0.255511"
    prijsEZ: "0.255511"
    prijsFR: "0.259731"
    prijsGSL: "0.255511"
    prijsMDE: "0.255511"
    prijsNE: "0.256421"
    prijsTI: "0.256111"
    prijsVDB: "0.256208"
    prijsVON: "0.254296"
    prijsWE: "0.259731"
    prijsZG: "0.255511"
    prijsZP: "0.254331"
  - datum: "2024-01-07 10:00:00"
    prijs: "0.087140"
    prijsAA: "0.258290"
    prijsAIP: "0.267339"
    prijsANWB: "0.258270"
    prijsBE: "0.258079"
    prijsEE: "0.267949"
    prijsEN: "0.259409"
    prijsEVO: "0.258270"
    prijsEZ: "0.258270"
    prijsFR: "0.262489"
    prijsGSL: "0.258270"
    prijsMDE: "0.258270"
    prijsNE: "0.259180"
    prijsTI: "0.258869"
    prijsVDB: "0.258966"
    prijsVON: "0.257054"
    prijsWE: "0.262490"
    prijsZG: "0.258270"
    prijsZP: "0.257089"
  - datum: "2024-01-07 11:00:00"
    prijs: "0.089130"
    prijsAA: "0.260698"
    prijsAIP: "0.269747"
    prijsANWB: "0.260678"
    prijsBE: "0.260487"
    prijsEE: "0.270357"
    prijsEN: "0.261817"
    prijsEVO: "0.260678"
    prijsEZ: "0.260678"
    prijsFR: "0.264897"
    prijsGSL: "0.260678"
    prijsMDE: "0.260678"
    prijsNE: "0.261588"
    prijsTI: "0.261277"
    prijsVDB: "0.261374"
    prijsVON: "0.259462"
    prijsWE: "0.264898"
    prijsZG: "0.260678"
    prijsZP: "0.259497"
  - datum: "2024-01-07 12:00:00"
    prijs: "0.088660"
    prijsAA: "0.260129"
    prijsAIP: "0.269179"
    prijsANWB: "0.260109"
    prijsBE: "0.259919"
    prijsEE: "0.269789"
    prijsEN: "0.261249"
    prijsEVO: "0.260109"
    prijsEZ: "0.260109"
    prijsFR: "0.264329"
    prijsGSL: "0.260109"
    prijsMDE: "0.260109"
    prijsNE: "0.261019"
    prijsTI: "0.260709"
    prijsVDB: "0.260806"
    prijsVON: "0.258894"
    prijsWE: "0.264329"
    prijsZG: "0.260109"
    prijsZP: "0.258929"
  - datum: "2024-01-07 13:00:00"
    prijs: "0.084810"
    prijsAA: "0.255471"
    prijsAIP: "0.264520"
    prijsANWB: "0.255451"
    prijsBE: "0.255260"
    prijsEE: "0.265130"
    prijsEN: "0.256590"
    prijsEVO: "0.255451"
    prijsEZ: "0.255451"
    prijsFR: "0.259670"
    prijsGSL: "0.255451"
    prijsMDE: "0.255451"
    prijsNE: "0.256361"
    prijsTI: "0.256050"
    prijsVDB: "0.256147"
    prijsVON: "0.254235"
    prijsWE: "0.259670"
    prijsZG: "0.255451"
    prijsZP: "0.254270"
  - datum: "2024-01-07 14:00:00"
    prijs: "0.084340"
    prijsAA: "0.254902"
    prijsAIP: "0.263951"
    prijsANWB: "0.254882"
    prijsBE: "0.254691"
    prijsEE: "0.264561"
    prijsEN: "0.256021"
    prijsEVO: "0.254882"
    prijsEZ: "0.254882"
    prijsFR: "0.259101"
    prijsGSL: "0.254882"
    prijsMDE: "0.254882"
    prijsNE: "0.255792"
    prijsTI: "0.255481"
    prijsVDB: "0.255578"
    prijsVON: "0.253666"
    prijsWE: "0.259102"
    prijsZG: "0.254882"
    prijsZP: "0.253701"
  - datum: "2024-01-07 15:00:00"
    prijs: "0.088010"
    prijsAA: "0.259343"
    prijsAIP: "0.268392"
    prijsANWB: "0.259323"
    prijsBE: "0.259132"
    prijsEE: "0.269002"
    prijsEN: "0.260462"
    prijsEVO: "0.259323"
    prijsEZ: "0.259323"
    prijsFR: "0.263542"
    prijsGSL: "0.259323"
    prijsMDE: "0.259323"
    prijsNE: "0.260233"
    prijsTI: "0.259922"
    prijsVDB: "0.260019"
    prijsVON: "0.258107"
    prijsWE: "0.263542"
    prijsZG: "0.259323"
    prijsZP: "0.258142"
  - datum: "2024-01-07 16:00:00"
    prijs: "0.093420"
    prijsAA: "0.265889"
    prijsAIP: "0.274938"
    prijsANWB: "0.265869"
    prijsBE: "0.265678"
    prijsEE: "0.275548"
    prijsEN: "0.267008"
    prijsEVO: "0.265869"
    prijsEZ: "0.265869"
    prijsFR: "0.270088"
    prijsGSL: "0.265869"
    prijsMDE: "0.265869"
    prijsNE: "0.266779"
    prijsTI: "0.266468"
    prijsVDB: "0.266565"
    prijsVON: "0.264653"
    prijsWE: "0.270089"
    prijsZG: "0.265869"
    prijsZP: "0.264688"
  - datum: "2024-01-07 17:00:00"
    prijs: "0.101770"
    prijsAA: "0.275992"
    prijsAIP: "0.285042"
    prijsANWB: "0.275972"
    prijsBE: "0.275782"
    prijsEE: "0.285652"
    prijsEN: "0.277112"
    prijsEVO: "0.275972"
    prijsEZ: "0.275972"
    prijsFR: "0.280192"
    prijsGSL: "0.275972"
    prijsMDE: "0.275972"
    prijsNE: "0.276883"
    prijsTI: "0.276572"
    prijsVDB: "0.276669"
    prijsVON: "0.274757"
    prijsWE: "0.280192"
    prijsZG: "0.275972"
    prijsZP: "0.274792"
  - datum: "2024-01-07 18:00:00"
    prijs: "0.104850"
    prijsAA: "0.279719"
    prijsAIP: "0.288769"
    prijsANWB: "0.279699"
    prijsBE: "0.279509"
    prijsEE: "0.289379"
    prijsEN: "0.280839"
    prijsEVO: "0.279699"
    prijsEZ: "0.279699"
    prijsFR: "0.283919"
    prijsGSL: "0.279699"
    prijsMDE: "0.279699"
    prijsNE: "0.280609"
    prijsTI: "0.280299"
    prijsVDB: "0.280396"
    prijsVON: "0.278484"
    prijsWE: "0.283919"
    prijsZG: "0.279699"
    prijsZP: "0.278519"
  - datum: "2024-01-07 19:00:00"
    prijs: "0.103550"
    prijsAA: "0.278146"
    prijsAIP: "0.287196"
    prijsANWB: "0.278126"
    prijsBE: "0.277936"
    prijsEE: "0.287806"
    prijsEN: "0.279266"
    prijsEVO: "0.278126"
    prijsEZ: "0.278126"
    prijsFR: "0.282346"
    prijsGSL: "0.278126"
    prijsMDE: "0.278126"
    prijsNE: "0.279036"
    prijsTI: "0.278726"
    prijsVDB: "0.278823"
    prijsVON: "0.276911"
    prijsWE: "0.282346"
    prijsZG: "0.278126"
    prijsZP: "0.276946"
  - datum: "2024-01-07 20:00:00"
    prijs: "0.100500"
    prijsAA: "0.274455"
    prijsAIP: "0.283505"
    prijsANWB: "0.274436"
    prijsBE: "0.274245"
    prijsEE: "0.284115"
    prijsEN: "0.275575"
    prijsEVO: "0.274436"
    prijsEZ: "0.274436"
    prijsFR: "0.278655"
    prijsGSL: "0.274436"
    prijsMDE: "0.274436"
    prijsNE: "0.275346"
    prijsTI: "0.275035"
    prijsVDB: "0.275132"
    prijsVON: "0.273220"
    prijsWE: "0.278655"
    prijsZG: "0.274436"
    prijsZP: "0.273255"
  - datum: "2024-01-07 21:00:00"
    prijs: "0.093310"
    prijsAA: "0.265756"
    prijsAIP: "0.274805"
    prijsANWB: "0.265736"
    prijsBE: "0.265545"
    prijsEE: "0.275415"
    prijsEN: "0.266875"
    prijsEVO: "0.265736"
    prijsEZ: "0.265736"
    prijsFR: "0.269955"
    prijsGSL: "0.265736"
    prijsMDE: "0.265736"
    prijsNE: "0.266646"
    prijsTI: "0.266335"
    prijsVDB: "0.266432"
    prijsVON: "0.264520"
    prijsWE: "0.269955"
    prijsZG: "0.265736"
    prijsZP: "0.264555"
  - datum: "2024-01-07 22:00:00"
    prijs: "0.091170"
    prijsAA: "0.263166"
    prijsAIP: "0.272216"
    prijsANWB: "0.263146"
    prijsBE: "0.262956"
    prijsEE: "0.272826"
    prijsEN: "0.264286"
    prijsEVO: "0.263146"
    prijsEZ: "0.263146"
    prijsFR: "0.267366"
    prijsGSL: "0.263146"
    prijsMDE: "0.263146"
    prijsNE: "0.264057"
    prijsTI: "0.263746"
    prijsVDB: "0.263843"
    prijsVON: "0.261931"
    prijsWE: "0.267366"
    prijsZG: "0.263146"
    prijsZP: "0.261966"
  - datum: "2024-01-07 23:00:00"
    prijs: "0.083860"
    prijsAA: "0.254321"
    prijsAIP: "0.263371"
    prijsANWB: "0.254301"
    prijsBE: "0.254111"
    prijsEE: "0.263981"
    prijsEN: "0.255441"
    prijsEVO: "0.254301"
    prijsEZ: "0.254301"
    prijsFR: "0.258521"
    prijsGSL: "0.254301"
    prijsMDE: "0.254301"
    prijsNE: "0.255211"
    prijsTI: "0.254901"
    prijsVDB: "0.254998"
    prijsVON: "0.253086"
    prijsWE: "0.258521"
    prijsZG: "0.254301"
    prijsZP: "0.253121"
friendly_name: Stroomprijs enever

Update for who it might help:

I have created the follwoing template code (test it in the developer tools)

{% set now_ts = as_timestamp(utcnow()) %}
now_ts: {{ now_ts }}
{% set values = namespace() %}
{% set values.current = None %}
{% for row in states.sensor.stroomprijs_enever.attributes.data %}
{% set row_timestamp = as_timestamp(row.datum) | round(0) %}
row_timestamp: {{ row_timestamp | int | timestamp_custom("%d.%m.%Y %H:%M") }} 
{% set values.current1 = row %}
{{ values.current1.prijsBE }}
{% endfor -%}

The result is a list of the date today from 00:00 to 23:00.

         08.01.2024 00:00       
          0.257934
               
          08.01.2024 01:00          
          0.254704
          
          08.01.2024 02:00        
          0.251509      
          
          08.01.2024 03:00        
          0.248230
                
          08.01.2024 04:00 
          0.247589
          
          08.01.2024 05:00 
          0.251981
          
          08.01.2024 06:00 
          0.266743
          
          08.01.2024 07:00 
          0.287059
          
          08.01.2024 08:00 
          0.299183
          
          08.01.2024 09:00   
          0.295214
          
          08.01.2024 10:00     
          0.286442
          
          08.01.2024 11:00      
          0.277270
          
          08.01.2024 12:00 
          0.255369
          
          08.01.2024 13:00 
          0.261008
          
          08.01.2024 14:00   
          0.273640
          
          
          08.01.2024 15:00  
          0.283296
          
          08.01.2024 16:00   
          0.293472
          
          08.01.2024 17:00    
          0.308912
          
          08.01.2024 18:00 
          0.303987
          
          08.01.2024 19:00  
          0.288039
          
          08.01.2024 20:00 
          0.273761
          
          08.01.2024 21:00   
          0.262750
          
          08.01.2024 22:00   
          0.263960
          
          08.01.2024 23:00 
          0.263343

Next step to get this list into sensor(s) per hour 00:00 to 23:00 so I am able to create a graph from that enties (or entities) and I would like to compare the vaulues per hour to find the lowest number for that day.

@petro If you have any pointers on the next steps to get to my end result I very much appriciate it! :slight_smile:

I’ll keep working on it
Thanks!

This finds the minimum price and the corresponding timestamp:

{% set minprice = state_attr('states.sensor.stroomprijs_enever', 'data')|map(attribute='prijsBE')|min %}
{{ minprice }}
{{ (state_attr('states.sensor.stroomprijs_enever', 'data')|selectattr('prijsBE','eq',minprice)|first)['datum'] }}

Should work for you in the Template editor.

Not sure what you’re aiming for with the individual hourly sensors. Show us what you think they should look like: do you want a “07:00” sensor or a “four hours from now” sensor? What graph are you thinking to plot?

1 Like

Thanks for having a look at this, When I use you code in the template editor it gives:

UndefinedError: No first item, sequence was empty.

I guess I need to define a value for first to start with?

What I am looking to do is plot a bar graph of every value for the day. This gives me a graphical indication of all hourly power pricing for that day.

It would be nice, in addition, to get a sensor from it that has the value of cheapest cost for power that on a certain hour for that day. That way I can use that sensor to automate on as that hour would have the cheapest power pricing.

Thanks!

I am only interrested in the price for the attribute:

prijsBE

That one hold the data for my power provider per hour. I load the data once a day with a rest API call so that source data is available per hour. (If that makes sense)

First line finds the smallest value for prijsBE, second line prints it. Does that work, without the third line?

The third line should then pull out all entries where the prijsBE is equal to that low value, take the first (and possibly only) one, and print its datum timestamp.

The third line will only work if the first line is there to define minprice.

Hi,

Just tried the first two lines in the developer tool, it gives me the follwoing error:

TypeError: min expected at least 1 argument, got 0

Hmmmmm I cleaned the dedveloper tool ,repasted and the error changed to:

No aggregated item, sequence was empty.

When I just run the first line, there is no error, it’s when I print the result {{ minprice }} that it comes up with the result, maybe it just did not find anything.

Is there another way I could try?

I found the template to get the lowest price for today:

{{ state_attr('sensor.stroomprijs_enever', 'data')|map(attribute='prijsBE')|min }}

Now I need to get the associated hour for this low price, do I just run the same template but then for the “datum” attribute? hmmm work in progress :slight_smile:

@Troon (All kudo’s to Troon)

Got it to find the lowest price at which hour:

{% set minprice = state_attr('sensor.stroomprijs_enever', 'data')|map(attribute='prijsBE')|min %}
{{ minprice }}

{{ (state_attr('sensor.stroomprijs_enever', 'data')|selectattr('prijsBE','eq',minprice)|first)['datum'] }}


result

0.247589

2024-01-08 04:00:00

For the time of day sensor which needs the time when the power price is at it’s lowest:

{% set minprice = state_attr('sensor.stroomprijs_enever', 'data')|map(attribute='prijsBE')|min %}
{% set mindatum = (state_attr('sensor.stroomprijs_enever', 'data')|selectattr('prijsBE','eq',minprice)|first)['datum'] %}
{% set mindatum_timestamp = as_timestamp(mindatum) %}
{{ mindatum_timestamp | int | timestamp_custom(" %H:%M") }} 

Result gives you the hour in this case 04:00

First Item :white_check_mark: af the list, I can now find which hour of the day gives me the lowest price so I can automate on that.

Now to the second part of the puzzel, I would like to graph all the prijsBE value’s per hour of the day as a bar chart, looked at APEX card which might do this for me but need some logic there to I see.

Any suggestions how to graph the prijsBE vaulues from this sensor per hour are appriciated!

So for the people using a power pricing feed with multiple providers like the one of enever.nl can use the apexcard to plot the graph:

  • Prerequisit: Install the apexcard via HACS
type: custom:apexcharts-card
graph_span: 1d
span:
  start: day
experimental:
  color_threshold: true
header:
  show: true
  title: Stroomprijs verloop vandaag (exclusief inkoopprijs 0.02099)
  show_states: true
  colorize_states: true
now:
  show: true
  color: '#ff0000'
  label: NU
apex_config:
  yaxis:
    forceNiceScale: true
  dataLabels:
    background:
      borderWidth: 0
      opacity: 0
      foreColor: '#03A9F4'
  plotOptions:
    bar:
      borderRadius: 5
      dataLabels:
        position: top
series:
  - entity: sensor.stroomprijs_enever
    show:
      datalabels: true
    float_precision: 4
    type: column
    name: Stroomprijs Budget Energie
    data_generator: |
      return entity.attributes.data.map((entry) => {
        return [new Date(entry.datum), entry.prijsBE];
      });
    color_threshold:
      - value: 0.3
        color: darkgreen
        opacity: 1
      - value: 0.35
        color: goldenrod
      - value: 1.25
        color: darkred
yaxis:
  - min: 0
    max: 0.4
    decimals: 3
    apex_config:
      tickAmount: 4