@@ -79,7 +79,7 @@ input_boolean:
7979template :
8080 - triggers :
8181 - platform : time_pattern
82- minutes : " /1 "
82+ minutes : " 0 "
8383 - platform : homeassistant
8484 event : start
8585
@@ -181,49 +181,59 @@ template:
181181 - name : " Electricity Today 32nd Lowest Price"
182182 unique_id : electricity_today_32nd_lowest
183183 unit_of_measurement : " c/kWh"
184- state : >
184+ availability : >
185185 {% set today = now().date().isoformat() %}
186186 {% set entries = state_attr('sensor.electricity_prices', 'data') %}
187187 {% if entries %}
188- {% set today_prices = entries
188+ {{ entries
189+ | selectattr('start', 'defined')
190+ | selectattr('start', 'string')
191+ | selectattr('start', 'search', today)
192+ | list
193+ | count >= 32 }}
194+ {% else %}
195+ {{ false }}
196+ {% endif %}
197+ state : >
198+ {% set today = now().date().isoformat() %}
199+ {% set entries = state_attr('sensor.electricity_prices', 'data') %}
200+ {% set today_prices = entries
189201 | selectattr('start', 'defined')
190202 | selectattr('start', 'string')
191203 | selectattr('start', 'search', today)
192204 | map(attribute='price')
193205 | list %}
194- {% if today_prices | count >= 32 %}
195- {% set sorted = today_prices | sort %}
196- {{ sorted[31] | round(2) }}
197- {% else %}
198- none
199- {% endif %}
200- {% else %}
201- none
202- {% endif %}
206+ {% set sorted = today_prices | sort %}
207+ {{ sorted[31] | round(2) }}
203208
204209 - sensor :
205210 - name : " Electricity Today 32nd Highest Price"
206211 unique_id : electricity_today_32nd_highest
207212 unit_of_measurement : " c/kWh"
208- state : >
209- {% set today = now().date() %}
213+ availability : >
214+ {% set today = now().date().isoformat() %}
210215 {% set entries = state_attr('sensor.electricity_prices', 'data') %}
211216 {% if entries %}
212- {% set today_prices = entries
217+ {{ entries
218+ | selectattr('start', 'defined')
219+ | selectattr('start', 'string')
220+ | selectattr('start', 'search', today)
221+ | list
222+ | count >= 32 }}
223+ {% else %}
224+ {{ false }}
225+ {% endif %}
226+ state : >
227+ {% set today = now().date().isoformat() %}
228+ {% set entries = state_attr('sensor.electricity_prices', 'data') %}
229+ {% set today_prices = entries
213230 | selectattr('start', 'defined')
214231 | selectattr('start', 'string')
215- | selectattr('start', 'search', today.isoformat() )
232+ | selectattr('start', 'search', today)
216233 | map(attribute='price')
217234 | list %}
218- {% if today_prices | count >= 32 %}
219- {% set sorted = today_prices | sort(reverse=true) %}
220- {{ sorted[31] | round(2) }}
221- {% else %}
222- none
223- {% endif %}
224- {% else %}
225- none
226- {% endif %}
235+ {% set sorted = today_prices | sort(reverse=true) %}
236+ {{ sorted[31] | round(2) }}
227237
228238 - sensor :
229239 - name : " Electricity Tomorrow Valid"
@@ -235,49 +245,59 @@ template:
235245 - name : " Electricity Tomorrow 32nd Lowest Price"
236246 unique_id : electricity_tomorrow_32nd_lowest
237247 unit_of_measurement : " c/kWh"
238- state : >
248+ availability : >
239249 {% set tomorrow = (now() + timedelta(days=1)).date().isoformat() %}
240250 {% set entries = state_attr('sensor.electricity_prices', 'data') %}
241251 {% if entries %}
242- {% set tomorrow_prices = entries
252+ {{ entries
253+ | selectattr('start', 'defined')
254+ | selectattr('start', 'string')
255+ | selectattr('start', 'search', tomorrow)
256+ | list
257+ | count >= 32 }}
258+ {% else %}
259+ {{ false }}
260+ {% endif %}
261+ state : >
262+ {% set tomorrow = (now() + timedelta(days=1)).date().isoformat() %}
263+ {% set entries = state_attr('sensor.electricity_prices', 'data') %}
264+ {% set tomorrow_prices = entries
243265 | selectattr('start', 'defined')
244266 | selectattr('start', 'string')
245267 | selectattr('start', 'search', tomorrow)
246268 | map(attribute='price')
247269 | list %}
248- {% if tomorrow_prices | count >= 32 %}
249- {% set sorted = tomorrow_prices | sort %}
250- {{ sorted[31] | round(2) }}
251- {% else %}
252- none
253- {% endif %}
254- {% else %}
255- none
256- {% endif %}
270+ {% set sorted = tomorrow_prices | sort %}
271+ {{ sorted[31] | round(2) }}
257272
258273 - sensor :
259274 - name : " Electricity Tomorrow 32nd Highest Price"
260275 unique_id : electricity_tomorrow_32nd_highest
261276 unit_of_measurement : " c/kWh"
262- state : >
277+ availability : >
263278 {% set tomorrow = (now() + timedelta(days=1)).date().isoformat() %}
264279 {% set entries = state_attr('sensor.electricity_prices', 'data') %}
265280 {% if entries %}
266- {% set tomorrow_prices = entries
281+ {{ entries
282+ | selectattr('start', 'defined')
283+ | selectattr('start', 'string')
284+ | selectattr('start', 'search', tomorrow)
285+ | list
286+ | count >= 32 }}
287+ {% else %}
288+ {{ false }}
289+ {% endif %}
290+ state : >
291+ {% set tomorrow = (now() + timedelta(days=1)).date().isoformat() %}
292+ {% set entries = state_attr('sensor.electricity_prices', 'data') %}
293+ {% set tomorrow_prices = entries
267294 | selectattr('start', 'defined')
268295 | selectattr('start', 'string')
269296 | selectattr('start', 'search', tomorrow)
270297 | map(attribute='price')
271298 | list %}
272- {% if tomorrow_prices | count >= 32 %}
273- {% set sorted = tomorrow_prices | sort(reverse=true) %}
274- {{ sorted[31] | round(2) }}
275- {% else %}
276- none
277- {% endif %}
278- {% else %}
279- none
280- {% endif %}
299+ {% set sorted = tomorrow_prices | sort(reverse=true) %}
300+ {{ sorted[31] | round(2) }}
281301
282302 - sensor :
283303 - name : " Energy Cost 15min"
@@ -333,7 +353,7 @@ template:
333353 - name : " Energy Cost Final 15min (c)"
334354 unique_id : energy_cost_final_15min_c
335355 unit_of_measurement : " c"
336- device_class : monetary
356+
337357 state_class : measurement
338358 state : >
339359 {% set energy_kwh = trigger.from_state.state | float(0) %}
@@ -386,7 +406,7 @@ template:
386406 - name : " Current 15-Minute Electricity Price"
387407 unique_id : " current_15min_electricity_price"
388408 unit_of_measurement : " c/kWh"
389- device_class : monetary
409+
390410 state_class : measurement
391411 state : >
392412 {# 1. Get the list of prices from the 'data' attribute #}
@@ -427,31 +447,30 @@ template:
427447 # Set the unit of measurement, copied from your existing sensor
428448 unit_of_measurement : " c/kWh"
429449 # Set the device class to 'monetary' so it's treated as a price
430- device_class : monetary
450+
431451 # Set the state class for long-term statistics
432452 state_class : measurement
433453 # The 'icon' is optional, but this one fits
434454 icon : " mdi:chart-line"
435455 # This is where the magic happens
436- state : >
456+ availability : >
437457 {% set tomorrow_str = (now().date() + timedelta(days=1)).isoformat() %}
438- {% set price_data = state_attr('sensor.electricity_prices', 'truedata') %}
439-
458+ {% set price_data = state_attr('sensor.electricity_prices', 'data') %}
440459 {% if price_data is not none %}
441460 {% set tomorrow_prices = price_data
442- | selectattr('start', 'starts_with', tomorrow_str)
443- | map(attribute='price')
461+ | selectattr('start', 'search', tomorrow_str)
444462 | list %}
445-
446- {% if tomorrow_prices | count > 0 %}
447- {{ tomorrow_prices | average | round(3) }}
448- {% else %}
449- {# This will happen if tomorrow's data is not yet available #}
450- unavailable
451- {% endif %}
463+ {{ tomorrow_prices | count > 0 }}
452464 {% else %}
453- {# This will happen if the 'truedata' attribute is missing #}
454- unavailable
465+ {{ false }}
455466 {% endif %}
467+ state : >
468+ {% set tomorrow_str = (now().date() + timedelta(days=1)).isoformat() %}
469+ {% set price_data = state_attr('sensor.electricity_prices', 'data') %}
470+ {% set tomorrow_prices = price_data
471+ | selectattr('start', 'search', tomorrow_str)
472+ | map(attribute='price')
473+ | list %}
474+ {{ tomorrow_prices | average | round(3) }}
456475
457476` ` `
0 commit comments