Template with state_attr and value_json

Hi.

I have this sensor:

- platform: template
  sensors:
    electricity_price_by_timezone:
      friendly_name: Electricity price by timezone
      entity_id: sensor.elspot_kwheeeur410
      value_template: "{{ state_attr('sensor.elspot_kwheeeur410','today') }}"

Template editor output:

- platform: template
  sensors:
    electricity_price_by_timezone:
      friendly_name: Electricity price by timezone
      entity_id: sensor.elspot_kwheeeur410
      value_template: "[0.0364, 0.035, 0.0342, 0.0338, 0.0347, 0.0465, 0.0593, 0.0656, 0.0672, 0.0674, 0.0667, 0.0667, 0.0671, 0.0667, 0.0643, 0.0569, 0.0565, 0.0584, 0.0642, 0.0646, 0.0492, 0.0433, 0.0359, 0.0343]"

How can I narrow down the “today” attribute to :

'today[now().hour-1]'

how does now().hour coorelate to the list?

[0.0364, 0.035, 0.0342, 0.0338, 0.0347, 0.0465, 0.0593, 0.0656, 0.0672, 0.0674, 0.0667, 0.0667, 0.0671, 0.0667, 0.0643, 0.0569, 0.0565, 0.0584, 0.0642, 0.0646, 0.0492, 0.0433, 0.0359, 0.0343]

if it’s 24 hours, you solved it yourself…

- platform: template
  sensors:
    electricity_price_by_timezone:
      friendly_name: Electricity price by timezone
      entity_id: sensor.elspot_kwheeeur410
      value_template: "{{ state_attr('sensor.elspot_kwheeeur410','today')[now().hour-1] }}"

but you’ll need to add a sensor that updates hourly or minutely for it to update if sensor.elspot_kwheeeur410 updates once a day.

Also, if you’re trying to get the last hour and the sensor updates every hour… then you could even do this

- platform: template
  sensors:
    electricity_price_by_timezone:
      friendly_name: Electricity price by timezone
      entity_id: sensor.elspot_kwheeeur410
      value_template: "{{ state_attr('sensor.elspot_kwheeeur410','today')[-1] }}"

Thank you.
Yes it is 24h array.
I had the array index at the wrong place:

value_template: "{{ state_attr('sensor.elspot_kwheeeur410','today[now().hour-1]') }}"

instead of:

value_template: "{{ state_attr('sensor.elspot_kwheeeur410','today')[now().hour-1] }}"

If I change entity_id to now().hour then it should update hourly, right?
I need to include some if-statement(s) to switch over to next attribute “tomorrow” when next day’s first hour kicks in (1-1=0). Otherwise I would fall back to today[0] .

Nope, that’s not a state object. Home assistant does not watch that.

I can’t think of an hourly state_object, so you should probably just integrate sensor.time from the Time & Date integration.