What helper is best for extracting time series values from entity? (Tesla Solar Integration)

Greetings - I’m using the Tesla Solar integration to track/calculate data about my solar installation and I’ve discovered that the number of kWh imported and exported is stored as always-increasing numbers from the time of install.

I’m trying to create a Helper that will look at those entity values and then extract out a summarized value for a given date range (i.e, how much has been exported since midnight today). I’ve tried a History Stats helper but wasn’t able to get any data for and I’m now wondering how to best go about this task.

The built-in Energy distribution card is able to do what I want to do - it either has access to this data or calculates it (or both) and shows the information (highlighted in red, below) for any given day, but I’m not sure how to get at those values behind the scenes, or if I even can.

How do I calculate these derived values given the source entity data is likely stored in the HA database (I’m guessing here, seems like the obvious place) and has to be timestamped as well.

Thank you!

Those values are created/calculated from the statistics tables in the database when the web page is rendered,

So you might be able to do it with an SQL sensor if you know SQL and can understand the table schema.

Alternatively you can just create a Utility Meter helper with a daily cycle.

I’ve set my system up with two template sensors - one for ‘To Grid’

{% if states('sensor.tesla_pw2_grid_power_w') | int < 0 %}
  {{ (-1) * states('sensor.tesla_pw2_grid_power_w') | int }}
{% else %}
  0
{% endif %}

and one for ‘From Grid’:

{% if states('sensor.tesla_pw2_grid_power_w') | int > 0 %}
  {{ states('sensor.tesla_pw2_grid_power_w') | int }}
{% else %}
  0
{% endif %}

Based on these, I also have daily and monthly ‘Utility Meters’

P.S.:
I also convert sensor.tesla_pw2_site_power from kW to W first to the sensor called sensor.tesla_pw2_grid_power_w :open_mouth:

I can work my way thorugh the required SQL - in looking at the SQL Sensor documentation, I have to specify a query as part of setting up the sensor, so that’s going to take some database research on my part to finish that up. I’m running MariaDB on my HA install, so I’ll dig into that.

On the Utility Meter helper… does it collect and/or render data only at the cycle time? I set one of those up previously and I didn’t see any data being collected straight away, but was curious if I’d have to wait a day before any data would populate. If that’s the case, then it probalby won’t suit as I’m looking for the information to be updated in real time (like the energy distribution card) and if it’s always a static number that represents, best case, the prior day, then it won’t be helpful on my dashboard.

No. Every time the source sensor updates.

Are you getting the sensor data from the Tesla integration or somewhere else? In looking at the entities and sensors on my system, I’m not seeing the 'grid_power_w" sensor, so that’s why I’m asking where that sensor is sourced from.

In hindsight, maybe I shouldn’t have used the P.S. to add the info.

I do that because the kW unit is a little unwieldy - I rather see 35W than 0.035kW in my dashboards.

1 Like

Wonderful - that’s helpful for sure.

Add-on question - is it possible to use the date/period picker with a sensor card and step back through the utility meter’s historical data? Will that work or would I need to do something else to page through the data?