How to use current hour as condition?

I’ve been trying for quite some time to make a good morning routine triggered by me taking my phone off of a wireless charger when I wake up. A part of the routine checks if the time is between 5:00 and 9:00 to not get triggered if someone goes the bathroom in the middle of the night.

What I have tried so far:

I have 3 sensors in configuration.yaml:

- sensor:
      - name: "Current Time"
        state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%H:%M:%S') }}"
        icon: "mdi:calendar-clock"

  - sensor:
      - name: "Current Hour"
        unique_id: current_hour
        state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%H') | int }}"
        icon: "mdi:calendar-clock"

  - sensor:
      - name: "Current Minute"
        unique_id: current_minute
        state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%M') | int }}"
        icon: "mdi:calendar-clock"

This creates a number that is always the current hour so my thought is if 4 < current_hour < 10 then allow the trigger. I put this in an If Then Else to send a notification if true or not for me to validate this part.

Unfortunately when I use current_hour as a numeric state in the automation and the hour is 5 I get told that it’s not true even though I can check the value and it definitely is 5.

So what am I missing to get this to work?

My numeric state is as follows:

if:
  - condition: numeric_state
    entity_id: sensor.current_hour
    above: 4
    below: 10
    value_template: ""
then:
  - service: notify.mobile_app_mabel_s_phone
    data:
      message: Time is valid
  - service: automation.trigger
    data:
      skip_condition: true
    target:
      entity_id: automation.turn_receiver_on_to_shield
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.goodmorning_occured
else:
  - service: notify.mobile_app_mabel_s_phone
    data:
      message: Time not valid

Why not just use a time condition?

condition: time
after: "04:59:59"
before: "09:00:01"
1 Like

Remove the following line from the Numeric State Condition:

    value_template: ""

I can try that. I though I had tried that at one point but it was using UTC. Let me try again to make sure!

I will try both suggestions. Unfortunately it usually works in my test cases but when 5:30 comes around, it never fires. So I’ll give it a go tomorrow morning!

If it was using UTC you have not set up your time zone.

Settings → System → General.

FWIW, the three Template Sensors (which simply report the current time in your local timezone) can be reduced to this:

  - sensor:
      - name: "Current Time"
        state: "{{ now().timestamp() | timestamp_custom('%H:%M:%S') }}"
        icon: "mdi:calendar-clock"

      - name: "Current Hour"
        unique_id: current_hour
        state: "{{ now().hour }}"
        icon: "mdi:calendar-clock"

      - name: "Current Minute"
        unique_id: current_minute
        state: "{{ now().minute }}"
        icon: "mdi:calendar-clock"

If they’re not reporting the correct current time in your local timezone then, like tom_l explained, your local timezone isn’t set correctly in Home Assistant.

Sorry it’s taken awhile to give a true test. I used the time comparison exactly as Tom posted between 4:59 and 9:00. This mostly works… in testing it works after the time is 6:00 but not at times between 5 and 6.

My time zone is set to New York:

My temple shows:

{{ now() }}
{{ now().astimezone() }}
{{ utcnow().astimezone() }}
{{ now().now() }}
{{ now().today() }}
{{ now().utcnow() }}
{{ utcnow() }}
{{ now().tzinfo }}
{{ now().astimezone().tzinfo }}
{{ utcnow().tzinfo }}
{{ now().hour }}

Which results in:

2023-02-07 06:37:20.221779-05:00
2023-02-07 06:37:20.221862-05:00
2023-02-07 06:37:20.222048-05:00
2023-02-07 06:37:20.222256
2023-02-07 06:37:20.222393
2023-02-07 11:37:20.222517
2023-02-07 11:37:20.222562+00:00
America/New_York
EST
UTC
6

So my time appears to be correct but it’s not turning on for that 1 hour stretch for some reason…

I can of course try to change my statement to be after 4 instead of after 5 but that doesn’t seem like the best way to solve the issue.

Thank you both for suggestions so far!

Even when changing the condition to 4:00 to 9:00, it will trigger if the time is exactly 6:00, 6:10, etc but not at 5:59 or anything before 6:00. This is what I can’t figure out… times are reporting correctly as shown above too…

Post the latest version of your automation.

Here is the entire automation currently:

alias: Good morning Routine
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.mabel_s_phone_is_charging
    to: "off"
  - platform: state
    entity_id:
      - binary_sensor.fernando_is_charging
    to: "off"
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.goodmorning_occured
            state: "off"
          - condition: state
            entity_id: input_boolean.awaymode
            state: "off"
    then:
      - service: notify.mobile_app_mabel_s_phone
        data:
          message: Testing time
      - if:
          - condition: time
            after: "04:00:00"
            before: "10:00:00"
        then:
          - service: notify.mobile_app_mabel_s_phone
            data:
              message: Time is valid
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.turn_on_receiver_to_tv
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.goodmorning_occured
          - if:
              - condition: or
                conditions:
                  - condition: numeric_state
                    entity_id: sensor.current_weekday_number
                    above: 4
                  - condition: numeric_state
                    entity_id: sensor.current_weekday_number
                    below: 1
            then:
              - service: light.turn_on
                data: {}
                target:
                  entity_id: light.espresso_machine_light
        else:
          - service: notify.mobile_app_mabel_s_phone
            data:
              message: Time not valid
    else: []
    enabled: true
mode: single
alias: Good morning Routine
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.mabel_s_phone_is_charging
      - binary_sensor.fernando_is_charging
    from: "on"
    to: "off"
condition:
  - condition: state
    entity_id: input_boolean.goodmorning_occured
    state: "off"
  - condition: state
    entity_id: input_boolean.awaymode
    state: "off"
  - condition: time
    after: "04:00:00"
    before: "10:00:00"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.turn_on_receiver_to_tv
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.goodmorning_occured
  - if: 
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sensor.current_weekday_number
            above: 4
          - condition: numeric_state
            entity_id: sensor.current_weekday_number
            below: 1
    then:
      - service: light.turn_on
        target:
          entity_id: light.espresso_machine_light
mode: single

Use the automation’s trace to analyze its behavior.

Well this morning at 5:50 AM it worked using your method. I am not sure really why there is a difference as we just listed our conditions in a different way but will take it if it continues to work. I will try again tomorrow to verify this has been corrected.

If you are interested, all of this:

  - if: 
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sensor.current_weekday_number
            above: 4
          - condition: numeric_state
            entity_id: sensor.current_weekday_number
            below: 1
    then:

is the same as this Template Condition in shorthand notation:

  - if: "{{ states('sensor.current_weekday_number') | int(0) in [0, 5, 6] }}"
    then:

or this:

  - if: "{{ now().weekday() in [0, 5, 6] }}"
    then:

Yes that is very helpful! I am not the best at YAML but always like to learn to improve! Thank you for the help

So the actual method appears to now work correctly … when it fires. It appears that taking a phone off of the charger doesn’t always trigger the phone charger disconnect event. I’ll play around with this a bit though