Skip to content

Commit 6dffdaf

Browse files
committed
$fn documentation
1 parent 33012bc commit 6dffdaf

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

readme.md

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,6 @@ type: custom:plotly-graph
400400
entities:
401401
- entity: sensor.temperature_in_celsius
402402
name: living temperature in Farenheit # Overrides the entity name
403-
lambda: |- # Transforms the data
404-
(ys) => ys.map(y => (y × 9/5) + 32)
405403
unit_of_measurement: °F # Overrides the unit
406404
show_value: true # shows the last value as text
407405
texttemplate: >- # custom format for show_value
@@ -686,10 +684,87 @@ entities:
686684
map_y: y + vars.temp1[i].y
687685
```
688686

689-
### `lambda:` transforms (deprecated)
687+
### Universal functions
690688

691-
Deprecated. Use filters instead.
692-
Your old lambdas should still work for now but this API will be removed in March 2023.
689+
Javascript functions allowed everywhere in the yaml. Evaluation is top to bottom and shallow to deep (depth first traversal).
690+
691+
#### Gotchas
692+
693+
- The following entity attributes are required for fetching, so if another function needs the entity data it needs to be declared below them. `entity`,`attribute`,`offset`,`statistic`,`period`
694+
- Functions are allowed for those properties (`entity`, `attribute`, ...) but they do not receive entity data as parameters. You can still use the `hass` parameter to get the last state of an entity if you need to.
695+
- Functions cannot return functions for performance reasons. (feature request if you need this)
696+
- Defaults are not applied to the subelements returned by a function. (feature request if you need this)
697+
- You can get other values from the yaml with the `getFromConfig` parameter, but if they are functions they need to be defined before.
698+
- If a function throws an exception, the complete plot won't be rendered. Make sure to use `if` statements or [Optional Chaining (?.)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) if the values you use may be undefined.
699+
700+
#### Adding the last value to the entitiy's name
701+
702+
```yaml
703+
type: custom:plotly-graph
704+
entities:
705+
- entity: sensor.garden_temperature
706+
name: |
707+
$fn ({ ys,meta }) =>
708+
meta.friendly_name + " " + ys[ys.length - 1]
709+
```
710+
711+
#### Sharing data across functions
712+
713+
```yaml
714+
type: custom:plotly-graph
715+
entities:
716+
- entity: sensor.garden_temperature
717+
718+
# the fn attribute has no meaning, it is just a placeholder to put a function there. It can be any name not used by plotly
719+
fn: |
720+
$fn ({ ys, vars }) =>
721+
vars.title = ys[ys.length - 1]
722+
title: $fn ({ vars }) => vars.title
723+
```
724+
725+
#### Histograms
726+
727+
```yaml
728+
type: custom:plotly-graph
729+
entities:
730+
- entity: sensor.openweathermap_temperature
731+
x: $fn ({ys,vars}) => ys
732+
type: histogram
733+
title: Temperature Histogram last 10 days
734+
hours_to_show: 240
735+
no_default_layout: true
736+
layout:
737+
margin:
738+
t: 0
739+
l: 50
740+
b: 40
741+
height: 285
742+
xaxis:
743+
autorange: true
744+
```
745+
746+
#### custom hover text
747+
748+
```yaml
749+
type: custom:plotly-graph
750+
title: hovertemplate
751+
entities:
752+
- entity: climate.living
753+
attribute: current_temperature
754+
customdata: |
755+
$fn ({states}) =>
756+
states.map( ({state, attributes}) =>({
757+
...attributes,
758+
state
759+
})
760+
)
761+
hovertemplate: |-
762+
<br> <b>Mode:</b> %{customdata.state}<br>
763+
<b>Target:</b>%{y}</br>
764+
<b>Current:</b>%{customdata.current_temperature}
765+
<extra></extra>
766+
hours_to_show: 24
767+
```
693768

694769
## Default trace & axis styling
695770

0 commit comments

Comments
 (0)