Using Google Home Alarm Timestamp as Trigger (Solved)

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

Not sure why you’re getting that first error as I don’t recieve it on the adapted automation below. You were missing a ' in the value_template: which has causing the second error. The timeout is added after the wait_template. You need to decide how long you want to wait for the alarm to stop snoozing.

Also from what I’ve read, the Google Home Integration uses polling to gather the alarms status which defaults to 180 seconds. So there maybe a delay in to the action > wait_template of up to 3 minutes before the actions are then run.

If you’re continuning to have errors, I think it may be better to break out your code into a seperate thread where more people will see it. As this thread’s original question has been marked as solved.

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

- id: 'Google Home alarm' #NOTE: Use a unique ID here i.e. 2314ffe5-2995-44e1-8a23-df4ce5c4cba7
  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') }}"
    timeout: "00:01:00" #NOTE: change this timeout for how long you're willing to wait for the alarm to stop snoozing.
    continue_on_timeout: false #NOTE: The automation won't continue once the above wait_template has timed out. 
  - entity_id: light.kastlamp
    service: light.turn_on
  mode: single
1 Like

I dont get anymore errors, so thats good
However, just tested with an alarm
I didnt snooze, and just stopped the alarm, but no light went on
Will see to start topic i guess

As I said, the automation needs some testing. I’d suggest you monitor the alarm sensor using a template in the Devloper Tools. {{ state_attr('sensor.woonkamer_mini_alarms', 'alarms[0].status') }}. Setup your alarm and see how this state changes as you run through the alarm ringing, snoozing and being turned off. This should help you identify which states the alarm status goes through in a typical use.

You could try using a wait_trigger instead of a wait_template The latter lets you specificy a to and from for the wait_trigger so might give you more control that you’re after.

Note: I think your requirements of the Google Home integration might be greater than the features of the integration so it maybe difficult to impliment the automation as seamlessly as you like. It looks like the GH integration uses polling to update each sensor, instead of updating each sensor when it’s state changes. The default/recommended polling is 180 seconds. So it’s possible that the alarm could go from being set to ringing to snoozed or back to none or set for the next alarm all in the space of 180s. In which case the follow of the automation wouldn’t get triggered as you are expecting. You can change the polling rate in the integration but it’s been reported that anything less than 180s can cause errors or the integration to break.

First off, i made a new topic, so maybe reply there…

You are right that the polling will cause issues…
Its just too slow.
Perhaps its better to make an automation that starts at the day and time set in the alarm?
Or how would you do it?

You can answer in the new thread cause nobody replying there