How can I export data from a sensor to a .csv or other format?

Hi,

I have a Kasa smart energy monitor which runs and logs under sensor.energy_meter_smart_switch_watts

I can configure the Lovelace card to show say 24 hours of usage, but is there any way to dump out all historical usage based on that sensor to a .csv so I can analyse it?
We’re trying to show an increase in wattage after firmware update on an LG dryer but I’m not really savvy enough with HA to know how to extract that data!

Any help is massively appreciated!

2 Likes

I have found the free version of TablePlus very useful to export csv data from my Home Assistant states and events tables. I used Postgresql as my database for HA, however this tool will query the native SQLite database of HA as well.

https://tableplus.com/

Query example to get whole house power usage for last two days, the SQL code may vary some for use with SQLite. Be gentle with your queries on the SQLite database of HA, if that is your default database, as it not as robust as other database. If you are going to experiment, first shutdown HA for a bit and copy the SQLite database to a test copy and then explore this copy of the data without effecting your HA running instance :

-- get SCE whole house power
SELECT
    *
FROM
    states
WHERE (entity_id = 'sensor.home_instantaneous_demand'
    AND last_updated > now() - interval '2 days')
ORDER BY
    last_updated DESC;
-- sqlite interval example :
-- https://stackoverflow.com/questions/1101182/what-are-the-sqlite-equivalents-of-mysqls-interval-and-utc-timestamp/7547245

2 Likes
1 Like

Here’s a custom component that makes it easy to download the existing data of a sensor as a CSV:

5 Likes