HI,
this was discussed many times and people are wondering why its not possible to use a template for “offset” when adding a sunrise/sunset trigger.
I would like to add this possibility to the sun component but I need some help.
/rant_on
I compared the code of other components that enable the use of templates and I tried to piece together how it could work for the sun trigger - but it seems I hit an issue at every corner and getting the feeling that the way I’m trying to achieve what I want is fundamentally wrong - this is how I feel all the time when trying to develop for home assistant… I read the code of other integrations and I think I also understand what its doing but it never seems to be applicable for what I need to do… It feels like a thick jungle and everybody else has a machete except for me…
/rant_off
I’m trying to add this in the file /compontents/sun/trigger.py
so first of I need vol to accept templates:
vol.Required(CONF_OFFSET, default=timedelta(0)): vol.Any(cv.time_period, cv.template)
is this the way to go? or the wrong strategy?
how can I make sure that the template evaluates to a cv.time_period later on?
next:
offset: Template = config.get(CONF_OFFSET)
if type(offset) is Template:
offset.hass = hass
offset = timedelta(int(offset.async_render(parse_result=False)))
this basically works but does not checking whatsoever…
originally the offset only accepts an integer or “HH:MM:SS” etc…
not it would also accept a string that is not even a template…
how can I make sure that anything thats entered that is not an int or a “timeformat” is a correct template that evaluates to a number or a “timeformat”?
also the int cast above is not what I want because ideally people could enter 00:{{template}}:00 to “build” a correct offset…
I saw the use of check_templates_warn(hass, title_tpl)
in other components but this seems to be “legacy”?
I hope you could give me some pointers in the right direction…
Edit:
I just realised… Could I be on a completely wrong path and extending this to a template would actually be futile since the templates content would only be interpreted when saving the automation and creating the trigger… So any changes to e.g. A number input would actually never be taken into account for the actual event trigger…?
But would it work to add the template functionality to the two helper functions that actually create the triggers?