Using template in - delay in automation

I want to use a value derived fro a sensor to make a timed delay.
The template in a delay works without the sensor just using a numeric value but the sensor results in a formatting error in the config editor.
The example shows the template used in a message and both result in the same output.
ie ‘00:8:00’ and the second produces the correct 8 minute delay.
Clearly templates work in the delay action but I must be formatting it wrong somewhere.
Any ideas what is wrong and why it will not work in the delay but work in the message?

      - service: notify.clicksend
          message: "'00:{{ states('sensor.dark_sky_daytime_high_temperature_0d') | int }}:00'"
# This results in an error #####
      - delay: '00:{{ states('sensor.dark_sky_daytime_high_temperature_0d') | int }}:00'
# #######################
      - service: notify.clicksend
          message: "'00:{{ 8.1 | int }}:00'"
# This works ############
      - delay: '00:{{ 8.1 | int }}:00' 
# #####################

I’m on a phone so can’t try this myself.
Try either encapsulating in quotes or using the string concatenator.
Ie : -

- delay: '00:' {{ states('sensor.dark_sky_daytime_high_temperature_0d') | int }}':00'
- delay: '00:' ~ '{{ states('sensor.dark_sky_daytime_high_temperature_0d') | int }}' ~ ':00'

Thanks for quick response.
1st one says bad indentation of a sequence entry at line 31, column 22:
- delay: ‘00:’ {{ states('sensor.dark_sky_dayti …

2nd one says
bad indentation of a sequence entry at line 32, column 8:
- delay: ‘00:’ ~ '{{ states('sen …

try this

- delay: "{{ '00:{:02}:00'.format(states('sensor.dark_sky_daytime_high_temperature_0d') | int) }}"

That is exactly it!!! Works a dream. Now I just need to understand it!
Thanks AhmadK.
It is for a night heat storage system. I want it to trigger for different amount of hours depending on the next days temperature. In addition we have found if it is going to be sunny despite the predicted temperature it needs to be on less time so I need to try and work out a formula based on % cloud cover and temperature. But you have given me the start as I could not get the delay part to work but now it does. Happy days. :slight_smile:

I’m glad it works for you.

Generally you need to a) know some python to understand what format function does and read how templates work in HA (specifically about Jinja2).

But in this case it’s fairly easy - you need to enclose your template in double quotes and inside you generate a string for your delay.
‘00:{}:00’.format(your_str) would do this as the only thing you need to configure is hours.
However, to do it properly you need your hours to have exactly 2 digits and to do so you add some formatting options inside that {}, which will be replaced by an format’s argument (you can write {1} as well). This formatting calls for a number so we add | int Jinja filter.
And one of the ways to safely get a state of your object is to call states('your_object') (it returns unknown if there is no such thing. You can do also states. your_object.state, just remember - it’s always a string.
Does it make more sense now?

Interesting. My heating system (not storage though) is HA-controlled but I’m still thinking about how to take external temperatures into account, for example - so far it just reacts to the temperatures across the house.
I’d suggest you to create a new topic describing your ideas/formule and ideally, some code you want to make work/improve.

Well its time to go and get the python book from the loft! Bedside reading tonight.

As the system is on a night tariff I wait until just past midnight, look up dark sky’s prediction of temperature, switch on the night storage via a sonoff and then set the delay according to temperature ranges. Now with your help I can make this into one automation with a variable delay and play about by multiplying the delay by the % cloud cover. As our apartment is south facing the sun has a huge effect on the inside temperature. I’m sure it will be trial and error but better than we have now.
Thanks again and by brushing up again on my python I’m sure I will think of lots of other uses!
I have just put thermometers in every room linked to HA so this could also be taken into account.
Happy days.

Yeah, collecting some data and analysing it is the way to go.
There are some interesting integrations available in here (like statistics).