Copying date time value is (incorrectly) taking timezones into consideration

Good afternoon,

The context of what I’m trying to do
I’m creating a series of controls that define when certain things are supposed to happen throughout the day (such as turning on lights, etc.). I’m creating two sets though - the set for today upon which action will be taken, and a default set. The idea is that the default settings are what should happen on a normal day, but you can make a change just for today by editing the today set. Then every day, the defaults will be copied to the today values, resetting all settings to what they ought to be. So, if I need to not turn lights on in a room which is usually empty because someone is staying in it tonight, I can change that setting for today, and then tomorrow, it will copy the value from the default set and we’re back to normal.

The code and the problem
I have it pretty well figured out and set up. However, I’ve come across a peculiar issue. When I copy the time values from the default data to today’s data, it inserts an 8 hour offset. This is my GMT offset (I live on the west coast of the US - Pacific Time (-8:00:00).

Here is the code that I have in my automation:

service: input_datetime.set_datetime
data:
  timestamp: "{{ state_attr('input_datetime.time_to_wake_ellie_default', 'timestamp') }}"
target:
  entity_id: input_datetime.time_to_wake_ellie

If the time on input_datetime.time_to_wake_ellie_default is 07:00:00, and I execute that code, input_datetime.time_to_wake_ellie gets set to 23:00:00.

I can adjust it by adding 28800 seconds (8 hours) to the timestamp (as below), but not only is this a bit hacky, I fear it may mess things up, as when Daylight Saving Time kicks in, we’ll only be 7 hours behind GMT, not 8.

service: input_datetime.set_datetime
data:
  timestamp: "{{ state_attr('input_datetime.time_to_wake_ellie_default', 'timestamp') + 28800 | int }}"
target:
  entity_id: input_datetime.time_to_wake_ellie

So, why is it factoring in timezones when I’m all I’m asking it to do is set one value to the other? Can anyone shed any light on how to configure this properly?

If the Input Datetime is configured to store date and time, use this:

service: input_datetime.set_datetime
data:
  datetime: "{{ states('input_datetime.time_to_wake_ellie_default') }}"
target:
  entity_id: input_datetime.time_to_wake_ellie

If the Input Datetime is configured to store time only, use this:

service: input_datetime.set_datetime
data:
  time: "{{ states('input_datetime.time_to_wake_ellie_default') }}"
target:
  entity_id: input_datetime.time_to_wake_ellie

The value of timestamp represents time in UTC. You can prove it to yourself by copy-pasting the following into the Template Editor and observing the result:

{{ state_attr('input_datetime.time_to_wake_ellie_default', 'timestamp') | as_datetime }}

Sure enough - you nailed it.

Thanks for takin the time to explain it so clearly.

1 Like