forked from kullarkert/ha_nordpool_hours_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnordpool_cheap_prices.yaml
179 lines (175 loc) · 5.91 KB
/
nordpool_cheap_prices.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
blueprint:
name: Nordpool cheap hours
description: >
This blueprint uses the nordpool integration and lets you choose
how many hours you will define as cheap, and make automations
based on that information.
domain: automation
source_url: https://github.com/heikil/ha_nordpool_hours_blueprint/blob/main/nordpool_cheap_prices.yaml
input:
grid_area:
name: Grid area sensor
description: This is your grid area sensor
selector:
entity:
domain: sensor
integration: nordpool
multiple: false
cheap_hours:
name: Cheap hours
description: Number of cheap hours
default: 5
selector:
number:
min: 0
max: 24
mode: slider
step: 1
unit_of_measurement: "h"
look_ahead:
name: Look ahead
description: Number of hours to look ahead
default: 24
selector:
number:
min: 0
max: 24
mode: slider
step: 1.0
unit_of_measurement: "h"
start_time:
name: Start time
description: "Input which describes the first time when the action should start for cheap price."
default: "00:00:00"
selector:
time: {}
end_time:
name: End time
description: "Input which describes the last time when the action should end for cheap price."
default: "23:59:59"
selector:
time: {}
day_monday:
name: Run on Monday
description: Run the action on Monday
default: true
selector:
boolean:
day_tuesday:
name: Run on Tuesday
description: Run the action on Tuesday
default: true
selector:
boolean:
day_wednesday:
name: Run on Wednesday
description: Run the action on Wednesday
default: true
selector:
boolean:
day_thursday:
name: Run on Thursday
description: Run the action on Thursday
default: true
selector:
boolean:
day_friday:
name: Run on Friday
description: Run the action on Friday
default: true
selector:
boolean:
day_saturday:
name: Run on Saturday
description: Run the action on Saturday
default: true
selector:
boolean:
day_sunday:
name: Run on Sunday
description: Run the action on Sunday
default: true
selector:
boolean:
expensive:
name: Expensive
description: Action to run on expensive hours
default: []
selector:
action: {}
cheap:
name: Cheap
description: Action to run on cheap hours
default: []
selector:
action: {}
variables:
grid_area_var: !input grid_area
cheap_hours_var: !input cheap_hours
look_ahead_var: !input look_ahead
start_time: !input start_time
end_time: !input end_time
day_monday: !input day_monday
day_tuesday: !input day_tuesday
day_wednesday: !input day_wednesday
day_thursday: !input day_thursday
day_friday: !input day_friday
day_saturday: !input day_saturday
day_sunday: !input day_sunday
trigger:
- platform: state
entity_id: !input grid_area
action:
- choose:
- conditions:
- condition: template
value_template: >
{%- set today = now().weekday() -%}
{%- set days = [day_monday, day_tuesday, day_wednesday, day_thursday, day_friday, day_saturday, day_sunday] -%}
{%- set current_price = state_attr(grid_area_var, 'current_price') | float -%}
{%- set today_prices_raw = state_attr(grid_area_var, 'today') -%}
{%- set now_time = now().time() -%}
{%- set start = strptime(start_time, '%H:%M:%S').time() -%}
{%- set end = strptime(end_time, '%H:%M:%S').time() -%}
{%- if current_price is not none and today_prices_raw is not none and today_prices_raw | length > 0 -%}
{%- if today_prices_raw | length >= look_ahead_var -%}
{%- set filtered_prices = today_prices_raw[:look_ahead_var] -%}
{%- else -%}
{%- set filtered_prices = today_prices_raw -%}
{%- endif -%}
{%- set sorted_prices = filtered_prices | sort -%}
{%- if sorted_prices | length > cheap_hours_var -%}
{%- set target_price = sorted_prices[cheap_hours_var] -%}
{{ days[today] and start <= now_time <= end and current_price < target_price }}
{%- else -%}
false
{%- endif -%}
{%- else -%}
false
{%- endif -%}
sequence: !input cheap
- conditions:
- condition: template
value_template: >
{%- set today = now().weekday() -%}
{%- set days = [day_monday, day_tuesday, day_wednesday, day_thursday, day_friday, day_saturday, day_sunday] -%}
{%- set current_price = state_attr(grid_area_var, 'current_price') | float -%}
{%- set today_prices_raw = state_attr(grid_area_var, 'today') -%}
{%- if current_price is not none and today_prices_raw is not none and today_prices_raw | length > 0 -%}
{%- if today_prices_raw | length >= look_ahead_var -%}
{%- set filtered_prices = today_prices_raw[:look_ahead_var] -%}
{%- else -%}
{%- set filtered_prices = today_prices_raw -%}
{%- endif -%}
{%- set sorted_prices = filtered_prices | sort -%}
{%- if sorted_prices | length > cheap_hours_var -%}
{%- set target_price = sorted_prices[cheap_hours_var] -%}
{{ days[today] and current_price > target_price }}
{%- else -%}
false
{%- endif -%}
{%- else -%}
false
{%- endif -%}
sequence: !input expensive
default: []