Hi @Klaneous,
I got it working. I am able to choose the day of the graph now.
Note: Recorder only holds 10 days of data by default.
See https://www.home-assistant.io/integrations/recorder/#purge_keep_days
today graph
Yesterday graph
What did I do?
Added input_select helpers
input_select:
statistics_chart_range:
initial: day
options:
- day
- week
- month
- year
input_datetime:
statistics_chart_date:
has_date: true
has_time: false
Added template sensors
template:
- sensor:
- name: statistics_chart_grouping
unique_id: statistics_chart_grouping
state: |
{% set grouping={
'day':'h',
'week':'d',
'month':'d',
'year':'d'}
%}
{% set range = states('input_select.statistics_chart_range') %}
{{ grouping[range] }}
- name: statistics_chart_span
unique_id: statistics_chart_span
state: |
{% macro last_date_in_range(date_str,range) -%}
{% set date=as_timestamp(date_str)|timestamp_local()|as_datetime() -%}
{% if range=='day' -%}
{% set end_date=date -%}
{% elif range=='week' -%}
{% set end_date=date + timedelta(days=(6-date.weekday())) -%}
{% elif range=='month' -%}
{% set month=(date.month % 12) + 1 -%}
{% set year=date.year+iif(date.month==12,1,0) -%}
{% set end_date=strptime(year|string + '-' + month|string + '-01', '%Y-%m-%d')-timedelta(days=1) -%}
{% elif range=='year' -%}
{% set end_date=strptime(date.year|string + '-12-31', '%Y-%m-%d') -%}
{% endif -%}
{{ as_timestamp(end_date)|timestamp_local() -}}
{% endmacro -%}
{% set range = states('input_select.statistics_chart_range') %}
{% set date_str = states('input_datetime.statistics_chart_date') %}
{% set spans={
'day':'24',
'week':'7',
'month':(last_date_in_range (strptime(date_str, '%Y-%m-%d'),range)|as_datetime()).day,
'year':last_date_in_range (strptime(date_str, '%Y-%m-%d'),range)|as_timestamp(0)|timestamp_custom('%j',0)|int +1}
%}
{{ spans[range] }}
- name: statistics_chart_period
unique_id: statistics_chart_period
state: |
{% set periods={
'day':'hour',
'week':'day',
'month':'day',
'year':'day'}
%}
{% set range = states('input_select.statistics_chart_range') %}
{{ periods[range] }}
- sensor:
- name: statistics_chart_offset
unique_id: statistics_chart_offset
state: |
{% macro last_date_in_range(date_str,range) -%}
{% set date=as_timestamp(date_str)|timestamp_local()|as_datetime() -%}
{% if range=='day' -%}
{% set end_date = date + timedelta(days=1) -%}
{% elif range=='week' -%}
{% set end_date=date + timedelta(days=(6-date.weekday())) -%}
{% elif range=='month' -%}
{% set month=(date.month % 12) + 1 -%}
{% set year=date.year+iif(date.month==12,1,0) -%}
{% set end_date=strptime(year|string + '-' + month|string + '-01', '%Y-%m-%d')-timedelta(days=1) -%}
{% elif range=='year' -%}
{% set end_date=strptime(date.year|string + '-12-31', '%Y-%m-%d') -%}
{% endif -%}
{{ as_timestamp(end_date) -}}
{% endmacro -%}
{% set range = states('input_select.statistics_chart_range') %}
{% set date_str = states('input_datetime.statistics_chart_date') %}
{% set offset = last_date_in_range(strptime(date_str, '%Y-%m-%d'),range)|int - now()|as_timestamp(0) -%}
{% if states('sensor.statistics_chart_grouping') == 'h' %}
{% set offset=(offset/60/60)|round(method='ceil') %}
{% else %}
{% set offset=(offset/60/60/24)|round(method='ceil') + 1 %}
{% endif %}
{{ iif(offset >= 0,'+','') }}{{ offset }}{{ states('sensor.statistics_chart_grouping') }}
added script to
/config/scripts.yaml
alias: statistics_chart_date_browse
sequence:
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.statistics_chart_date
data:
datetime: >
{% set range = states('input_select.statistics_chart_range') %}
{% set date = states('input_datetime.statistics_chart_date')|as_datetime
%}
{% set direction = direction %}
{% if range == 'day' %}
{% if direction > 0 %}
{% set target = date + timedelta(days=1) %}
{% else %}
{% set target = date - timedelta(days=1) %}
{% endif %}
{% elif range == 'week' -%}
{% if direction > 0 %}
{% set target = date + timedelta(days=7) %}
{% else %}
{% set target = date - timedelta(days=7) %}
{% endif %}
{% elif range == 'month' -%}
{% if direction > 0 %}
{% set month=((date.month) % 12) + 1 %}
{% set year=date.year+iif(date.month==12,1,0) %}
{% set target=strptime(year|string + '-' + month|string + '-01', '%Y-%m-%d') -%}
{% else %}
{% set month=((date.month - 2) % 12) + 1 %}
{% set year=date.year+iif(date.month==1,-1,0) %}
{% set target=strptime(year|string + '-' + month|string + '-01', '%Y-%m-%d') -%}
{% endif %}
{% elif range=='year' -%}
{% if direction > 0 %}
{% set target=strptime((date.year|int + 1)|string + '-01-01', '%Y-%m-%d') -%}
{% else %}
{% set target=strptime((date.year|int - 1)|string + '-01-01', '%Y-%m-%d') -%}
{% endif %}
{% endif -%}
{% if direction == 0 %}
{% set target = now() %}
{% endif -%}
{{ target }}
mode: single
icon: mdi:arrow-right-bold-outline
Created a new dashboard view
- theme: Backend-selected
title: Enphase
path: enphase
icon: mdi:solar-power
type: sidebar
badges: []
cards:
- type: custom:config-template-card
variables:
offset: states['sensor.statistics_chart_offset'].state
span: states['sensor.statistics_chart_span'].state
period: states['sensor.statistics_chart_period'].state
grouping: states['sensor.statistics_chart_grouping'].state
entities:
- sensor.statistics_chart_offset
- sensor.statistics_chart_span
- sensor.statistics_chart_period
- sensor.statistics_chart_grouping
card:
type: custom:apexcharts-card
graph_span: ${span + period}
span:
offset: ${offset}
stacked: true
header:
show: false
title: Total Power
series:
- entity: sensor.power_production
type: column
name: Produced
color: '#01B4DE'
group_by:
func: avg
duration: 5min
- entity: sensor.power_consumption
transform: return x *-1 ;
type: column
name: Consumed
color: '#F37320'
group_by:
func: avg
duration: 5min
- entity: sensor.power_net
type: column
name: Imported/Exported
color: '#545456'
group_by:
func: avg
duration: 5min
yaxis:
- min: -7000
max: 7000
decimals: 0
- type: custom:mod-card
card:
type: horizontal-stack
cards:
- type: entities
entities:
- entity: input_datetime.statistics_chart_date
name: date
- type: entities
entities:
- entity: input_select.statistics_chart_range
name: period
- type: glance
show_name: false
show_state: false
entities:
- entity: script.statistics_chart_date_browse
icon: mdi:chevron-left
tap_action:
action: call-service
service: script.statistics_chart_date_browse
data:
direction: -1
- entity: script.statistics_chart_date_browse
icon: mdi:chevron-down
tap_action:
action: call-service
service: script.statistics_chart_date_browse
data:
direction: 0
- entity: script.statistics_chart_date_browse
icon: mdi:chevron-right
tap_action:
action: call-service
service: script.statistics_chart_date_browse
data:
direction: 1