Sensor with different values in an apexchart pie -- MelCloud -- OperationMode example

Hello
I was searching the way to display a pie chart from an uniq Sensor with different values. Apexchart pie uses different entities to construct the pie

This is an example with MelCloud Operation mode

The idea is to provide a pie chart with Operation Mode day distribution
OperationMode

Important Notes:
Restart Hassio completely after sensors integration. It’s mandatory to restart always than a new integration is included.
Accuracy of pie chart depends on update interval of Rest Api integration. It’s not recomended to set a very low interval becuase it will collapse the Cloud integration

The configuration is the following one

configuration.yaml

Configuration for REST Api

sensor REST API Melcloud

sensor:
  - platform: rest
    name: heat_pump_api
    resource: !secret mcloud_resource
    method: GET
    headers: 
        X-MitsContextKey: !secret mcloud_api_key
    value_template: '{{value_json[0].Structure.Devices[0]["Device"]["LastTimeStamp"]}}'
    icon: mdi:heat-wave
    scan_interval: 300
    json_attributes_path: !secret mcloud_att_path
    json_attributes:
       ....
      - OperationMode
      ....
      - LastTimeStamp

Configuration of Sensors

Operation Mode Sensor with names (inside configuration.yaml):

 - platform: template
    sensors:
      heat_pump_operationmode:
        friendly_name: "Heat Pump Operation Mode"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',0) %}
            Off
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',1) %}
            Heating ACS
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',2) %}
            Heating Radiators
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',3) %}
            Cooling
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',4) %}  
            Standby   
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',5) %}
            Defrost
          {% elif is_state_attr('sensor.heat_pump_api','OperationMode',6) %}  
            Legionella    
          {% else %}
            Unknown
          {% endif %}

Sensor for each of the Operation mode. These sensors will be used to check that operation mode is correctly working ( configure inside plaftorm template in configuration.yaml file)
“Off mode” has been change from 0 to 10, to be sure it correctly detected

      heat_pump_opm_off:
        friendly_name: "Heat Pump OPM OFF"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',0) %}  
            {{ 10.0 }}
          {% else %}
            {{ none }}
          {% endif %}
        device_class: aqi
     
      heat_pump_opm_acs:
        friendly_name: "Heat Pump OPM ACS"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',1) %}  
            {{ 1.0 }}
          {% else %}
            {{ none }}
          {% endif %} 
        device_class: aqi
      
      heat_pump_opm_heatr:
        friendly_name: "Heat Pump OPM HeatR"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',2) %}  
             {{ 2.0 }}
          {% else %}
             {{ none }}
          {% endif %}
        device_class: aqi
        
      heat_pump_opm_cool:
        friendly_name: "Heat Pump OPM Cool"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',3) %}  
            {{ 3.0 }} 
          {% else %}
             {{ none }}
          {% endif %}
        device_class: aqi
        
      heat_pump_opm_standby:
        friendly_name: "Heat Pump OPM Standby"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',4) %}  
            {{ 4.0 }} 
          {% else %}
             {{ none }}
          {% endif %}
        device_class: aqi
        
      heat_pump_opm_defrost:
        friendly_name: "Heat Pump OPM Defrost"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',5) %}  
            {{ 5.0 }}
          {% else %}
            {{ none }}
          {% endif %} 
        device_class: aqi

      heat_pump_opm_legionella:
        friendly_name: "Heat Pump OPM Legg"
        value_template: >-
          {% if is_state_attr('sensor.heat_pump_api','OperationMode',6) %}  
            {{ 6.0 }} 
          {% else %}
             {{ none }}
          {% endif %}
        device_class: aqi

Configuration of history_stat sensors for each operation Mode. These will control the time used by each sensor ( configure at the same level of platform template )

  - platform: history_stats
    name: heat_pump_opm_heatr_time
    entity_id: sensor.heat_pump_operationmode
    state: "Heating Radiators" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    #duration:
    #  hours: 24
    
  - platform: history_stats
    name: heat_pump_opm_off_time
    entity_id: sensor.heat_pump_operationmode
    state: "Off" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: heat_pump_opm_acs_time
    entity_id: sensor.heat_pump_operationmode
    state: "Heating ACS" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: heat_pump_opm_cooling_time
    entity_id: sensor.heat_pump_operationmode
    state: "Cooling" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: heat_pump_opm_stand_time
    entity_id: sensor.heat_pump_operationmode
    state: "Standby" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: heat_pump_opm_defrost_time
    entity_id: sensor.heat_pump_operationmode
    state: "Defrost" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: heat_pump_opm_legion_time
    entity_id: sensor.heat_pump_operationmode
    state: "Legionella" 
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

Display of sensors. Add new history graph card in frontend Hass

type: history-graph
hours_to_show: 24
entities:
  - entity: sensor.heat_pump_opm_acs
    name: acs
  - entity: sensor.heat_pump_opm_defrost
    name: defrost
  - entity: sensor.heat_pump_opm_heatr
    name: heat
  - entity: sensor.heat_pump_opm_off
    name: 'off'
  - entity: sensor.heat_pump_opm_standby
    name: sby
  - entity: sensor.heat_pump_opm_legionella
    name: leg
  - entity: sensor.heat_pump_opm_cool
    name: Cool
  - entity: sensor.heat_pump_operationmode
    name: M
  - entity: sensor.heat_pump_opm_heatr_time
    name: Heat
  - entity: sensor.heat_pump_opm_off_time
    name: 'off'
  - entity: sensor.heat_pump_opm_acs_time
    name: acs
  - entity: sensor.heat_pump_opm_cooling_time
    name: cool
  - entity: sensor.heat_pump_opm_defrost_time
    name: defr
  - entity: sensor.heat_pump_opm_stand_time
    name: sby
  - entity: sensor.heat_pump_opm_legion_time
    name: Legg
