Display / output Home Assistant data as a table - Reporting
Charts are great, but a simple table view of historical data would be desirable for certain evaluations. Unfortunately, Home Assistant has very little to offer out of the box. Only certain cards can access the history data, but then without any possibility to edit, filter or customize the data. As a solution, I am working on a HACS integration in parallel to this guide: now available: Tables & evaluations in Home Assistant.
It is only possible to output any SQL queries from the database as a table in a Lovelace card in a roundabout way, which is the aim of this article.
The example presented here includes data from five different entities that are read from the database, stored in an entity and output via a markdown card in Lovelace. In the markdown card, certain columns can be calculated based on the values of other columns: e.g. the column: "Delta" is calculated from "Lead" and "Return". For this example, I have used the custom integration sql_json . However, certain database values can also be displayed in a Lovelace card without the HACS integration sql_json:
☑SQL integration: JSON
Using the existing SQL integration, it is possible to save the result for certain SQL queries in a separate sensor, see: Home Assistant SQL integration. Stored in JSON format, the data can be output relatively easily as a table via a markdown card. Here is a concrete example for the daily PV yield: SQL integration sensor, see:
Below is the SQL query used for a continuous entity (energy meter):
{% set data = state_attr('sensor.daily_pv_yield','json') | from_json %}
<table><tr>
<th>Datum</th>
<th>Ertrag</th>
</tr>
{% for i in range(0,data | count)%}
<tr>
<td align=center>
{{data[i].localdate }}
</td>
<td align=center>
{{ '{:.2f}'.format(data[i].state | round(2) ) | replace(".",",") }} kWh
</td>
</tr>
{% endfor %}
Caution with large amounts of data: Queries run every 30 seconds
By default, SQL queries are executed every 30 seconds, which could slow down Home Assistant very quickly, so large queries should be excluded from the standard polling:
The query can be executed on demand via a script or regularly via automation:
Combine certain entities in a query
With this variant, the data from several sensors can be combined; here is the corresponding query for the example presented at the beginning of this article:
When using the HACS integration Card-Mod, the layout of the table can be customized, as shown in the examples, different backgrounds for even and odd rows.
The Flex Table integration is a little easier to set up, but less flexible:
For simple tables ok, but at the latest when linking or calculating certain columns, the Flex Table Card reaches its limits, so I would prefer the Markdown Card to the Flex-table Card.
ⓘ Call up data directly, Plotly-Graph-Table
The table view of Plotly-Graph should not go unmentioned at this point. Originally designed for displaying charts, Plotly-Graph can also display historical data as a table: Directly and without using a query in advance.
type: custom:plotly-graph
hours_to_show: 999999
entities:
- entity: sensor.heating_water_energy
type: table
period:
"0": day
statistic: sum
filters:
- delta
- store_var: water
- entity: sensor.flowmeter_sum
type: table
period:
"0": day
statistic: sum
columnwidth:
- 16
- 10
- 10
header:
values:
- Date
- Flowmeter
- Water
fill:
color: $ex css_vars["primary-color"]
font:
color: $ex css_vars["primary-text-color"]
filters:
- delta
cells:
values:
- |
$ex xs.toReversed().map(x=>
new Intl.DateTimeFormat('de-DE', {
day: '2-digit',
month: '2-digit',
year: '2-digit'
}).format(x))
- $ex ys.toReversed().map(x=> x.toFixed(2))
- $ex vars.water.ys.map(x=> x.toFixed(2))
align:
- center
- left
fill:
color: $ex css_vars["card-background-color"]
grid_options:
columns: full
rows: 12
The table view in Plotly: works, but is definitely not its core competence. Plotly is neither visually convincing nor easy to use (scrolling behavior). The data cannot be exported or copied to the clipboard. For these reasons, I cannot recommend Plotly for displaying tables.
Conclusion
Home Assistant has its strengths in displaying current values and has great data visualization capabilities. However, certain other solutions offer more for storing and displaying historical data. Not only when visualizing charts, but also when displaying tables, Home Assistant could take an example from other visualization solutions such as Grafana. As a solution, I am working on a HACS integration in parallel to this guide: now available: Tables & evaluations in Home Assistant.
I do not want to recreate the calculation outside the History Table Card. I want to use the values that the card has already calculated for the Battery benefit column.
I noticed that calc variables can be used by other calculations within the table, but I cannot find a way to use the calculated result of a column as a sum/total over the selected period.
Is there currently a way to do this?
For example, is there a syntax or configuration such as:
* sum: Batterybenefit
* stat_type: sum for a calculated column
* or another way to expose/sum the result of a calc variable?
If this is not currently possible, is there another recommended way to obtain the sum of a calculated column over the selected period?
The important point is that I want to sum the result of the existing calculation, not duplicate the underlying calculation in another sensor.
Questions / Comments