Using a variable for time in an automation

I am still trying to get my head around how things work in Home Assistant and I can use a little help on some very basic things.

I like to keep the level of interaction with HA to the absolute minimum, but unfortunately me and my housemates do not wake-up every day at the same time. I looked at how people created an alarm clock alarm clock as a package and many similar posts, but all I want is a variable alarm_time with a card to adjust it so that I can use this alarm_time in my automations. In automations.yaml it most likely looks something like this:

- id: '0123456789'
  alias: Master Bedroom - Wakeup Gently
  description: ''
  trigger:
  - platform: time
    at: `alarm_time`

I tried adding in the format of alarm time the seconds as well so that it has a value something like: 07:30:00, but that does not work.

As there are many things that need to happen in at least my house so depending on the alarm time I want to be able to also trigger automations at different times depending on the alarm time which looks like:

- id: '9876543210'
  alias: Master Bedroom - Wakeup Gently
  description: ''
  trigger:
  - platform: time
    at: `alarm_time` - 00:30:00 (but then formatted in the right way)

I can of course make a total spagetti off all these automations creating an complete list of actions in that one automation, but then it easily becomes totally unmanagable and intransparent what is happening in your house.

So the solution to combine it with a boolean sensor as done in the alarm clock as a package to create a state change but does not sound very attractive to me or is there a smarter way than creating a whole list of actions in that one automation?

A question that goes along with this one, where to find how to format the alarm_time in this case in the right way? or if it is impossible to use a variable for alarm_time where is that written?

Hey SpoofEx and welcome to the community.

I integrated an alarm clock as well, was some work with a template that subtracts seconds from the time sensor, however you donā€™t need to do that anymore as of v0.115.
Take a look at the blog post.

Go to Configuration - Helpers, click on ā€œAdd new Helperā€ -> ā€œDate and/or timeā€.
Choose what fits your use case best:

  • Time: Triggers each day on this time (e.g. every day, 06 AM)
  • Date: Triggers on this specific date (e.g. at 05.12.2020)
  • Date and time: Triggers on specific date on specific time (e.g. at 06 AM on 05.12.2020)

The name of those helpers is something like input_datetime.alarm or similar, depending on the name youā€™ve chosen. You can see the entity_id in ā€œConfiguraion - Helpersā€

For an alarm clock, Iā€™d go with a Time one only. Have that as your automation trigger.
Now, the alarm will trigger every day. To avoid that on weekends for example, you can add a time condition to your automation, so that the automation only fires on weekdays.
Additionally, Iā€™d add another ā€œToggleā€ helper ā€œAlarm Statusā€, and have that as a condition as well (state of input_boolean.alarm_status is on as condition). Then, from the frontend, you can easily turn on and off your alarm clock.

1 Like

Thanks fedot!

That is exactly what I was looking for!

With a little bit of help life can be so much easier

Hello,
Iā€™m also busy getting myself an alarm clock.

I have already done most of the revival, although I would still like to add things. (Iā€™m new to, HA)
I implemented in my Lovelace a card with the time of my alarm clock which activates the automation

I would like to be able to take this time (ok, I have my variable wake_time_1) and take a time from it in order to create a variable wake_time_1_1 to activate an automation that turns on my phone charger. So when I wake up, my phone is charged and good for the day.

The problem is that I donā€™t know how to deduct a time from my variable wake_time_1 and create my variable wake_time_1_1.

Best case scenario, Iā€™d like to retrieve my battery capacity and deduce the ideal time before I wake up so that the battery is at 90% by the time I wake up. Well, itā€™s more complex, so activating one hour before I wake up already suits me.
Especially since I already have an automation that stops the charger at 90% of my battery.

Thank you for the helps.

In order to help you, we need to know what type of entity wake_time_1 is. The specific way to achieve your goal is different for an Input number helper vs. Input datetime (date & time) vs. Input datetime (time-only) helper.

Yes, my entity is time-only.

Okay, there are two main ways to cover your base question.

Option 1, use a template trigger to trigger an automation 1 hour before the time represented by a time-only Input datetime as follows:

trigger:
  - platform: template
    value_template: >
      {{ now() >= today_at( states('input_datetime.wake_time_1')) - timedelta(hours=1) }}

