How to access an entity attribute

I like to access an attribute from a entiry. sensor.energy_production_today with a state = 32.465 and the attribute list is :

watts: 
'2025-07-04T00:00:00+02:00': 0
'2025-07-04T00:15:00+02:00': 0
'2025-07-04T00:30:00+02:00': 0
'2025-07-04T00:45:00+02:00': 0
'2025-07-04T01:00:00+02:00': 0
'2025-07-04T01:15:00+02:00': 0
'2025-07-04T01:30:00+02:00': 0
'2025-07-04T01:45:00+02:00': 0
'2025-07-04T02:00:00+02:00': 0
'2025-07-04T02:15:00+02:00': 0
'2025-07-04T02:30:00+02:00': 0
'2025-07-04T02:45:00+02:00': 0
'2025-07-04T03:00:00+02:00': 0
'2025-07-04T03:15:00+02:00': 0
'2025-07-04T03:30:00+02:00': 0
'2025-07-04T03:45:00+02:00': 0
'2025-07-04T04:00:00+02:00': 0
'2025-07-04T04:15:00+02:00': 0
'2025-07-04T04:30:00+02:00': 0
'2025-07-04T04:45:00+02:00': 0
'2025-07-04T05:00:00+02:00': 0
'2025-07-04T05:15:00+02:00': 0
'2025-07-04T05:30:00+02:00': 0
'2025-07-04T05:45:00+02:00': 18
'2025-07-04T06:00:00+02:00': 100
'2025-07-04T06:15:00+02:00': 220
'2025-07-04T06:30:00+02:00': 311
'2025-07-04T06:45:00+02:00': 386
'2025-07-04T07:00:00+02:00': 535
'2025-07-04T07:15:00+02:00': 695
'2025-07-04T07:30:00+02:00': 866
'2025-07-04T07:45:00+02:00': 1037
'2025-07-04T08:00:00+02:00': 1214
'2025-07-04T08:15:00+02:00': 1400
'2025-07-04T08:30:00+02:00': 1598
'2025-07-04T08:45:00+02:00': 1810
'2025-07-04T09:00:00+02:00': 2037
'2025-07-04T09:15:00+02:00': 2263
'2025-07-04T09:30:00+02:00': 2484
'2025-07-04T09:45:00+02:00': 2699
'2025-07-04T10:00:00+02:00': 2901
'2025-07-04T10:15:00+02:00': 3076
'2025-07-04T10:30:00+02:00': 3216
'2025-07-04T10:45:00+02:00': 3314
'2025-07-04T11:00:00+02:00': 3371
'2025-07-04T11:15:00+02:00': 3400
'2025-07-04T11:30:00+02:00': 3398
'2025-07-04T11:45:00+02:00': 3331
'2025-07-04T12:00:00+02:00': 3244
'2025-07-04T12:15:00+02:00': 3192
'2025-07-04T12:30:00+02:00': 3246
'2025-07-04T12:45:00+02:00': 3431
'2025-07-04T13:00:00+02:00': 3648
'2025-07-04T13:15:00+02:00': 3810
'2025-07-04T13:30:00+02:00': 3823
'2025-07-04T13:45:00+02:00': 3728
'2025-07-04T14:00:00+02:00': 3568
'2025-07-04T14:15:00+02:00': 3369
'2025-07-04T14:30:00+02:00': 3167
'2025-07-04T14:45:00+02:00': 2917
'2025-07-04T15:00:00+02:00': 2653
'2025-07-04T15:15:00+02:00': 2437
'2025-07-04T15:30:00+02:00': 2307
'2025-07-04T15:45:00+02:00': 2246
'2025-07-04T16:00:00+02:00': 2231
'2025-07-04T16:15:00+02:00': 2233
'2025-07-04T16:30:00+02:00': 2248
'2025-07-04T16:45:00+02:00': 2295
'2025-07-04T17:00:00+02:00': 2342
'2025-07-04T17:15:00+02:00': 2360
'2025-07-04T17:30:00+02:00': 2332
'2025-07-04T17:45:00+02:00': 2280
'2025-07-04T18:00:00+02:00': 2197
'2025-07-04T18:15:00+02:00': 2078
'2025-07-04T18:30:00+02:00': 1913
'2025-07-04T18:45:00+02:00': 1716
'2025-07-04T19:00:00+02:00': 1504
'2025-07-04T19:15:00+02:00': 1297
'2025-07-04T19:30:00+02:00': 1099
'2025-07-04T19:45:00+02:00': 907
'2025-07-04T20:00:00+02:00': 730
'2025-07-04T20:15:00+02:00': 563
'2025-07-04T20:30:00+02:00': 413
'2025-07-04T20:45:00+02:00': 285
'2025-07-04T21:00:00+02:00': 212
'2025-07-04T21:15:00+02:00': 129
'2025-07-04T21:30:00+02:00': 42
'2025-07-04T21:45:00+02:00': 0
'2025-07-04T22:00:00+02:00': 0
'2025-07-04T22:15:00+02:00': 0
'2025-07-04T22:30:00+02:00': 0
'2025-07-04T22:45:00+02:00': 0
'2025-07-04T23:00:00+02:00': 0
'2025-07-04T23:15:00+02:00': 0
'2025-07-04T23:30:00+02:00': 0
'2025-07-04T23:45:00+02:00': 0

wh_period: 
'2025-07-04T00:00:00+02:00': 0
'2025-07-04T01:00:00+02:00': 0
'2025-07-04T02:00:00+02:00': 0
'2025-07-04T03:00:00+02:00': 0
'2025-07-04T04:00:00+02:00': 0
'2025-07-04T05:00:00+02:00': 7.25
'2025-07-04T06:00:00+02:00': 227.25
'2025-07-04T07:00:00+02:00': 729.5
'2025-07-04T08:00:00+02:00': 1440.25
'2025-07-04T09:00:00+02:00': 2302
'2025-07-04T10:00:00+02:00': 3064.5
'2025-07-04T11:00:00+02:00': 3330.25
'2025-07-04T12:00:00+02:00': 3254.75
'2025-07-04T13:00:00+02:00': 3745.5
'2025-07-04T14:00:00+02:00': 3266
'2025-07-04T15:00:00+02:00': 2433
'2025-07-04T16:00:00+02:00': 2287.25
'2025-07-04T17:00:00+02:00': 2382.75
'2025-07-04T18:00:00+02:00': 2043.5
'2025-07-04T19:00:00+02:00': 1266.5
'2025-07-04T20:00:00+02:00': 545.25
'2025-07-04T21:00:00+02:00': 139.5
'2025-07-04T22:00:00+02:00': 0
'2025-07-04T23:00:00+02:00': 0

unit_of_measurement: kWh
device_class: energy
friendly_name: Solar production forecast Estimated energy production - today

With a template I get the watts table:

{{state_attr("sensor.energy_production_today","watts")}}

But like you can see in the table there is for every 15 minutes a value.
I don’t find a way to get the value on the present hour.

It is now 10 o’clock then the template gives the value out of the table 2025-07-04T10:00:00 => 3064.5

Can somebody point me in the direction how to make that automation?
Thanks

something like this?

{% set watts = state_attr('sensor.energia_hidro_energy_power', 'watts') %}
{% if watts %}
  {% set ts = now().replace(minute=0, second=0, microsecond=0) %}
  {% set offset = ts.strftime('%z') %}
  {% set offset_correct = offset[:3] ~ ':' ~ offset[3:] %}
  {% set key = ts.strftime('%Y-%m-%dT%H:00:00') ~ offset_correct %}
  {{ watts[key] if key in watts else 'Key not found' }}
{% else %}
  'No watts data'
{% endif %}

The value 3064.5 is from the sensor’s wh_period attribute. Therefore I have configured the following template to get the value from the sensor’s wh_period attribute (not the watts attribute).

