Trouble migrating YAML to Python Script (read tibber price info)

Hi, I am migrating YAML to Python.
I menage to read and write the state of an entity this way:

# get the value of an entity e
def getE(e):
    inputStateObject = hass.states.get(e)
    inputState = inputStateObject.state
    return inputState

# set the value of an entity e
def setE(e, val):
    e1 = e.split('.')
    service_data = {"entity_id": e, "value": val}
    hass.services.call(e1[0], "set_value", service_data, False)

# demo:
x = getE("input_number.num_ess_sollwert")
setE("input_number.dummy", x)

BUT: in this case (tibber price info) there are all infos I need hidden in the attributes:

To read the prices per hour it works in YAML this way for e.g. three o’clock in the morning, addressing the price via an array:

target:
  entity_id: input_number.tibber_preis_03h
data:
  value: |
    {{ (states.sensor.tibber_preis_info.attributes.today[3].total) }}
enabled: true
action: input_number.set_value

BUT: this leads to 2x24 stupid YAML blocks (24 price info each for today and tomorrow) instead of adressing the dictionary in Python someway like

# not working:
for t_hour in range(24)
    price[t_hour] = states.sensor.tibber_preis_info.attributes.today[t_hour].total

Please help, thank you!

…solved. After a short dialog with chatGPT: here is the solution, works fine…

# Entity ID für die Tibber Preis-Info
entity_id = 'sensor.tibber_preis_info'

# Zugriff auf die Entität
state = hass.states.get(entity_id)

if state is not None:
    # Zugriff auf die Attribute der Entität
    attributes = state.attributes

    # Extrahiere die Preise für heute
    today_prices = attributes.get('today', [])
    # Extrahiere die Preise für morgen
    tomorrow_prices = attributes.get('tomorrow', [])

    # Erstelle je eine Liste nur mit den Preisen
    prices_today    = [entry['total'] for entry in today_prices]
    prices_tomorrow = [entry['total'] for entry in tomorrow_prices]

    #setE("input_number.dummy", prices_today[23])

else:
    print("Diese Entität existiert nicht.")