Option 2, if you plan on using this in multiple automations it would be better to create a Template Sensor.

template:
  - sensor:
      - name: "Wake Time 1 Pre"
        device_class: timestamp 
        state: "{{ today_at( states('input_datetime.wake_time_1')) - timedelta(hours=1) }}"

This will create a entitiy called sensor.wake_time_1_pre that you can use in a Time trigger.

1 Like

If the entity is an input_datetime it can be referenced directly in a Time Trigger.

 trigger:
   - platform: time
     at: input_datetime.whatever

From the documentation:

Thanks for the reply. I use the option 2.
My code is correct ?
The automation is not working.

Thanks for the help.

alias: reveil_chargeur
description: ""
trigger:
  - platform: template
    value_template: |-
      template:
        - sensor:
            - name: "Wake Time 1 Pre"
              device_class: timestamp 
              state: "{{ today_at( states('input_datetime.wake_time_1')) - timedelta(hours=1) }}"
condition:
  - type: is_battery_level
    condition: device
    device_id: 43c28e2d1a124f351bd1d13b7cac1f8e
    entity_id: sensor.oneplus_6t_justin_battery_level
    domain: sensor
    below: 92
action:
  - type: turn_on
    device_id: 8470b4b93565bffd076f97bfc7214237
    entity_id: switch.chargeur_outlet
    domain: switch
mode: single

Not, quiteā€¦ Take a look at the documentation that I linked in the post. Template sensors need to ve set uo in your configuration file, not within the automation.

I add only this in my configuration?

template:
  - sensor:
      - name: "Wake Time 1 Pre"
        device_class: timestamp 
        state: "{{ today_at( states('input_datetime.wake_time_1')) - timedelta(hours=1) }}"

I add only this in my configuration?

alias: reveil_chargeur
description: ""
trigger:
  - platform: time
    at: sensor.wake_time_1_pre
condition:
  - type: is_battery_level
    condition: device
    device_id: 43c28e2d1a124f351bd1d13b7cac1f8e
    entity_id: sensor.oneplus_6t_justin_battery_level
    domain: sensor
    below: 92
action:
  - type: turn_on
    device_id: 8470b4b93565bffd076f97bfc7214237
    entity_id: switch.chargeur_outlet
    domain: switch
mode: single

I test this.

Not working.
One thing must be beyond me.

When I use the graphics mode, it does not offer me the sensor.wake_time_1_pre

Yes, the above can be added to the configuration.yaml file. If this is your first Template entity, you should be able to just copy/paste it into the file. Make sure template: is all the way to the left. Also, make sure to restart Home Assistant to load your changes.

After your have loaded the changes, confirm that the sensor has been created by searching for it in the Developer Tools > States page or the Settings > Devices & Services > Entities page.





No, if you are using the graphical Automation editor you do not need to add your automation to the configuration.




That is a bug... just type the complete entity ID into the field.

Yes, I meant automation.

I actually had an extra template in my configuration.

Thank you so much for de helps.

I have a little problem.
The activation of the automation is not done.
When I tested, it worked. That is, when I changed the wake-up time.
But at the ā€œofficial time - 1hā€ of my alarm clock. It does not work.

I find it weird that he tells me that Wake Time 1 Pre is September 6th. Since itā€™s already September 6th at 10:10 a.m.
But what disturbs me the most is that he said 4 hours ago
Capture dā€™eĢcran 2022-09-06 aĢ€ 10.08.07

Whereas, when I modify and encode a closer time, it tells me in 20 minutes. (It was 10:10 a.m. at my house)

Capture dā€™eĢcran 2022-09-06 aĢ€ 10.10.21

And also I wanted to test with 20h, but it didnā€™t update. It rested on September 6 10:30.

The sensor will always be in the past if you look at it past that time for the current day. You can modify the template to jump ahead in time to tomorrows time, but you have to build that into your template.

template:
  - sensor:
      - name: "Wake Time 1 Pre"
        device_class: timestamp 
        state: >
          {% set alarm = today_at(states('input_datetime.wake_time_1')) %}
          {% set offset = 23 if now() >= alarm else -1 %}
          {{ alarm + timedelta(hours=offset) }}

OK, thanks.
I test that.