HomeAssistant-CEZdistribuce icon indicating copy to clipboard operation
HomeAssistant-CEZdistribuce copied to clipboard

Add remaining time to change

Open zigul opened this issue 4 years ago • 5 comments

zigul avatar Oct 19 '20 19:10 zigul

Could be done in Lovelace:

## Zbývá : {{(as_timestamp(strptime((state_attr("sensor.hdo","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%H")|int-1}} hodin {{(as_timestamp(strptime((state_attr("sensor.hdo","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%M")|int}} minut

MattXcz avatar Oct 20 '20 20:10 MattXcz

Zbývá : {{(as_timestamp(strptime((state_attr("sensor.hdo","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%H")|int-1}} hodin {{(as_timestamp(strptime((state_attr("sensor.hdo","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%M")|int}} minut

Could you describe exactly how to use it in entities card, please? `type: entities entities:

  • entity: binary_sensor.nizky_tarif name: Nízký tarif title: HDO show_header_toggle: false state_color: true`

petosak avatar Dec 04 '20 17:12 petosak

Hi is possileble to working? I do this in configuration.yaml:

sensor:

  • platform: template sensors: hdo_cas_zmena: entity_id: binary_sensor.distribuce_t2 friendly_name: 'HDO čas změna' value_template: > {{(as_timestamp(strptime((state_attr("binary_sensor.distribuce_t2","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%H")|int-1}} hodin {{(as_timestamp(strptime((state_attr("binary_sensor.distribuce_t2","do")), ("%H:%M")) | default(0)) - as_timestamp(now()) ) | timestamp_custom("%M")|int}} minut

and still not working... Please help me

dragonerix avatar Nov 11 '21 07:11 dragonerix

good point... who has this template in correct format, please?

DastyDiesel avatar Jan 29 '24 09:01 DastyDiesel

Jen si odložím nápad k řešení problému s přidáním času do dalšího změny. Třeba se najde někdo rychlejší než já kdo to využije a doplní sensor s touto hodnotou.

def calculate_time_to_change(schedules, now):
    soonest_change = None
    for schedule in schedules:
        start_time = datetime.combine(now.date(), schedule['start'])
        end_time = datetime.combine(now.date(), schedule['end'])

        if end_time < start_time:
            end_time += timedelta(days=1)

        if start_time <= now < end_time:
            soonest_change = end_time
            break
        elif now < start_time:
            soonest_change = start_time if soonest_change is None or start_time < soonest_change else soonest_change

    return soonest_change - now if soonest_change else None
    
schedules = [
    {'start': datetime.strptime('23:00', '%H:%M').time(), 'end': datetime.strptime('6:15', '%H:%M').time()},
    # next times
]

now = datetime.now()

time_to_change = calculate_time_to_change(schedules, now)
print(f"Time until the next change: {time_to_change}")

pokornyIt avatar Mar 23 '24 22:03 pokornyIt