Parsing local time to compare with now() (for alarm clock automation)

Hi,
I really need help before I go crazy :slight_smile:
With the last App Companion update we can access at the ‘sensor.XXX_next_allarm’ of our phone.
What I would like to do would be to create an automation that checks if now() equals state_attr(‘sensor.XXX_next_alarm’, ‘Local Time’).
I suppose there is a way to extract from “Local Time” all information (day, month, hour, minute) but I can’t figure out how to do it.

What I’m seeing is this: Local Time: Wed Aug 19 10:30:00 GMT+02:00 2020 but I’m not able to parse it.

Someone can help me? :pray:
Thanks too much!

In Developer Tools > Template, try:

{{ as_timestamp(states('sensor.eva_109_next_alarm')) | timestamp_local }}

1 Like

Hi @TazUk,
thanks for you answer.
But now I can I extract, in example, hour for setting it to a input_number?

{{ as_timestamp(states('sensor.eva_109_next_alarm'))  | timestamp_custom('%H', true) | int }}
1 Like

Tanks @TazUk!
It’s works!

if I can I ask you another 3 questions (I hope the lasts):
My goal is to create an alarm clock synchronized with my phone.
However, I would like the wake up sequence to start 10 minutes before the set time.
Is it possible to subtract 10 minutes from the time?
Is it necessary to work with milliseconds?
In case how can I convert timestamps into milliseconds?
Thank you very much for your availability

I’ve done this myself in theory but not implemented yet.

Assuming that your wake up sequence will be triggered by an automation…

I have a time_date sensor configured - restart required:

sensor:
   - platform: time_date
     display_options:
        - "date_time"

I can then create a template that will be true 10 minutes (measured in seconds) before my next mobile alarm:

{{ as_timestamp(states('sensor.turk_next_alarm')) - as_timestamp(states('sensor.date_time').replace(',','')) == 600 }}

Giving the trigger for the automation:

- alias: "Alarm"
  trigger:
    - platform: template
      value_template: "{{ as_timestamp(states('sensor.eva_109_next_alarm')) - as_timestamp(states('sensor.date_time').replace(',','')) == 600 }}"
  condition: []
  action:
      <wakeup actions>

Untested as my notes are purely figured out in Developer Tools > Template, but you should be able to set an extra alarm on your phone and test with that in the Templating tool to tweak as required.

1 Like

Thank you so much @TazUk
Now I should be able to go it alone.
I’ll let you know about the outcome of my tests.
Thanks again

Hi @TazUk,
all works fine!
Thank you soo much!
I made some change for my target, so I put here my code if it can help someone else :

########################################
### AUTOMATION
########################################
automation:
  - alias: 'SvegliaAutomation'
    initial_state: true
    trigger:
      platform: template
      # if alarm clock on the phone is active autimetion will start xxx seconds before to execute wakeup sequence
      value_template: >-
          {% if ( as_timestamp(states('sensor.eva_l09_next_alarm')) ) == None %}
            False
          {% else %}
            {{ as_timestamp(states('sensor.eva_l09_next_alarm')) - as_timestamp(states('sensor.date_time').replace(',','')) == (states('input_number.wakeup_sequence_duration') | int) }}
          {% endif %}
    condition:
      - condition: state
        entity_id: input_boolean.alarm_clock_homeassistant_status
        state: 'on'
    action:
      - service: script.wake_up # put here your wakeup sequence


##################################
### SENSOR   (to put in lovelace)       ###
##################################
sensor:
  - platform: time_date
    display_options:
      - "date_time"

  - platform: template
    sensors:
      alarm_clock_time_android:
        value_template: >-
            {% if ( as_timestamp(states('sensor.eva_l09_next_alarm')) ) == None %}
              -
            {% else %}
              {{ ( as_timestamp(states('sensor.eva_l09_next_alarm')) ) | timestamp_custom('%H:%M %a %d/%m/%Y', true) }}
            {% endif %}
      alarm_clock_time_homeassistant:
        value_template: >-
            {% if ( as_timestamp(states('sensor.eva_l09_next_alarm')) ) == None %}
              -
            {% else %}
              {{ ( as_timestamp(states('sensor.eva_l09_next_alarm')) - (states('input_number.wakeup_sequence_duration') | int) ) | timestamp_custom('%H:%M %a %d/%m/%Y', true) }}
            {% endif %}


############################
### INPUT
############################
input_number:
  wakeup_sequence_duration:
    initial: 720
    min: 0
    max: 1200

input_boolean:
  alarm_clock_homeassistant_status:
    initial: on