Using Google Home Alarm Timestamp as Trigger (Solved)

Hi gurus, I’m stepping up a notch in my automation and I’m trying to use the Google Home alarm to trigger a morning automation in HASS.io v0.88.2 running on a Pi3b. I’m having trouble getting this to trigger. I can see the timestamp in the entities and as a sensor, which is in the format “2019-03-20T18:00:00+00:00”. If the alarm is not set, the state is unavailable. I’m in time zone UTC+1, so I’m not sure that needs a correction as well. I’ve tried matching it to now() and to the time sensor (defined in sensors:) and a whole lot of other things and formatting, but none of appears to work, I’m guessing the issue is in the string/value formatting. Thanks for your help.

#### Wake Up
- alias: "Wake Up Procedure"
  trigger:   
    platform: template
#    value_template: "{{ states('sensor.time') == strptime(states('sensor.slaapkamer_hm_alarm'), '%H:%M') }}"
#    value_template: "{{ now() == states('sensor.slaapkamer_hm_alarm.timestamp') }}"
    value_template: "{{ now() == states('sensor.slaapkamer_hm_alarm') }}"
  action:
    - service: light.turn_on
      entity_id: light.slaapkamer_plafond

I use the following to trigger an automation based on my alarm settings:

trigger:
  • platform: template
    value_template: ‘{{ states.sensor.time.state == states.sensor.alarm_clock_time_long.state }}’

Is that for a Google Home alarm timestamp? If yes, what format is your sensor.time in and how do you obtain it? Like this it doesn’t work for me, if only it were that simple. Also considering that when the alarm is not set it is Unavailable, I would think that the statement in your format could possibly result in an error? States Examples

I have this setup easily in node-red. I can post the config for that if you want to

I would prefer to keep it simple and not add another layer if possible. There is a post here: Track Google Home Alarms from before the sensor was added in HASS, as you can see down the bottom @firstof9 links to the Google Home component. Unfortunately no explanation on how to use it. (maybe I should have continued the thread there)

I’ve now managed to build a sensor that returns a true or false for a match between time and Google Home timestamp. I still need to overcome the timezone difference and get the automation to work with it though. I hope there is an easier way. The section on the right returns a string and as_timestamp does not work to add an hour. The sensor is fairly clunky and looks like this:
- platform: template
sensors:
time_check5:
value_template: ‘{{ states(“sensor.time”) ==(states(“sensor.slaapkamer_hm_alarm”) | regex_replace(".+T|:…+…","")) }}’

Sorry, for some reason it doesn’t want to edit to a code block.

And I got it to work! Don’t know why I’ve been faffing around with this for 3 days, as it turns out to be fairly simple. I must have made a formatting mistake in the original timestamp template, as this works. Code is thus:

In sensors:

#Time Sensor
    - platform: time_date
      display_options:
        - 'time'
# Google Home Alarm Sensor
    - platform: template
      sensors:
        google_home_alarm_sensor:
          value_template: '{{ states("sensor.time") == as_timestamp(states("sensor.slaapkamer_hm_alarm"))|timestamp_custom ("%H:%M") }}'

in Automations:

#### Wake Up
        - alias: "Wake Up Procedure"
          trigger:   
            - platform: state
              entity_id: sensor.google_home_alarm_sensor
          action:
            - service: light.turn_on
              entity_id: light.slaapkamer_plafond_1

Next step is to make a wake up light and start Spotify.

5 Likes

Following this! What you’re trying to do is also exactly what I want. Keep up the good work :smiley:

1 Like

nice!
since these are in fact binary_sensors (either on or off) you’d best create them as such, and not as regular template sensors:

binary_sensor:
# Google Home Alarm Sensor
    - platform: template
      sensors:
        googlehome_woonkamer_alarm:
          value_template: >
            {{ states('sensor.time') == as_timestamp(states('sensor.woonkamer_speaker_alarm'))|timestamp_custom ('%H:%M') }}
          device_class: vibration

        googlehome_hall_alarm:
          value_template: >
            {{ states('sensor.time') == as_timestamp(states('sensor.hall_alarm'))|timestamp_custom ('%H:%M') }}
          device_class: vibration

        googlehome_master_bedroom_alarm:
          value_template: >
            {{ states('sensor.time') == as_timestamp(states('sensor.master_bedroom_speaker_alarm'))|timestamp_custom ('%H:%M') }}
          device_class: vibration

of course we need a device_class: alarm… not even sure if it isnt already there but it wasn’t listed so I took vibration for the occasion :wink:

any thoughts how to get rid of the ugly invalid date, when no alarm or timer is set?:

01

1 Like

Is this still possible? Seems like an update to the local API from Google has made it impossible to use the ‘googlehome’ component, which I guess is needed to track alarms.Or maybe I am misunderstanding something?

