HomeAssistant-CEZdistribuce
HomeAssistant-CEZdistribuce copied to clipboard
Add remaining time to change
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
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`
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
good point... who has this template in correct format, please?
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}")