Time on home assistant

I’m trying to run two automations between 6am and 9pm, and the other between 9pm and 6am.

What I’m running into is conflicting times. It shows the execution at the right time but fails due to time being way off.

Executed: June 5, 2022, 18:44:25
Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '06:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '22:44:25.173410'
before:
  __type: <class 'datetime.time'>
  isoformat: '21:00:00'
result: false

Here’s the automation:

alias: EFFCOOL-DAY
description: Change Temp Day
trigger:
  - platform: numeric_state
    entity_id: climate.thermostat
    attribute: temperature
    below: '74'
condition:
  - condition: time
    after: '06:00:00'
    before: '21:00:00'
  - condition: not
    conditions:
      - condition: state
        entity_id: climate.thermostat
        attribute: hvac_mode
        state: heat
action:
  - service: climate.set_temperature
    data:
      temperature: 74
    target:
      entity_id: climate.thermostat
mode: restart

Any ideas.

No idea as you have not posted your automations.

Posted the code

Are you on a +4 or -4 timezone?
HA works in UTC, so not providing a timezone in timestamps can make it all go haywire.

The execution time mentioned on top is right between the possible range, so it looks indeed like it ran at the right time but that you are interpreting now() values in the wrong time zone.

You need to realize a time without a date (what you specified for before and after) cannot convert to UTC because there is no date. Converting those to UTC would cause weird time shifts during daylight savings changes. Those are ‘as is’ and Home assistant interprets them to be intended as local time for any given day.

The current time and execution time have a date and is internally represented as UTC, because they represent a single specific point in time. It needs to be converted to local time to display properly, and to be compared to before and after times properly.

That makes a lot of sense. I had gotten around it by offsetting the time temporarily, I’m going to look into how to change this. Thank you.

To start with, go to the template section of dev tools and type in this

{{now()}}
{{utcnow()}}

and report back the details.

Yes. It shows the time off by 4hrs. But the time looks right everywhere else and triggers automations at the right time. Just fails when it tries to compare due to the utc time.

I added a helper to make the time offset for me.

What helper did you use? Because as I said, when you have daylight savings the offset changes.
I do not know exactly what you are converting, maybe the as_timestamp and timestamp_local filter can help?

{{utcnow() | as_timestamp | timestamp_local }}

His automation is simple enough. Do you actually have the timezone configured in home assistant? settings → system → general.

You don’t need a helper if you have timezone set properly. As @ednomx didn’ report back whay he saw, its hard to know.

Yes, the time zone is right. You can see my automations shows the right time when it triggered, but it than shows 4 hrs off on the now() time.

Just give us the outputs, not your description of them

{{now()}}
{{utcnow()}}

2022-06-09 11:12:30.791350+00:00
2022-06-09 11:12:30.791350+00:00

Right, so your timezone is not set (or is set to utc)
Mine is

2022-06-10 09:08:00.009443+12:00
2022-06-09 21:08:00.009483+00:00

My tz is set to Pacific/Auckland, which is 12 hours ahead of utc, hence +12:00 in the now() output.

1 Like

By the way the place to set it is here Open your Home Assistant instance and show your general Home Assistant settings.