I have the following data that I would like to plot
{ start: '01:00', end: '05:00', capacity: 30 },
{ start: '05:00', end: '09:00', capacity: 30 },
{ start: '09:00', end: '17:00', capacity: 50 },
{ start: '17:00', end: '20:00', capacity: 70 },
{ start: '20:00', end: '22:00', capacity: 60 },
{ start: '22:00', end: '01:00', capacity: 45 }
The values are all provided by sensors i.e.
sensor.ss_prog1_time
sensor.ss_prog2_time
number.sunsynk_prog1_capacity
i.e sensor.ss_prog1_time = 01:00
and sensor.ss_prog2_time = 05:00
etc
I have the following code but it seems overly complicated and very resource intensive. Is there perhaps a more efficient way to achieve the same thing
EDIT: Updated with reduced data points
EDIT2: Minimise function calls
type: custom:plotly-graph
hours_to_show: 2.2d
time_offset: 27h
refresh_interval: 10m
entities:
- entity: sensor.sunsynk_battery_soc
line:
color: red
shape: spline
width: 3
show_value: true
texttemplate: '%{y}'
- entity: number.sunsynk_prog1_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.6)
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog1_time'].state;
const endTimeString = hass.states['sensor.ss_prog2_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog1_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
- entity: number.sunsynk_prog2_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.9)
show_value: false
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog2_time'].state;
const endTimeString = hass.states['sensor.ss_prog3_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog2_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
- entity: number.sunsynk_prog3_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.7)
show_value: false
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog3_time'].state;
const endTimeString = hass.states['sensor.ss_prog4_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog3_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
- entity: number.sunsynk_prog4_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.9)
show_value: false
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog4_time'].state;
const endTimeString = hass.states['sensor.ss_prog5_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog4_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
- entity: number.sunsynk_prog5_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.7)
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog5_time'].state;
const endTimeString = hass.states['sensor.ss_prog6_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog5_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
- entity: number.sunsynk_prog6_capacity
line:
color: rgb(30,144,255)
width: 1
fill: tozeroy
fillcolor: rgba(30,144,255, 0.9)
filters:
- fn: |
({ys, xs, hass}) => {
const startTimeString = hass.states['sensor.ss_prog6_time'].state;
const endTimeString = hass.states['sensor.ss_prog1_time'].state;
const capacity = parseFloat(hass.states['number.sunsynk_prog6_capacity'].state);
const now = new Date();
const [startHours, startMinutes] = startTimeString.split(':').map(Number);
const [endHours, endMinutes] = endTimeString.split(':').map(Number);
const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), startHours, startMinutes);
let endTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHours, endMinutes);
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}
return { xs: [startTime, endTime], ys: [capacity, capacity] };
}
layout:
showlegend: false
yaxis:
nticks: 20
range:
- 0
- 105
fixedrange: true
xaxis:
nticks: 9
config:
scrollZoom: false