DST/UTC problem

For some reason, if I feed utcnow() through a timestamp format, it comes out DST instead of UTC.

What am I doing wrong please?

Always consult the docs

1 Like

I suppose the question is… if it converts a UNIX timestamp into its string representation…and it’s being fed a UTC timestamp… then why it is converting it into a BST timestamp at all?

timestamps are timezone agnostic. When you use them, you need to account for TZ using timestamp_custom. Lastly, timestamp_custom assumes local because most people want local.

1 Like

My thinking is… if I feed a timestamp into a formatting tool, it formats the time that I gave it… not what it thinks most people want… but I think I’m coming at this from the wrong angle.

That’s why it’s an parameter, supply it with local=false. It’s an override. Way more people would be having the same complaint if it defaulted to UTC which is a TZ that is only meant for under the hood.

I’m going to drop it and use {{ utcnow().strftime(ā€˜%H:%M’) == "07:00" }} instead. I need UTC because my energy supply is ā€œEconomy 7ā€ and my meter knows nothing about DST, so many of my on/off timings for heating and water have to be UTC.

You can compare datetime aware objects. You don’t need to use UTC.

You’re having issues because you’re attempting to compare strings, not objects.

1 Like

Also, keep in mind, you can create template entities in UTC or local TZ’s and use them in time triggers. Automations will fire at those times.

Grateful for a link to those please. I haven’t been able to find any other way of doing it other than referencing the UTC time directly.

Well, it’s not straightforward. There wont’ be doc’s that cover this. Can you post the overall goal with entities (and their states) that you plan to use?

My overall goal is to trigger automations to fire at specific UTC times in order to turn heaters on and off, to match the times that my electricity meter trips over to Economy 7… so that I’m generating heat and heating my water at the cheap rate to match the meter. My cheap rate is 7 hours spread over two periods… at UTC 22:30 is the first one which ends at 00:30 and then starts again at 02:30 and ends at 07:30. By timing my hot water heating elements and main room heaters to be active during those periods, I’m using energy at 17p per kWh rather than 31p/kWh. That’s all I’m doing really.
EDIT - It’s just that my meter knows nothing about British Summer Time and is always operating at GMT, or UTC.

Just make a template sensor with device_class (show as in UI) as timestamp. Use the following templates

{% set time = '07:00' %}
{{ utcnow().date() ~ 'T' ~ time ~ ':00+00:00' }}

Then, whatever entity it ends up making, just use that entity to trigger things

- trigger: time
  at: sensor.template_sensor_that_was_created
1 Like