Run Automation based on first time in a day that an entity changes state (a door opens)

I would like to run a morning routine which is based on opening the kitchen door for the first time each day.
It would include Alexa giving me my calendar update, getting a BBC World Service News Briefing, and maybe other things.
It should start with Alexa asking if it is me who, in this case, opened the door - the answer will be yes 99% of the time, but should check.
Think I can do most of this, but it is the initial trigger which is beyond me - how do I start that automation, which should only run once a day, based on that door opening for the first time.
I am slowly getting into HA, but still very much at beginner stage - I can only do things that people tell me how to do !

Use a date helper to hold the last run date. Then have a condition that when it triggers on door open, that checks the helper is before todays date. In your actions, update the date helper to todays date.

Another way is to set an input boolean when the door’s opened and then reset it at midnight (or some other preferred time).

Thank you very much guys
Sorry to be a pain but would either one of you mind posting some code for these.
I am ok at editing code to suit my needs, but writing from scratch is a bit beyond me usually - although being able to do more things in the UI is certainly helping

You can actually do all of this from the UI. Would you prefer doing that or for it to be done with YAML?

You need something like this. Change the trigger to be your door sensor and add your other alexa actions to it. You will need an input date helper called last door open (or change the references to this). It basically checks the date of last door open is not equal to todays date and if not, sets the last door open date helper to todays date. The next time it runs today, these dates will be the same and will fail the condition and so not run actions. Then tomorrow, dates will again be different and it will run.

alias: Door open automation
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.door_open_sensor
    to: open
condition:
  - condition: template
    value_template: >-
      {{
      as_timestamp(states('input_datetime.last_door_open'),'%Y-%m-%d')|timestamp_custom('%Y-%m-%d')
      != as_timestamp(now())|timestamp_custom('%Y-%m-%d') }}
action:
  - service: input_datetime.set_datetime
    data:
      date: "{{ as_timestamp(now())|timestamp_custom('%Y-%m-%d') }}"
    target:
      entity_id: input_datetime.last_door_open
mode: single

Effectively what you are asking for is an automation that executes its action only if it wasn’t triggered yet during the current day. In other words, the last time it was triggered has to be prior to the start of the current day.

That’s a common request and the solution is to use a Template Condition that checks the automation’s last_triggered attribute. If its value is prior to today’s date at 00:00 then it means the automation has not yet triggered today and it is allowed to continue (to execute its action).

condition:
  - condition: template 
    value_template: >
      {{ this.attributes.last_triggered | default(as_datetime(0), true) < today_at() }}
  • today_at() produces a datetime object whose value is today’s date and time (at 00:00).

  • this.attributes.last_triggered reports the value of the automation’s last_triggered attribute (which is a datetime object).

  • If the automation is new and has never been triggered yet, it will not have a last_triggered attribute. To prevent a template error for this unique situation, default(as_datetime(0), true) produces a datetime object whose value is in the past (Unix Epoch: 1970-01-01 00:00).

1 Like

Ooh nice way to do it!

Hi guys
Apologies for the very late reply.
I have not had a chance to look at this yet, but just wanted to thank you both for helping out. It is very much appreciated

I sort of have it working, however this morning it triggered in our small gym room. And I have a feeling yesterday when the gym door was opened, it triggered Alexa to say:
This is a test of the Alexa Actionable Notification.
I do have another actionable notification set up for that gym when the door is opened, although it still needs some work.

What I have so far is

alias: Actionable Notification Morning Briefing
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_9
    to: "on"
condition:
  - condition: template
    value_template: >-
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
      today_at() }}
action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: Good morning Mike. Do you want your morning briefing?
      event_id: actionable_notification_morning_briefing
      alexa_device: media_player.kitchen_echo_show
mode: single

The entity id specified is the kitchen door contact sensor.

The script.activate_alexa_actionable_notification is:

alias: activate_alexa_actionable_notification
description: Activates an actionable notification on a specific echo device
fields:
  text:
    description: The text you would like alexa to speak.
    example: What would you like the thermostat set to?
  event_id:
    description: Correlation ID for event responses
    example: ask_for_temperature
  alexa_device:
    description: Alexa device you want to trigger
    example: media_player.master_bedroom_echo_dot
  suppress_confirmation:
    description: Set true if you want to suppress 'okay' confirmation
    example: "true"
sequence:
  - service: input_text.set_value
    data_template:
      entity_id: input_text.alexa_actionable_notification
      value: >-
        {"text": "{{ text }}", "event": "{{ event_id }}",
        "suppress_confirmation": "{{ suppress_confirmation }}"}
  - service: media_player.play_media
    data_template:
      entity_id: "{{ alexa_device }}"
      media_content_type: skill
      media_content_id: amzn1.ask.skill.jfjfjhjhjv658
mode: single

Not sure whether I have mixed and matched incorrectly from the advice above, or just made some other daft error.

While I am here, can anyone also sort out a different automation, but still related to timestamps. Looking to get Alexa to ask if the aircon and lights should be turned on if the door is opened, and there has been no motion inside for the previous 10 minutes/

I have the following:

alias: Gym Work Out Question
description: >-
  Asks if the person is planning a workout and would like the aircon and lights
  turned on, providing there has been no motion detected for last 10 minutes