no this has been deprecated. unfortunately so.

Is there an alternative ?

No workaround i suppose…
I just want to trigger some action in my home assistant when the alarm setted in home assistant start

Google Home has been re-enabled via HACS. It works with Google Alarms as well. Which makes the above automation possible again.

@nicolaas I used your automation and added a condition check to make sure the current date and the alarm date match, so the automation doesn’t trigger when the alarm is set but at a future date.

 condition:
  - alias: "Check the alarm date and current date match"
    condition: template
    value_template: "{{ states('sensor.date') == as_timestamp(states('sensor.master_bedroom_clock_alarms'))|timestamp_custom ('%Y-%m-%d') }}"
1 Like

Hi,
I want to make automation too, based on google home alarms set.
I have 3 google homes, each google home will start a different script or automation
What i want to do is, if an alarm is turned off (so not snooze) and condition alarm is set on device x, then turn on script or automation (which will include turning on lights and music)

How do i do this?

@sentur how does your complete automation look like
@nicolaas how does yours look like now?

I’m no longer using this automation. Rather than using the alarm going off as the trigger, I now use a PIR sensor to detect motion after a certain time in a room. Then a series of actions are triggered.

From what you’ve said the below is the basis of what you’re trying to achieve with this automation. I’ve not tested it but based upon the documentations for the Googe Home integration. You’ll need to update the names of your Google Home devices. The automations is based upon there being a new automation per GH device.


- id: cf37dede-c327-499a-a532-577c84877361
  alias: 'GH Alarm trigger'
  trigger:
  - trigger: template #Trigger if the current / time date is the same as the current alarm time.
    value_template: "{{ as_timestamp(now()) == as_timestamp(states('sensor.NAME_OF_GOOGLE_HOME)) }}"
  condition: []
  action:
  - alias: "Wait until the alarm status isn't snoozed"
    wait_template: "{{ not is_state_attr('sensor.NAME_OF_GOOGLE_HOME', 'alarms[0].status', 'snoozed') }}"
  #...
  #Do you actions / sripts
  mode: single
1 Like

Cool thx!
So it will run once i turn off the alarm right?
Gonna test it thx !

Edit: tried, not working
I get Invalid config for [automation]: required key not provided @ data[‘trigger’][0][‘platform’]. Got None.

##########################
## Google Home alarms
##########################

- id: 'Google Home alarm'
  alias: 'GH Alarm trigger'
  trigger:
  - trigger: template #Trigger if the current / time date is the same as the current alarm time.
    value_template: "{{ as_timestamp(now()) == as_timestamp(states('sensor.woonkamer_mini_alarms)) }}"
  condition: []
  action:
  - alias: "Wait until the alarm status isn't snoozed"
    wait_template: "{{ not is_state_attr('sensor.woonkamer_mini_alarms', 'alarms[0].status', 'snoozed') }}"
  - entity_id: light.kastlamp
    service: light.turn_on
  #...
  #Do you actions / sripts
  mode: single  

The trigger type is incorrect. Below should now fix the error you were having.

You may wish to add a timeout to the wait command. Otherwise this automations could run indefinitely.

##########################
## Google Home alarms
##########################

- id: 'Google Home alarm'
  alias: 'GH Alarm trigger'
  trigger:
  - platform: template #Trigger if the current / time date is the same as the current alarm time.
    value_template: "{{ as_timestamp(now()) == as_timestamp(states('sensor.woonkamer_mini_alarms')) }}"
  condition: []
  action:
  - alias: "Wait until the alarm status isn't snoozed"
    wait_template: "{{ not is_state_attr('sensor.woonkamer_mini_alarms', 'alarms[0].status', 'snoozed') }}"
  - entity_id: light.kastlamp
    service: light.turn_on
  mode: single  

Still errors

  • Invalid config for [automation]: required key not provided @ data[‘trigger’][0][‘platform’]. Got None. (See /config/configuration.yaml, line 62).
  • Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char “’” at 46) for dictionary value @ data[‘value_template’]. Got None. (See /config/configuration.yaml, line 62).
##########################
## Google Home alarms
##########################

- id: 'Google Home alarm'
  alias: 'GH Alarm trigger'
  trigger:
  - platform: template #Trigger if the current / time date is the same as the current alarm time.
    value_template: "{{ as_timestamp(now()) == as_timestamp(states('sensor.woonkamer_mini_alarms)) }}"
  condition: []
  action:
  - alias: "Wait until the alarm status isn't snoozed"
    wait_template: "{{ not is_state_attr('sensor.woonkamer_mini_alarms', 'alarms[0].status', 'snoozed') }}"
  - entity_id: light.kastlamp
    service: light.turn_on
  #...
  #Do you actions / sripts
  mode: single  
  • how do i add the wait command? Add a delay? how long?
1 Like