Use of time delta in a simple template

Hi,

I am just a few days into HA on a new install (2020.12 then) and I need some pointers on how to make a template that takes 2 hours from a time (input_datetime) entity and sets a different entity to the result of the computation.

This is what I thought would work:

service: input_datetime.set_datetime
data:
  time: >
    {{ input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon - timedelta( hours =
    1, minutes = 0 ) }}
entity_id: input_datetime.gomd_nar_ska_motorvarmaren_faktiskt_starta

The error message I get says:

Error executing script. Unexpected error for call_service at pos 1: Error rendering data template:
UndefinedError: ‘input_datetime’ is undefined. While executing automation automation.ny_automation

I appreciate all the help I could get on this (probably something silly is wrong,.,)

This subtracts 2 hours (7200 seconds) from the input_datetime’s timestamp then converts it back to a time string and (displays only the last 8 characters)

service: input_datetime.set_datetime
data:
  entity_id: input_datetime.gomd_nar_ska_motorvarmaren_faktiskt_starta
  time: >
    {{ ((state_attr('input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon', 'timestamp') - 7200) | timestamp_utc)[-8:] }}

As for the template you created:

{{ input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon - timedelta( hours = 1, minutes = 0 ) }}

You can’t refer to input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon directly like that in a template. You can use this:

states.input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon.state

or this:

states('input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon')

However, it would still fail to work because both of them return the input_datetime’s state which is a time string in this format:

12:45:00

That’s a string which is simply text. You cannot subtract from a string using timedelta because it only works with a datetime object.

1 Like

Thanks!

Is datetime objects always assumed to be set in UTC? Will that be a problem if I assign the new computed time to a new helper datetime object and use it to define a time trigger?

That is, if my local time is UTC+1 and I set

input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon = 06:30

and then compute

input_datetime.gomd_nar_ska_motorvarmaren_faktiskt_starta = 06:00

using the template above, will it then trigger the event at the correct time, or be one hour too late?
(that is 06:00 in UTC which is then 07:00 in local time, if daylight savings is the same, which it may not be)?

Just to make sure that I know how this works.

When you assign 06:00 to the input_datetime, that’s the time that it will store and the time that will be used by a Time Trigger.

For example, if the input_datetime is to be used as an alarm time for 06:00, simply set it to 06:00 (and don’t concern yourself with time conversions).

Paste the template I provided into the Template Editor and experiment with it. Change the value of input_datetime.nar_ska_bilen_vara_uppvarmd_imorgon and you will see the template produces a time that’s always 2 hours earlier than the input_datetime’s value.

service: input_datetime.set_datetime
data:
date: >
{{ strptime(states(‘input_datetime.ultima_desparasitacion’), ‘%Y-%m-%d’) +
timedelta(days=90) }}
target:
entity_id: input_datetime.desparasitar_perros

Could not parse date for dictionary value @ data[‘date’]. Got None me da este error y no se el porque en la automatizacion

quiero hacer una automatizacion que añada la fecha de un input a otro input distinto