diff --git a/readme.md b/readme.md index ca0d3ab..16b7ce6 100644 --- a/readme.md +++ b/readme.md @@ -638,6 +638,20 @@ entities: - map_y: parseFloat(y) * parseFloat(hass.states['sensor.cost'].state) ``` +This can also be used to fetch data by calling a HA service. As this is a call that requires a network connection, the function needs to be defined `async`: +```yaml + filters: + - fn: |- + async ({xs, ys, meta, hass}) => { + const weather = await hass.callService("weather", "get_forecasts", {type: "hourly"}, {entity_id:"weather.home"}, true, true) + const home = weather.response["weather.home"].forecast + return { + xs: home.map(h => new Date(h.datetime)), + ys: home.map(h => h.temperature) + } + } +``` + ##### Using vars Compute absolute humidity diff --git a/src/parse-config/parse-config.ts b/src/parse-config/parse-config.ts index 515c19e..2a09e79 100644 --- a/src/parse-config/parse-config.ts +++ b/src/parse-config/parse-config.ts @@ -161,7 +161,7 @@ class ConfigParser { value = parent[key]; if (path.match(/^entities\.\d+\.filters\.\d+$/)) { - this.evalFilter({ parent, path, key, value }); + await this.evalFilter({ parent, path, key, value }); } if (path.match(/^entities\.\d+$/)) { if (!this.fnParam.xs) { @@ -330,7 +330,7 @@ class ConfigParser { } return value; } - private evalFilter(input: { + private async evalFilter(input: { parent: object; path: string; key: string; @@ -356,7 +356,7 @@ class ConfigParser { } const filterfn = config === null ? filter() : filter(config); try { - const r = filterfn(this.fnParam); + const r = await filterfn(this.fnParam); for (const key in r) { this.fnParam[key] = r[key]; }