Skip to content

Commit 5f6af77

Browse files
committed
blueprint: added blueprint for weather forecasts
1 parent 7c0f999 commit 5f6af77

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

_docs/blueprints.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ This blueprint will provide the data source for Octopus Energy rates as provided
4747
!!! warning
4848

4949
This automation will run when any of the underlying entities update. This make take a while initially. If you want the data available immediately, then you'll need to run the automation manually.
50+
51+
### Weather
52+
53+
[Install blueprint](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fbottlecapdave.github.io%2FHomeAssistant-TargetTimeframes%2Fblueprints%2Ftarget_timeframes_weather.yaml) | [Source](./blueprints/target_timeframes_weather.yaml)
54+
55+
This blueprint will provide the data source based on the forecast of the provide weather sensor and will use the specified `Forecast attribute` as the value for the calculation of the target timeframes. The forecast will be refreshed every 30 minutes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
blueprint:
2+
name: Target Timeframes - Weather source
3+
description: Configures a target timeframe data source from Weather
4+
domain: automation
5+
author: BottlecapDave
6+
input:
7+
target_timeframe_data_source_sensor:
8+
name: Target timeframe data source sensor
9+
description: The data source sensor which represents the data source to update
10+
selector:
11+
entity:
12+
filter:
13+
- domain:
14+
- sensor
15+
integration: target_timeframes
16+
multiple: false
17+
weather_sensor:
18+
name: Weather sensor
19+
description: The weather sensor to get the forecast for.
20+
selector:
21+
entity:
22+
filter:
23+
- domain:
24+
- weather
25+
multiple: false
26+
forecast_attribute:
27+
name: Forecast attribute
28+
description: Type in the name of the desired forecast attribute to use as the value (e.g. "precipitation", "uv_index" or "temperature"). See the metadata of the result for possible options
29+
default: "condition"
30+
selector:
31+
text:
32+
variables:
33+
target_timeframe_data_source_sensor: !input target_timeframe_data_source_sensor
34+
weather_sensor: !input weather_sensor
35+
forecast_attribute: !input forecast_attribute
36+
millisecond_jitter: >
37+
{{ range(1, 1000) | random }}
38+
mode: queued
39+
max: 4
40+
triggers:
41+
- trigger: time_pattern
42+
minutes: '/30'
43+
condition: []
44+
action:
45+
# Add a bit of jitter so the API isn't hit at once
46+
- delay:
47+
milliseconds: >
48+
{{ millisecond_jitter }}
49+
- action: weather.get_forecasts
50+
target:
51+
entity_id: !input weather_sensor
52+
data:
53+
type: hourly
54+
response_variable: weather_forecast
55+
- variables:
56+
data_source_data: >
57+
{%- set forecast_items = weather_forecast[weather_sensor]["forecast"] -%}
58+
{%- set data = namespace(items=[]) -%}
59+
{%- for forecast in forecast_items -%}
60+
{%- set start = forecast["datetime"] | as_timestamp | timestamp_utc -%}
61+
{%- set end = ((start | as_datetime) + timedelta(minutes=30)) | as_timestamp | timestamp_utc -%}
62+
{%- set value = forecast[forecast_attribute] | float -%}
63+
{%- set new_item = [{ 'start': start , 'end': end, 'value': value, 'metadata': forecast }] -%}
64+
65+
{%- set data.items = data.items + new_item -%}
66+
67+
{%- set start = end -%}
68+
{%- set end = ((start | as_datetime) + timedelta(minutes=30)) | as_timestamp | timestamp_utc -%}
69+
{%- set new_item = [{ 'start': start , 'end': end, 'value': value, 'metadata': forecast }] -%}
70+
71+
{%- set data.items = data.items + new_item -%}
72+
{%- endfor -%}
73+
{{ data.items }}
74+
- action: target_timeframes.update_target_timeframe_data_source
75+
data: >
76+
{{ { 'data': data_source_data } }}
77+
target:
78+
entity_id: !input target_timeframe_data_source_sensor

0 commit comments

Comments
 (0)