custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.89k stars 232 forks source link

Custome_fields - display remaning time #698

Open cricri19top opened 1 year ago

cricri19top commented 1 year ago

I want to create a button (On/OFF) for garden watering and I would like to display also the remaining time (until will be off) on the same card. I have created a timer which is triggered when button is pressed; the problem is that the remaing time is not updated (it remamins fixed 00:30:00); here is the code: custom_fields: timer: | [[[ return states['timer.timp_udare_gradina'].attributes.remaining; ]]] I have tried many variants to solved the problem, but I could not;

woodmj74 commented 1 year ago

Is this more a problem with the timer in core than the button-card? I set a timer going and checked the attributes in Developer Tools > States and the attribute remaining is not updating. Perhaps raise a defect in core?

A work around could be to create a template sensor which when the timer is active calculates the remaining time and use that? (you could probably do it as javascript but no idea how!)

{% if is_state('timer.water_garden', 'active') %}
{{ relative_time(now() - timedelta( seconds = as_timestamp(state_attr('timer.water_garden','finishes_at')) - as_timestamp(now())))  }}
{% endif %}
RomRider commented 1 year ago

Put the timer as the main entity or the card or if you can't, then set the timer custom_field to be a custom:button-card just showing the state of the timer entity and you'll have what you want :)