|
| 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