Statistics Graph Card - Unable to select state for stat_type

Statistics Graph Card - Unable to select state for stat_type.

- name: Daily Return on Investment
  unique_id: daily_return_on_investment
  unit_of_measurement: AUD
  state_class: measurement
  state: |
    {% set total = 0 | float %}
    {% set answer = states('sensor.daily_solar_energy_to_load_roi') %}
    {% if is_number(answer) %}{% set total = total + answer | float %}{% endif %}
    {% set answer = states('sensor.daily_battery_return_on_investment') %}
    {% if is_number(answer) %}{% set total = total + answer | float %}{% endif %}
    {% set answer = states('sensor.daily_mains_export_feed_in_return') %}
    {% if is_number(answer) %}{% set total = total + answer | float %}{% endif %}
    {{ total }}
  icon: mdi:currency-usd
- name: Daily Cost
  unique_id: daily_cost
  unit_of_measurement: AUD
  state_class: measurement
  state: |
    {% set total = 0 | float %}
    {% set answer = states('sensor.daily_mains_energy_import_cost') %}
    {% if is_number(answer) %}{% set total = answer | float %}{% endif %}
    {% set answer = states('sensor.daily_electricity_supply_charge') %}
    {% if is_number(answer) %}{% set total = total + answer | float %}{% endif %}
    {{ total }}
  icon: mdi:currency-usd
- name: Daily Bill Cost
  unique_id: daily_bill_cost
  unit_of_measurement: AUD
  state_class: measurement
  state: |
    {% set total = 0 | float %}
    {% set answer = states('sensor.daily_cost') %}
    {% if is_number(answer) %}{% set total = answer | float %}{% endif %}
    {% set answer = states('sensor.daily_mains_export_feed_in_return') %}
    {% if is_number(answer) %}{% set total = total - answer | float %}{% endif %}
    {{ total }}
  icon: mdi:currency-usd

which I am trying to graph with the cost which should show the state at midnight and the daily totals reset to zero, caused by the reset of the source sensors.
The graph is:

chart_type: bar
period: day
type: statistics-graph
entities:
  - sensor.daily_cost
  - sensor.daily_bill_cost
  - sensor.daily_return_on_investment
days_to_show: 7
title: Daily Costs
hide_legend: false
stat_types:
  - max

I am unable to set the stat_type to state. Why?

What does that mean? You don’t have the option? Are you getting an error?

How long did you wait after creating those sensors? It aggregates hourly, so there will only be data after an hour.

Are you excluding these sensors from your recorder?

thanks for the interest.

Those sensors are 7 days old, so not an issue of age. On the UI, the state, change and sum options will not allow me to tick (select.)

If I use the code editor and force the stat_types to state, the graph is completely blanked, except for the header and the right axis title of AUD.

Craig

Could you go to the built-in History dashboard and plot the data for a week? You should see something like this, where the lighter period shows it’s going over to long-term stats.

It is available for total and total_increasing. You have measurement.

1 Like

WTF (Why?)

Because of a design.
You have a “measurement” sensors - for them you can use “mean”, “min”, “max”.
(I am not saying anything about “use total/total_increasing for you sensors”, this is another question)

I don’t know or recall the full reasoning. Perhaps look up the developer docs on state classes.

But, if I think about this: with total or total increasing, you could say the state must be the last value for a given hour. Remember stats have hourly granularity. So, for an energy sensor this would make sense.

Now imagine a power sensor with a state class of measurement. Power can change all the time, up and down. Which value in the hour will represent the state? Only mean, max and min really make sense (or any kind of percentile).

Also, if LTS for a constantly changing value contains a “state” - then it will occupy MORE space than a history data.