I just wanted to add some info on this because although these forums are great I really struggled to get enough info and I’m sure I am not alone. HA is quite difficult and very tricky to get right. So I’ll go through the steps I have taken as a newbie, some of these will be obvious to some but perhaps not to others.
First I will assume that people looking for this data have already implemented the energy tab and got values showing there.
I wanted to get the daily costs, i.e. the right hand values from the energy tab and use them elsewhere on a separate dashboard, rather than having to click on the energy tab.
Anyway here’s the data I wanted to grab …
So I started by installing SqlLite into HA and then in the query tab of the table statistics_meta I ran this query, to look for the data that corresponds to the name “Study”.
SELECT * FROM “statistics_meta” where statistic_id like ‘%study%’
The results give me this…
id statistic_id source unit_of_measurement has_mean has_sum name
6 sensor.study_hub_today_energy_cost recorder EUR 0 1 NULL
39 sensor.study_hub_current_power recorder W 1 0
So then I used the ID (6) in the next query on the statistics table:
(SELECT round(max(sum), 2)
WHERE metadata_id = 6
AND date(created_ts, ‘unixepoch’) = date(‘now’))
(SELECT round(min(sum), 2)
WHERE metadata_id = 6
AND date(created_ts, ‘unixepoch’) = date(‘now’)),
2) AS sum
Although I know SQL well enough I realise others do not, so what the query does it to use the ID of the sensor to select the latest row from the table for today’s date, and then selects the earliest row from the table for today’s date, and then subtracts one from the other. This is because the statistics table, as far as I make out, stores the costs in accumulated amounts. So we take the latest figure, subtract the earlier figure, and the difference is the actual cost so far today. This should match the value on the energy tab.
Now to get this into a usable value I tried to use “configuration by YAML” section on SQL - Home Assistant but I had no joy at all, I spent days tweaking it and it never worked for me, so instead I used the sql integration option which is the first method shown on the page. Much simpler… I added the integration SQL and populated as follows:
I give the query a name, add “sum” to the column, paste in the query from above and click submit and finish. On the integration page when I click on the “study cost” integration it shows the following under the sensor tab, so now I have the actual cost in HA.
And finally I used a markdown card on my dashboard as follows……
I’m sure there are better and easier ways to get data into a statistics sensor, but this method works for me. I might try other methods as I get more familiar with HA, such as the proper statistics integration as per Statistics - Home Assistant.