-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathadvanced_cheapest_hours_multiple.yaml
279 lines (265 loc) · 12.8 KB
/
advanced_cheapest_hours_multiple.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Package to create and handle cheapest hours from nord pool integration!
# This is the MULTIPLE entry version.
# For more details check out the blog post at https://www.creatingsmarthome.com/?p=1620
template:
- sensor:
# The actual cheapest hour sensor. If multiple entries are required, copy and set unique_id and configure attributes.
- name: "Cheapest hours energy (Water Boiler)"
unique_id: cheapest_hours_energy_water_boiler
device_class: timestamp
state: >
{%- set sensor = (this.attributes.get('sensor', 'sensor.nordpool_kwh_fi_eur_3_10_01') | string) -%}
{%- set numberOfSequentialHours = (this.attributes.get('number_of_sequential_hours',1) | int) -%}
{%- set lastHour = (this.attributes.get('last_hour',23) | int) -%}
{%- set firstHour = (this.attributes.get('first_hour', 0) | int) -%}
{%- set startingToday = (this.attributes.get('starting_today', false) | bool) -%}
{%- if state_attr(sensor, 'tomorrow_valid') == true -%}
{%- set arr = state_attr(sensor, 'today') + state_attr(sensor, 'tomorrow') -%}
{%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00"), cheapestPrice=999.00) -%}
{%- if startingToday == true -%}
{%- set ns.starting = firstHour -%}
{%- else -%}
{%- set ns.starting = firstHour + 24 -%}
{%- endif -%}
{%- set ns.ending = lastHour + 24 + 1 -%}
{%- for i in range(ns.starting + numberOfSequentialHours, ns.ending+1) -%}
{%- set ns.counter = 0.0 -%}
{%- for j in range(i-numberOfSequentialHours, i) -%}
{%- set ns.counter = ns.counter + arr[j] -%}
{%- endfor -%}
{%- set ns.list = ns.list + [ns.counter] -%}
{%- if ns.counter < ns.cheapestPrice -%}
{%- set ns.cheapestPrice = ns.counter -%}
{%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (i - numberOfSequentialHours)) -%}
{%- endif -%}
{%- endfor -%}
{{ ns.cheapestHour }}
{%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
{%- endif -%}
attributes:
# CHANGE-ME: Set your personal configurations in here
number_of_sequential_hours: 5 # Amount of sequantial cheapest hours in search
first_hour: 22 # Search starting hour
last_hour: 08 # Search ending hour
starting_today: true # Is the first_hour today (true / false). If false, first_hour needs to be before last_hour.
sensor: sensor.nordpool_kwh_fi_eur_3_10_024 # Nord pool sensor id. Check it ouf from your integrations page!
fail_safe_starting: '00:00' # If nordpool fetch fails, starting time to make the calendar entry
- sensor:
# The actual cheapest hour sensor. If multiple entries are required, copy and set unique_id and configure attributes.
- name: "Cheapest hours energy (Car Charging)"
unique_id: cheapest_hours_energy_car_charging
device_class: timestamp
state: >
{%- set sensor = (this.attributes.get('sensor', 'sensor.nordpool_kwh_fi_eur_3_10_01') | string) -%}
{%- set numberOfSequentialHours = (this.attributes.get('number_of_sequential_hours',1) | int) -%}
{%- set lastHour = (this.attributes.get('last_hour',23) | int) -%}
{%- set firstHour = (this.attributes.get('first_hour', 0) | int) -%}
{%- set startingToday = (this.attributes.get('starting_today', false) | bool) -%}
{%- if state_attr(sensor, 'tomorrow_valid') == true -%}
{%- set arr = state_attr(sensor, 'today') + state_attr(sensor, 'tomorrow') -%}
{%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00"), cheapestPrice=999.00) -%}
{%- if startingToday == true -%}
{%- set ns.starting = firstHour -%}
{%- else -%}
{%- set ns.starting = firstHour + 24 -%}
{%- endif -%}
{%- set ns.ending = lastHour + 24 + 1 -%}
{%- for i in range(ns.starting + numberOfSequentialHours, ns.ending+1) -%}
{%- set ns.counter = 0.0 -%}
{%- for j in range(i-numberOfSequentialHours, i) -%}
{%- set ns.counter = ns.counter + arr[j] -%}
{%- endfor -%}
{%- set ns.list = ns.list + [ns.counter] -%}
{%- if ns.counter < ns.cheapestPrice -%}
{%- set ns.cheapestPrice = ns.counter -%}
{%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (i - numberOfSequentialHours)) -%}
{%- endif -%}
{%- endfor -%}
{{ ns.cheapestHour }}
{%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
{%- endif -%}
attributes:
# CHANGE-ME: Set your personal configurations in here
number_of_sequential_hours: 7 # Amount of sequantial cheapest hours in search
first_hour: 00 # Search starting hour
last_hour: 23 # Search ending hour
starting_today: false # Is the first_hour today (true / false). If false, first_hour needs to be before last_hour.
sensor: sensor.nordpool_kwh_fi_eur_3_10_024 # Nord pool sensor id. Check it ouf from your integrations page!
fail_safe_starting: '00:00' # If nordpool fetch fails, starting time to make the calendar entry
automation:
# Automation to trigger when calendar event hits the cheapest hour mark
# Water boiler start/stop automation
- id: 'cheapest_hours_calendar_entry_water_boiler'
alias: 'Cheapest hours: Calendar trigger (water boiler)'
description: ''
trigger:
- platform: calendar
event: start
entity_id: calendar.electricity
- platform: calendar
event: end
entity_id: calendar.electricity
condition:
- condition: template
value_template: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ (state_attr(sensorId, 'friendly_name') | string) == trigger.calendar_event.summary }}
action:
- if:
- condition: template
value_template: '{{ trigger.event == ''start'' }}'
then:
# CHANGE-ME: Actions to do when cheapest hours starts
- service: light.turn_on
entity_id: light.office_work
else:
# CHANGE-ME: Actions to do when cheapest hours ends
- service: light.turn_off
entity_id: light.office_work
mode: single
# Car charger start/stop automation
- id: 'cheapest_hours_calendar_entry_car_charging'
alias: 'Cheapest hours: Calendar trigger (car charging)'
description: ''
trigger:
- platform: calendar
event: start
entity_id: calendar.electricity
- platform: calendar
event: end
entity_id: calendar.electricity
condition:
- condition: template
value_template: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ (state_attr(sensorId, 'friendly_name') | string) == trigger.calendar_event.summary }}
action:
- if:
- condition: template
value_template: '{{ trigger.event == ''start'' }}'
then:
# CHANGE-ME: Actions to do when cheapest hours starts
- service: light.turn_on
entity_id: light.office_ceiling
else:
# CHANGE-ME: Actions to do when cheapest hours ends
- service: light.turn_off
entity_id: light.office_ceiling
mode: single
# -- GLOBAL CONFIGURATIONS BELOW, NO NEED TO CHANGE UNLESS CREATING MULTIPLE SEQUENCES --
# Create calendar event
- id: 'cheapest_hours_set_sequence'
alias: 'Cheapest hours: Set next cheapest sequence'
description: 'Checks tomorrow energy prices every hour and create calendar entry when available AND events not yet created'
trigger:
- platform: time_pattern
hours: /1
condition:
- condition: template
# from which sensor we should try the tomorrow price validation
value_template: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ state_attr((state_attr(sensorId, 'sensor') | string), 'tomorrow_valid') == true }}
- condition: state
entity_id: input_boolean.cheapest_hours_set
state: 'off'
action:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.cheapest_hours_set
# Water boiler event creation
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ as_timestamp(states(sensorId), 0) | timestamp_local }}
end_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ (as_timestamp(states(sensorId), 0) + (3600 * state_attr(sensorId, 'number_of_sequential_hours') | float)) | timestamp_local }}
summary: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ state_attr(sensorId, 'friendly_name') | string }}
target:
entity_id: calendar.electricity
# Car charging event creation
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ as_timestamp(states(sensorId), 0) | timestamp_local }}
end_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ (as_timestamp(states(sensorId), 0) + (3600 * state_attr(sensorId, 'number_of_sequential_hours') | float)) | timestamp_local }}
summary: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ state_attr(sensorId, 'friendly_name') | string }}
target:
entity_id: calendar.electricity
# If multiple cheapest hours sequences are required, copy 'calendar.create_event' block from aboce and change the 'sensorId' variabes values to match corresponding new cheapest hours templated sensor
mode: single
# Failsafe
- id: 'cheapest_hours_failsafe'
alias: 'Cheapest hours: Failsafe'
description: 'Failsafe: Set cheapest hours from fail_safe value to amount of hours'
trigger:
- platform: time
at: '23:15'
condition:
- condition: state
entity_id: input_boolean.cheapest_hours_set
state: 'off'
action:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.cheapest_hours_set
# Water boiler failsafe
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ as_timestamp(today_at(state_attr(sensorId, 'fail_safe_starting')) + timedelta( hours = 24 ), 0) | timestamp_local }}
end_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ (as_timestamp(today_at(state_attr(sensorId, 'fail_safe_starting')) + timedelta( hours = 24 ), 0) + (3600 * state_attr(sensorId, 'number_of_sequential_hours') | float)) | timestamp_local }}
summary: >
{%- set sensorId = 'sensor.cheapest_hours_energy_water_boiler' -%}
{{ state_attr(sensorId, 'friendly_name') | string }}
target:
entity_id: calendar.electricity
# Car charging failsafe
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ as_timestamp(today_at(state_attr(sensorId, 'fail_safe_starting')) + timedelta( hours = 24 ), 0) | timestamp_local }}
end_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ (as_timestamp(today_at(state_attr(sensorId, 'fail_safe_starting')) + timedelta( hours = 24 ), 0) + (3600 * state_attr(sensorId, 'number_of_sequential_hours') | float)) | timestamp_local }}
summary: >
{%- set sensorId = 'sensor.cheapest_hours_energy_car_charging' -%}
{{ state_attr(sensorId, 'friendly_name') | string }}
target:
entity_id: calendar.electricity
# If multiple cheapest hours sequences are required, make a copy of 'calendar.create_event' block above and set the 'sensorId' values to match the new one
mode: single
# input_boolean reset
- id: 'cheapest_hours_clear_set_flag'
alias: 'Cheapest hours: Reset the set helper for the next day'
description: 'Clears cheapes hours helper boolean when the day changes.'
trigger:
- platform: time
at: '01:15:00'
condition: []
action:
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.cheapest_hours_set
mode: single
# We need a helper to know if the calendar mark(s) has already been set!
input_boolean:
cheapest_hours_set:
name: Cheapest hours set for the next day
icon: mdi:clock