forked from kotope/ha_nordpool_cheapest_hours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_cheapest_hours.yaml
174 lines (166 loc) · 7.52 KB
/
advanced_cheapest_hours.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
# Package to create and handle cheapest hours from nord pool integration!
# This is the SINGLE 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"
unique_id: cheapest_hours_energy
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_01 # 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
# start/stop automation
- id: 'cheapest_hours_calendar_entry'
alias: 'Cheapest hours: Calendar trigger'
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' -%}
{{ (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
# -- 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' -%}
{{ 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
# Event creation
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy' -%}
{{ as_timestamp(states(sensorId), 0) | timestamp_local }}
end_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy' -%}
{{ (as_timestamp(states(sensorId), 0) + (3600 * state_attr(sensorId, 'number_of_sequential_hours') | float)) | timestamp_local }}
summary: >
{%- set sensorId = 'sensor.cheapest_hours_energy' -%}
{{ 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
# Failsafe action
- service: calendar.create_event
data:
start_date_time: >
{%- set sensorId = 'sensor.cheapest_hours_energy' -%}
{{ 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' -%}
{{ (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' -%}
{{ 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