{% set wh_period = state_attr('sensor.energy_production_today', 'wh_period') | default({}, true) %}
{% set dt_now = now().strftime('%Y-%m-%dT%H:00:00%z')[:-2] ~ ':00' %}
{{ wh_period.get(dt_now, none) }}

Copy-paste the example into the Template Editor and confirm it reports the correct value for the current hour period. The template is designed to return none (the null object) if there is no data for the current hour period.


NOTE

If you want to report values from the watts attribute, simply replace the string wh_period with watts in the template’s first line.

state_attr('sensor.energy_production_today', 'watts')
                                              ^^^^^

EDIT

Correction. Removed needless opening braces from the example. Thanks WarLion!

Sorry, I looked in the wrong table (not the wh_period ). It has to be in the watts table.

I tried the template of WarLion. It is working but the time now it is 20:56 in Belgium and the result gifs the value 724. That is the value at 20.00.

{% set wh_period = state_attr('sensor.energia_hidro_energy_today', 'wh_period') | default({}, true) %}
{% set dt_now = now().strftime('%Y-%m-%dT%H:00:00%z')[:-2] ~ ':00' %}
{{ wh_period.get(dt_now, none) }}

You should not wrap a state_attr() function call in {{ }}

Why should I do that when I am assigning its result to a Jinja2 variable?

{{ … }} is used for output
{% … %} is used for logic

You’re essentially trying to output something {{ … }} inside a logic block {% … %}, which doesn’t make sense and will result in a template syntax error.

In that case the value at 10:00 is 2901.

'2025-07-04T10:00:00+02:00': 2901

remember home assistant store values every 60 min


1 Like

I am assigning the output of state_attr() to a Jinja2 variable. Please post an example of another way to assign a value to a variable.
Duh! I now see what you’re saying!

It’s a copy-paste error I made when I transferred my test example to the forum.

Thanks for pointing it out to me!

1 Like

i did in this response to your code

1 Like

What value do you want it to report at 20:56? The two closest values are:

'2025-07-04T20:45:00+02:00': 285
'2025-07-04T21:00:00+02:00': 212

Do you want it to report 285 or 212?


EDIT

Try this version.

{% set watts = state_attr('sensor.energia_hidro_energy_today', 'watts') | default({}, true) %}
{% set dt_now = (now().strftime('%Y-%m-%dT%H:MM:00%z')[:-2] ~ ':00')
  | replace('MM', '{:02d}'.format((now().minute // 15) * 15)) %}
{{ watts.get(dt_now, none) }}

For example, if your current time is 21:39, it will report the value for 21:30. If your time is 21:52, it will report the value for 21:45.

'2025-07-04T21:30:00+02:00': 42
'2025-07-04T21:45:00+02:00': 0

As another option, you can map the keys of the attributes to datetime objects so that you can do datetime comparisons. Then you don’t have to worry about timezone offsets:

{% set watts = state_attr('sensor.energia_hidro_energy_today', 'watts') | default({}, true) %}
{% set watts_dict = zip(watts.keys() | map('as_datetime'), watts.values() ) %}
{{ watts_dict | selectattr(0, '<=', now()) | map(attribute=1) | list | last }}

Replace the last line to get the next time interval instead of the previous:

{{ watts_dict | selectattr(0, '>=', now()) | map(attribute=1) | list | first }}
1 Like

Nice. :+1:
I was under the mistaken impression that as_datetime wouldn’t be able to convert the datetime string because of the colon in its timezone. I should’ve tested it rather than rely on a hunch.

@pepe2
Did you get a chance to test what I had suggested yesterday?

{% set watts = state_attr('sensor.energia_hidro_energy_today', 'watts') | default({}, true) %}
{% set dt_now = (now().strftime('%Y-%m-%dT%H:MM:00%z')[:-2] ~ ':00') | replace('MM', '{:02d}'.format((now().minute // 15) * 15)) %}
{{ watts.get(dt_now, none) }}

What was the result?