I am looking to create an entity (sensor) called “Hours until 2am” the value I want to be in decimal and 2 decimal points and I want this to be looking for the next occurrance of 2am so always be a positive value.
For example at 00:00 it would be 2.00
at 01:00 it would be 1.00
at 02:00 it would be 0.00
at 03:00 it would be 23.0
So always looking at the “next” 2am.
I did find some guides online but either they needed an add on which was no longer part of HA or they just produced a negative number.
In the end I did an automation (see below) but as it uses an array its only able to do 30 minute blocks unless I create a array for all 1440 minutes of each day which is not ideal.
Has anyone already done this as a sensor or automation without having to create a really long array.
alias: Set time_until_2am
trigger:
- platform: time_pattern
minutes: /30
action:
- service: input_number.set_value
target:
entity_id: input_number.hours_till_2am
data_template:
value: >
{% set current_time = now().strftime('%H:%M') %} {% set time_array = {
'00:00': 2, '00:30': 1.5, '01:00': 1, '01:30': 0.5,
'02:00': 0, '02:30': 23.5, '03:00': 23, '03:30': 22.5,
'04:00': 22, '04:30': 21.5, '05:00': 21, '05:30': 20.5,
'06:00': 20, '06:30': 19.5, '07:00': 19, '07:30': 18.5,
'08:00': 18, '08:30': 17.5, '09:00': 17, '09:30': 16.5,
'10:00': 16, '10:30': 15.5, '11:00': 15, '11:30': 14.5,
'12:00': 14, '12:30': 13.5, '13:00': 13, '13:30': 12.5,
'14:00': 12, '14:30': 11.5, '15:00': 11, '15:30': 10.5,
'16:00': 10, '16:30': 9.5, '17:00': 9, '17:30': 8.5,
'18:00': 8, '18:30': 7.5, '19:00': 7, '19:30': 6.5,
'20:00': 6, '20:30': 5.5, '21:00': 5, '21:30': 4.5,
'22:00': 4, '22:30': 3.5, '23:00': 3, '23:30': 2.5
} %} {{ time_array[current_time] if current_time in time_array else 99
}}