Pie Chart Card Based on Sensor States

I have a sensor that can be in three states:

  • Nothing going on
  • Thing 1 going on
  • Thing 2 going on

I would like to have a Pie Chart telling me what percentage of the time that something was going on it was Thing 1 and what percentage it was Thing 2. I can see how to get a straight percentage for Thing 1 or Thing 2 using history_stats, but I can’t figure out how to remove “Nothing going on” from the denominator, nor can I figure out how create a pie chart.

Any suggestions/examples?

I was just looking for a similar thing and found I can use the pie/donut functionality of the GitHub - RomRider/apexcharts-card: :chart_with_upwards_trend: A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant

I have a sensor called ‘tesla_pw2_power_status’ that can be green, blue, or red - so three states as well, but it’s never nothing.

There might an easier way, but I set up three sensors based on history stats:

- platform: history_stats
  name: Solar System - Green
  entity_id: sensor.tesla_pw2_power_status
  state: 'green'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
  end: '{{ now() }}'
- platform: history_stats
  name: Solar System - Blue
  entity_id: sensor.tesla_pw2_power_status
  state: 'blue'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
  end: '{{ now() }}'
- platform: history_stats
  name: Solar System - Red
  entity_id: sensor.tesla_pw2_power_status
  state: 'red'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
  end: '{{ now() }}'

And this is the setup for the pie/donut chart:

type: custom:apexcharts-card
chart_type: donut
header:
  show: true
  title: Power Source
  show_states: false
  colorize_states: true
stacked: true
series:
  - entity: sensor.solar_system_green
    color: green
    name: Solar
    show:
      legend_value: true
      datalabels: percent
  - entity: sensor.solar_system_blue
    color: blue
    name: Battery
    show:
      legend_value: true
      datalabels: percent
  - entity: sensor.solar_system_red
    color: red
    name: Grid
    show:
      legend_value: true
      datalabels: percent

and the result:

image

It’s not 100% as I’d like it to be, but pretty close.
Like, for now, I can not find a way to add a %-sign after the number in the donut - but I can live without it.

It shows me what I already knew - it was a miserable, rainy day - not good for solar power & the self-sufficiency coefficient :frowning:

1 Like