update_interval: 60m

image
image

Configuration of Pie Chart

Add new apexchart Card for previous day

type: custom:apexcharts-card
chart_type: pie
graph_span: 24h
span:
  start: day
header:
  show: true
  floating: false
  title: Ecodan Usage Yesterday
  show_states: false
  colorize_states: true
show:
  last_updated: true
all_series_config:
  offset: '-1d'
  group_by:
    func: max
    duration: 24h
  show:
    datalabels: percent
series:
  - entity: sensor.heat_pump_opm_off_time
    name: 'off'
    color: '#949391'
  - entity: sensor.heat_pump_opm_acs_time
    name: ACS
    color: '#f2ac1f'
  - entity: sensor.heat_pump_opm_defrost_time
    name: Defr
    color: '#cfcf15'
  - entity: sensor.heat_pump_opm_heatr_time
    name: Heat
    color: '#b31212'
  - entity: sensor.heat_pump_opm_stand_time
    name: Sby
    color: cyan
  - entity: sensor.heat_pump_opm_legion_time
    name: Legg
    color: '#abcf37'
  - entity: sensor.heat_pump_opm_cooling_time
    name: Cool
    color: '#56a5ec'
update_interval: 60m

image

Same configuration for actual day ( only difference remove offset: ‘-1d’ from previous)

type: custom:apexcharts-card
chart_type: pie
graph_span: 24h
span:
  start: day
header:
  show: true
  floating: false
  title: Ecodan Usage
  show_states: false
  colorize_states: true
show:
  last_updated: true
all_series_config:
  group_by:
    func: max
    duration: 24h
  show:
    datalabels: percent
series:
  - entity: sensor.heat_pump_opm_off_time
    name: 'off'
    color: '#949391'
  - entity: sensor.heat_pump_opm_acs_time
    name: ACS
    color: '#f2ac1f'
  - entity: sensor.heat_pump_opm_defrost_time
    name: Defr
    color: '#cfcf15'
  - entity: sensor.heat_pump_opm_heatr_time
    name: Heat
    color: '#b31212'
  - entity: sensor.heat_pump_opm_stand_time
    name: Sby
    color: cyan
  - entity: sensor.heat_pump_opm_legion_time
    name: Legg
    color: '#abcf37'
  - entity: sensor.heat_pump_opm_cooling_time
    name: Cool
    color: '#56a5ec'
update_interval: 60m

image

The result could be check with MelCloudApp.
Take into account that values are not exactly the same. Perhaps the update ratio from MelCloud App is different or have another implementation.
image

Then a message with the summary could be sent to telegram. I only could send in text mode

alias: Send Ecodan Distribution
trigger:
  - platform: time
    at:
      - "23:59:50"
condition: []
action:
  - service: notify.NOTIFIER_NAME
    data:
      title: Ecodan Energy Distribution
      message: >-
        {% set total = (states("sensor.heat_pump_opm_heatr_time")|float +
              states("sensor.heat_pump_opm_off_time")|float  +
              states("sensor.heat_pump_opm_acs_time")|float  +
              states("sensor.heat_pump_opm_cooling_time")|float  +
              states("sensor.heat_pump_opm_defrost_time")|float  +
              states("sensor.heat_pump_opm_stand_time")|float  +
              states("sensor.heat_pump_opm_legion_time")|float)|round(2) %} 
          📉<b>Total:</b> {{ total }}h📈
         
        ⛔️ Off:     {{ states("sensor.heat_pump_opm_off_time") }}h / {{
        ((states("sensor.heat_pump_opm_off_time")|float / total ) *
        "100.0"|float)| round(2) }} %

        🚰 ACS:    {{ states("sensor.heat_pump_opm_acs_time") }}h / {{
        ((states("sensor.heat_pump_opm_acs_time")|float / total ) *
        "100.0"|float)| round(2) }} %

        ♨️ Heat:   {{ states("sensor.heat_pump_opm_heatr_time") }}h / {{
        ((states("sensor.heat_pump_opm_heatr_time")|float / total ) *
        "100.0"|float)| round(2) }} %

        ❄️ Cool:   {{ states("sensor.heat_pump_opm_cooling_time") }}h / {{
        ((states("sensor.heat_pump_opm_cooling_time")|float / total ) *
        "100.0"|float)| round(2) }} %

        ☢️ Defrost: {{ states("sensor.heat_pump_opm_defrost_time") }}h / {{
        ((states("sensor.heat_pump_opm_defrost_time")|float / total ) *
        "100.0"|float)| round(2) }} % 

        ♻️ Standby: {{ states("sensor.heat_pump_opm_stand_time") }}h / {{
        ((states("sensor.heat_pump_opm_stand_time")|float / total ) *
        "100.0"|float)| round(2) }} %

        ⚕️ Legionella: {{ states("sensor.heat_pump_opm_legion_time") }}h / {{
        ((states("sensor.heat_pump_opm_legion_time")|float / total ) *
        "100.0"|float)| round(2) }} % 
      data:
        parse_mode: html
        disable_notification: true

I hope this helps or could be used as base for other sensors.
Best regards