The electricity provider who I am with in Auckland, NZ - Electric Kiwi - gives me a completely free hour of power every single day at a time of my choosing during off peak.
To maximise this, appliances I can run with a timer (not smart appliances) need to be scheduled for when we are typically in bed. Currently this hour runs between 6am and 7am for us.
So, my requirement was a sensor which would tell me how many hours until the free hour each day, so that I can set the appliance start delay accordingly.
This was surprisingly difficult working with jinja and times (for a novice, albeit a novice with good ability in vbscript/powershell). I daresay it would have taken less than 4 hours if there wasn’t beer involved, but then again it wouldn’t have been as much fun.
Anyway, this maybe simple and trivial compared to most of the amazing stuff I see here, but at the end of the day it makes my life simpler, and I couldn’t google up an precooked example.
I spent a considerable time messing with to_timestamp() before resorting to just pulling out the hours and minutes as integers.
So here it is:
sensor free_hour:
- platform: template
sensors:
time_till_free_hour:
friendly_name: "Free Hour"
unique_id: 000006
icon_template: mdi:timer-outline
value_template: >-
{% set freeHourStartTime = "06:00" %}
{% set nowHour = now().hour %}
{% set nowMins = now().minute %}
{% set freeHourStartTimeObj = strptime(freeHourStartTime, "%H:%M") %}
{% set startTimeInt = freeHourStartTimeObj.strftime("%H") | int %}
{% if nowHour >= startTimeInt%}
{% set timeUntilFreeHours = 24 + startTimeInt - nowHour -1 %}
{% else %}
{% set timeUntilFreeHours = startTimeInt - nowHour -1 %}
{% endif %}
{% set timeUntilFreeMins = 60 - nowMins %}
{% set timeUntilFreeStr = timeUntilFreeHours ~ ":" ~ timeUntilFreeMins %}
{{ strptime(timeUntilFreeStr,"%H:%M").strftime("%H:%M") }}
Could it be done with less lines? definitely but then it would be harder to understand.
Is there a better way to achieve the same thing? Would love to know!