Hey,
I’'m stuck and need some pointers. The documentation is confusing (me) and I’ve tried to find the answers in different threads. No luck. I think I’m quite confused on the different time objects and formats.
My goal is to to have an automation that turn on a wall-plug during the three cheapest consecutive hours between 9pm and 5am (charging an electric bicycle).
The electricity price is made available in the afternoon, therefore I have a time based trigger that intends to create a sensor containing the date/time to turn on the wall-plug.
Following this, I intended to have an automation that triggers on this date/time and perform its task (turn on the wall-plug)
My configuration currently looks like this:
- trigger:
- platform: time
at: '17:30:00'
sensor:
- name: "Nordpool EO4 Cheapest Hours During the Night"
unique_id: nordpool_eo4_chapest_night_hours_time
state: >
{% set nordpoolSensor = "sensor.nordpool_kwh_se4_sek_3_095_0" %}
{% set priceData = namespace(numbers=[]) %}
{% for i in state_attr(nordpoolSensor,'raw_today') %}
{% set priceData.numbers = priceData.numbers + [i.value] %}
{% endfor %}{% for i in state_attr(nordpoolSensor,'raw_tomorrow') %}
{% set priceData.numbers = priceData.numbers + [i.value] %}
{% endfor %}
{% set three_consecutive_hours = namespace(numbers=[]) %}
{% for n in range(21,29) %}
{% set three_hour_sum = (priceData.numbers[n]+priceData.numbers[n+1]+priceData.numbers[n+2])|round(2) %}
{% set three_consecutive_hours.numbers = three_consecutive_hours.numbers + [three_hour_sum] %}
{% endfor %}
{% set min_three_hour = min(three_consecutive_hours.numbers) %}
{% set least_expensive_hours = three_consecutive_hours.numbers.index(min_three_hour) + 21 %}
{% if least_expensive_hours > 23 %}
{% set least_expensive_hours = least_expensive_hours - 24 %}
{% set t = (now().replace(hour=least_expensive_hours, minute=0, second=0).timestamp() + 3600*24) %}
{% else %}
{% set t = (now().replace(hour=least_expensive_hours, minute=0, second=0).timestamp()) | timestamp_local %}
{% endif %}
{{t}}
This will give me the following output (which with the current input is correct, the three cheapest hours starts tomorrow at 1am local time):
Resultattyp: string
2022-09-23T01:00:00.950538+02:00
I fail to use this in my automation and seek guidance how to format the output to make it usable in an automation.
…or… any other tricks to achive my goal is obviously appreciated.
Thanks /Marcus