History is stored in the database for the default 10 days, and it can be extracted. The ApexCharts card does this for you (from the history tables) and does the ‘group by’ summation required.
If you otherwise want to display the costs for the past 7 days, you have to get the data from history by other means.
It appears to be quite possible to create an SQL sensor using the SQL integration. This will perform an SQL query on the HA database, and can be used to return just one value into a sensor state. In your case, you are looking for the summation of the past week of sensor.octopus_energy_electricity_xxxxxx_current_accumulative_cost
state value.
I recently worked out the required SQL query to get at the history for these sensors, once you have first worked out the meta-id value.
If you use the SQLiteWeb add-on in HA, you can access the HA database, and run the SQL query on the statistics_meta table:
SELECT * FROM "statistics_meta" where "statistic_id" LIKE 'sensor.octopus_energy%previous%' AND "unit_of_measurement" = "GBP"
which will give the sensors for just the Octopus energy previous cost figures (use ‘current’ for the current sensors)
You then need to pick out the meta-id of your sensor of choice (in my case this is 115) and then create an SQL sensor (use the SQL integration) with the following query. Change the 115 value!
SELECT SUM(state) AS total FROM
(SELECT id, state, udate FROM
(SELECT metadata_id AS id, state, date(created_ts, "unixepoch") AS udate, time(created_ts, "unixepoch") AS utime
FROM "statistics"
WHERE metadata_id = 115 AND unixepoch()-created_ts < 86400 * 7 AND utime > "20" AND utime < "23"
ORDER BY udate, utime DESC)
GROUP BY udate)
This returns the sum in ‘total’ so use ‘total’ as the query table column name. Nothing else, apparently, is required and it, apparently, works.
You will need to set up one SQL sensor for each of electricity and gas, and then use these new sensors in your display.
I am no expert in SQL, so there may be someone who can simplify this as I am just using what worked for me in a bit of Node-RED code.