-
Notifications
You must be signed in to change notification settings - Fork 1
/
nordpool_on_hours_blueprint.yaml
157 lines (150 loc) · 5.35 KB
/
nordpool_on_hours_blueprint.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
blueprint:
name: Nordpool price based on off during a period
description: This blueprints uses the nordpool integration and lets you turn on devices on the chepest hours in a row within a timespan and make automations based on that information.
domain: automation
input:
grid_area:
name: Grid area sensor
description: This is your grid area sensor
selector:
entity:
domain:
- sensor
integration: nordpool
multiple: false
start_hour:
name: Start hour
description: Set the hour to start to check for time period with low prices
default: 22
selector:
number:
min: 0.0
max: 23.0
mode: slider
step: 1.0
end_hour:
name: End hour
description: Set the hour to end the check for time period with low prices
default: 6
selector:
number:
min: 0.0
max: 23.0
mode: slider
step: 1.0
on_hours:
name: Hours on
description: Set the number of hours per day that the devices should be on
default: 3
selector:
number:
min: 0.0
max: 24.0
mode: slider
step: 1.0
turnon:
name: TurnOn
description: Actions to run to turn on devices
default: []
selector:
action: {}
turnoff:
name: TurnOff
description: Actions to run to turn off devices
default: []
selector:
action: {}
variables:
grid_area_var: !input grid_area
start_hour_var: !input start_hour
end_hour_var: !input end_hour
on_hours_var: !input on_hours
trigger:
- platform: time_pattern
minutes: 0
action:
- choose:
- conditions:
- condition: template
value_template: >-
{% set current_hour = now().hour %}
{% set offset = 0 %}
{% if current_hour == 0 %}
{% if states('input_text.prices') != '' %}
- service: input_text.set_value
data_template:
entity_id: input_text.yesterdays_prices
value: "{{ states('input_text.prices') }}"
{% endif %}
- service: input_text.set_value
data_template:
entity_id: input_text.prices
value: ''
- service: input_text.set_value
data_template:
entity_id: input_text.tomorrow_prices
value: ''
{% endif %}
{% set yesterdays_prices = [] %}
{% if current_hour > end_hour_var %}
- service: input_text.set_value
data_template:
entity_id: input_text.yesterdays_prices
value: ''
{% elif states('input_text.yesterdays_prices') != '' %}
{% set offset = 24 %}
{% set yesterdays_prices = states('input_text.yesterdays_prices') | from_json %}
{% endif %}
{% set prices = states('input_text.prices') | default(state_attr(grid_area_var, 'today') | list) | from_json %}
{% set tomorrow_prices = [] %}
{% if current_hour >= 14 and states('input_text.tomorrow_prices') == '' %}
{% set tomorrow_prices = state_attr(grid_area_var, 'tomorrow') | list %}
- service: input_text.set_value
data_template:
entity_id: input_text.tomorrow_prices
value: "{{ tomorrow_prices | to_json }}"
{% elif states('input_text.tomorrow_prices') != '' %}
{% set tomorrow_prices = states('input_text.tomorrow_prices') | from_json %}
{% endif %}
{% set is_between = false %}
{% set hours_in = 0 %}
{% if start_hour_var <= end_hour_var %}
{% set is_between = start_hour_var <= current_hour <= end_hour_var %}
{% set hours_in = current_hour - start_hour_var %}
{% else %}
{% set is_between = start_hour_var <= current_hour or current_hour <= end_hour_var %}
{% set hours_in = (24 - start_hour_var) + current_hour %}
{% endif %}
{% set seton = false %}
{% if is_between %}
{% set all_prices = yesterdays_prices + prices + tomorrow_prices %}
{% if start_hour_var <= end_hour_var %}
{% set used_prices = all_prices[start_hour_var + offset:end_hour_var + offset + 1] %}
{% else %}
{% set used_prices = all_prices[end_hour_var:start_hour_var + 1 + offset] %}
{% endif %}
{% set new_ranks = [] %}
{% set sorted_used_prices = used_prices | sort %}
{% for i in range(used_prices | length - on_hours_var) %}
{% set rank = 0 %}
{% for j in range(on_hours_var) %}
{% set rank = rank + sorted_used_prices.index(used_prices[i+j]) + 1 %}
{% endfor %}
{% set new_ranks = new_ranks + [rank] %}
{% endfor %}
{% set lowest_rank = new_ranks | sort | first %}
{% set final_ranks = [] %}
{% for r in new_ranks %}
{% if r == lowest_rank %}
{% for j in range(on_hours_var) %}
{% set final_ranks = final_ranks + [1] %}
{% endfor %}
{% else %}
{% set final_ranks = final_ranks + [2] %}
{% endif %}
{% endfor %}
{% set seton = final_ranks[hours_in] == 1 %}
{% endif %}
{{ seton }}
sequence: !input turnon
default: !input turnoff