Editing the data from a Rest Sensor (Tibber Data)

Hi.
I have a Sensor with data from Tibber.

- platform: rest
  name: Tibber REST
  resource: https://api.tibber.com/v1-beta/gql
  method: POST
  scan_interval: 60
  payload: '{ "query": "{ viewer { homes { currentSubscription { priceInfo { today { total startsAt } tomorrow { total startsAt }}}}}}" }'
  json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo"
  json_attributes:
    - today
    - tomorrow
  value_template: "OK"
  headers:
    Authorization: !secret tibber_token
    Content-Type: application/json
    User-Agent: REST

It gets me a table like:

today: 
- total: 0.1556
  startsAt: '2024-08-25T00:00:00.000+02:00'
- total: 0.1552
  startsAt: '2024-08-25T01:00:00.000+02:00'
- total: 0.1548
  startsAt: '2024-08-25T02:00:00.000+02:00'

I would like to have the totals as cents not € so multiply by 100.
And startsAt is with an offset in timezone and i would like to have it calculate the hours into it.
I tryed a few things and searched for an answer but some are old and dont work with the syntax anymore and some dont work at all.
How would i do it?
I tryed with an template sensor like this:

    strompreisvorschau:
      friendly_name: Strompreisvorschau
      value_template: >
        {{ states('sensor.tibber_rest')}}
      attribute_templates:
        today: >
          {% set today_data = state_attr('sensor.tibber_rest', 'today') %}
          {% for entry in today_data %}
            - total: {{ (entry.total * 100) | round(2) }}
              startsAt: "{{ as_timestamp(entry.startsAt) | timestamp_local() }}"
          {% endfor %}
        tomorrow: >
          {% set tomorrow_data = state_attr('sensor.tibber_rest', 'tomorrow') %}
          {% for entry in tomorrow_data %}
            - total: {{ (entry.total * 100) | round(2) }}
              startsAt: "{{ as_timestamp(entry.startsAt) | timestamp_local() }}"
          {% endfor %}

but the output is like:

today: - total: 15.56
    startsAt: "2024-08-25T00:00:00+02:00"

  - total: 15.52
    startsAt: "2024-08-25T01:00:00+02:00"

  - total: 15.48
    startsAt: "2024-08-25T02:00:00+02:00"

  - total: 15.4
    startsAt: "2024-08-25T03:00:00+02:00"

the total seems to be correct but there are empty lines and the date is still wrong.
Can someone help me?

This is what ChatGPT gave me. The time conversion works, but I didn’t test the additional line thing.

sensor:
  - platform: template
    sensors:
      strompreisvorschau:
        friendly_name: Strompreisvorschau
        value_template: >
          {{ states('sensor.tibber_rest')}}
        attribute_templates:
          today: >
            {% set hour_offset = 0 %}  # Change this value to adjust hours
            {% set today_data = state_attr('sensor.tibber_rest', 'today') %}
            {% if today_data %}
              [
              {% for entry in today_data %}
                {
                  "total": {{ (entry.total * 100) | round(2) }},
                  "startsAt": "{{ (as_timestamp(entry.startsAt) + (hour_offset * 3600)) | timestamp_local | timestamp_custom('%Y-%m-%dT%H:%M:%S') }}"
                }{{ "," if not loop.last else "" }}
              {% endfor %}
              ]
            {% else %}
              []
            {% endif %}
          tomorrow: >
            {% set hour_offset = 0 %}  # Change this value to adjust hours
            {% set tomorrow_data = state_attr('sensor.tibber_rest', 'tomorrow') %}
            {% if tomorrow_data %}
              [
              {% for entry in tomorrow_data %}
                {
                  "total": {{ (entry.total * 100) | round(2) }},
                  "startsAt": "{{ (as_timestamp(entry.startsAt) + (hour_offset * 3600)) | timestamp_local | timestamp_custom('%Y-%m-%dT%H:%M:%S') }}"
                }{{ "," if not loop.last else "" }}
              {% endfor %}
              ]
            {% else %}
              []
            {% endif %}

Explanation:

  1. Handling JSON Output Correctly: The template now ensures the output is a valid JSON array ([]) with objects for each entry. This prevents the empty lines issue and makes it easier for other integrations or automations to parse.
  2. Date Conversion: The startsAt field is converted using as_timestamp(entry.startsAt) | timestamp_local, which correctly handles timezone offsets. The output is formatted as a string in the format YYYY-MM-DDTHH:MM:SS.
  3. Avoiding Extra Commas: The loop.last check ensures that there is no trailing comma after the last entry, which is important for valid JSON formatting.
  4. Empty Data Handling: If either the today or tomorrow attribute is missing or empty, the output will be an empty list ([]), preventing errors.

This should give you the desired output without the extra lines and with correctly formatted dates.