trigger:
  - platform: state
    entity_id:
      - binary_sensor.gym_ring_door_contact_sensor
    from: "off"
    to: "on"
condition:
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.lumi_lumi_sensor_motion_aq2_motion_6.last_changed|as_timestamp(0)
    > 600 }}
action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: >-
        Hi there. Planning a workout ? Would you like me to turn on the aircon
        and lights ? 
      event_id: actionable_notification_gym_door_opened
      alexa_device: media_player.gym_echo_dot
mode: single

Thought I had something similar working yesterday, but have changed the condition and now all I get from Alexa is “this is a test of the alexa actionable skill”

No, I am still doing something wrong (unless one of the kids was in the kitchen after midnight each day, which is possible)
The automation I have now is:

description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_9
    to: "on"
condition:
  - condition: template
    value_template: >-
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
      today_at() }}
action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: Good morning Mike. Do you want your morning briefing?
      event_id: actionable_notification_morning_briefing
      alexa_device: media_player.kitchen_echo_show
mode: single

But I then have a script

alias: Kitchen Good Morning
sequence:
  - service: media_player.play_media
    data:
      media_content_id: "media_content_id: Good Morning"
      media_content_type: routine
    target:
      device_id: 08db3ec48f8c102c6134a537a28108a1
  - condition: template
    value_template: >
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
      today_at() }}
mode: single

My head is spinning and I no longer know whether both are required - and if they are, what am I getting wrong? I suspect I am trying to run before I can walk with HA.

I have rechecked, and the routine does run correctly if I just give Alexa the routine name.

I’ve used this approach before, but the template method is nice too.

1 Like

No, it is not triggering at all.
Why is the template condition in the script ? Because I am a dumb*ss. Removed now, so will see if the automation triggers the script tomorrow

Thanks Rofo
Will stick with the template method for now and see if I can get that working, but will come back to the input boolean as a plan B.

Thank you again.
It did trigger - someone did go in the kitchen after midnight.
It then stopped as the “condition failed”
Any suggestion ?
Also, could you say how to have the condition start at a specific time, rather than midnight ? Looks like I need a reset at maybe around 2am

condition:
  - condition: template
    value_template: >-
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
        today_at('02:00') }}

Thank you
Have put this in.
Probably will get messed up tonight as my sons football team (and mine) are playing very late our time, so he will very likely be in the kitchen after 2 am (in fact probably after 5am), but I will ask him what happens if he does, otherwise wait a day.

Still a problem

The automation was missing something which I corrected, but as it is now:

alias: Actionable Notification Morning Briefing
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_9
    to: "on"
condition:
  - condition: template
    value_template: |-
      {{ this.attributes.last_triggered | default(as_datetime(0), true) <
        today_at('02:00') }}
action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: Good morning Mike. Do you want your morning briefing?
      event_id: actionable_notification_morning_briefing
      alexa_device: media_player.kitchen_echo_show
      suppress_confirmation: "true"
mode: single

I tried to test it with the “run” option to see if there was a problem with the action, and there is. The Trace shows:

Actionable Notification Morning Briefing triggered

activate_alexa_actionable_notification started triggered by automation triggered Actionable Notification Morning Briefing triggered

activate_alexa_actionable_notification turned on triggered by automation Actionable Notification Morning Briefing triggered

Alexa Actionable Notification holder changed to {"text": "Good morning Mike. Do you want your morning briefing?", "event": "actionable_notification_morning_briefing", "suppress_confirmation": "true"} triggered by automation Actionable Notification Morning Briefing triggered

activate_alexa_actionable_notification  turned off triggered by automation Actionable Notification Morning Briefing triggered

So it is getting switched on and then off, it seems by the same thing, virtually simultaneously.

This does not explain why opening a completely different door does trigger a different alexa to ask the text “Good morning Mike, Do you want your morning briefing” (although it goes no further when I respond). And there is no Trace shown for the Actionable Notification Morning Briefing when I opened that other door. But I guess that is the next part of the puzzle to try to solve.

Any further ideas on what dumb thing I am doing now ?

I have not changed the script, which I thing I copied directly from the github site, but is below just in case:

alias: activate_alexa_actionable_notification
description: Activates an actionable notification on a specific echo device
fields:
  text:
    description: The text you would like alexa to speak.
    example: What would you like the thermostat set to?
  event_id:
    description: Correlation ID for event responses
    example: ask_for_temperature
  alexa_device:
    description: Alexa device you want to trigger
    example: media_player.master_bedroom_echo_dot
  suppress_confirmation:
    description: Set true if you want to suppress 'okay' confirmation
    example: "true"
sequence:
  - service: input_text.set_value
    data_template:
      entity_id: input_text.alexa_actionable_notification
      value: >-
        {"text": "{{ text }}", "event": "{{ event_id }}",
        "suppress_confirmation": "{{ suppress_confirmation }}"}
  - service: media_player.play_media
    data_template:
      entity_id: "{{ alexa_device }}"
      media_content_type: skill
      media_content_id: amzn1.ask.skill.5ee34aa5-ac06-43d1-8423-7ebfedf53d46
mode: single

Sorry this is pretty long, and may still not give you all the information you need.

Maybe not directly helpful but in such cases like yours I typically start over and delete every automation that does not work (or at least disable it). Then I work on just one thing at a time. This way I prevent mix-up between different automations.