-
Hi, You wrote: Work in progress. type: custom:plotly-graph-dev
entities:
- entity: sensor.garden_temperature
offset: $fn () => "0d"
name: |
$fn({ys, vars}) => {
vars.title = ys[0];
return ys[ys.length-1]
}
- entity: sensor.keller_humidity
title: $fn({ vars }) => vars.title
hours_to_show: 2 I try to plot a graph for the current calenderday so from midnight to midnight:
for instance: So it has to be calculated, but unfortunately a $fn for time_offset doesn't work: type: custom:plotly-graph
entities:
- entity: sensor.buienradar_temperature
time_offset: $fn () => "1d"
name: |
$fn({vars}) => {
vars.my_title = "Temperature outside";
var now = new Date()
var tomorrow = new Date(now)
tomorrow.setDate(tomorrow.getDate() + 1)
tomorrow.setHours(0,0,0,0)
var HoursLeftToday = (tomorrow.getTime() - now.getTime()) / 1000 / 3600; // milliseconds -> hours
vars.my_time_offset = HoursLeftToday.toFixed(1) + "h"; // add "h" to the duration
console.log(vars.my_time_offset);
return "yesterday";
}
- entity: sensor.buienradar_temperature
name: todayy
title: $fn({ vars }) => vars.my_title
hours_to_show: 24
#time_offset: $fn({ vars }) => vars.my_time_offset
time_offset: 6.3h Why does it work for the title and not for time_offset? regards martin |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
TL;DR: The error message explains itFunctions inside entities need to run after the data is fetched, and to know what to fetch, the time_offset needs to be known beforehand. But the time_offsets hasn't ran yet because it's written below the entities. So pretty much this: SolutionLuckily, the computation you do for the entity's name doesn't depend on the fetched data, so you can extract it and put it above entities: not_a_prop: $fn ({ vars }) => {.
...
vars.my_title = "...";
vars.my_time_offset = "...";
}
time_offset: $fn({ vars }) => vars.my_time_offset # before entities as the error message indicates
entities:
- entity: ..
name: $fn ({ ... }) => ... This way, everything is computed before it is needed without requiring 1.21 gigawatts |
Beta Was this translation helpful? Give feedback.
TL;DR: The error message explains it
Functions inside entities need to run after the data is fetched, and to know what to fetch, the time_offset needs to be known beforehand. But the time_offsets hasn't ran yet because it's written below the entities.
When you write plain values instead of functions, this order is not enforced because it is known that the value won't change and there won't be any side effects (like storing in vars)
So pretty much this:
Solution
Luckily, the computation you do for the entity's name doesn't depend on the fetched data, so you can extract it and put it above entities: