finen003
(Eric)
February 4, 2023, 2:02pm
1
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
tom_l
February 4, 2023, 2:12pm
2
Why not just use a time condition?
condition: time
after: "04:59:59"
before: "09:00:01"
1 Like
123
(Taras)
February 4, 2023, 2:18pm
3
Remove the following line from the Numeric State Condition:
value_template: ""
finen003
(Eric)
February 4, 2023, 2:23pm
4
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!
finen003
(Eric)
February 4, 2023, 2:30pm
5
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!
tom_l
February 4, 2023, 2:35pm
6
If it was using UTC you have not set up your time zone.
Settings → System → General.
123
(Taras)
February 4, 2023, 2:43pm
7
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.
finen003
(Eric)
February 7, 2023, 11:41am
8
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!
finen003
(Eric)
February 9, 2023, 11:30am
9
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…
123
(Taras)
February 9, 2023, 2:00pm
10
Post the latest version of your automation.
finen003
(Eric)
February 9, 2023, 2:58pm
11
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
123
(Taras)
February 9, 2023, 3:23pm
12
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.
finen003
(Eric)
February 10, 2023, 12:35pm
13
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.
123
(Taras)
February 10, 2023, 2:03pm
14
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:
finen003
(Eric)
February 10, 2023, 2:07pm
15
Yes that is very helpful! I am not the best at YAML but always like to learn to improve! Thank you for the help
finen003
(Eric)
February 12, 2023, 6:46pm
16
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