Call trigger once a day

Hey, can anyone help me with an automaton I’m doing. I have done a automation where when the hallway sensor is trigger with a time condition between 07:00 to 10:00 it will trigger Alexa TTS to say good morning how are you today. Works perfectly, my only problem is that it triggers everytime there is motion on that sensor between the times stated… how do I config it to only announce once everyday between the times stated!
Thanks

1 Like
  • Create an input_boolean and call it whatever you like.
  • Create an automation to switch ON the boolean every night at 00:00.
  • Now, go to your light automation and create one more action, where it will turn OFF the boolean when the “goodmorning text” announces.
    at the same automation, create a condition to only do the action when the boolean is ON.

boom! done!

Hopefully this will work correctly! (hoping I didnt miss anything)

3 Likes

add a condition, it won’t trigger on a day it already has been triggered:

condition: template
value_template: >
  {{as_timestamp(state_attr('automation.yournamehere','last_triggered'))|timestamp_custom('%-d') != as_timestamp(now())|timestamp_custom('%-d')}}
12 Likes

I was going to suggest the following but, as we both know, it will fail at startup:

{{ state_attr('automation.something','last_triggered').day != utcnow().day }}

At startup, Home Assistant restores last_triggered not as a datetime object but as a string (and will cause the template to fail). To circumvent this bug, it’s best to use your approach of first converting all dates to timestamps.

I’m hoping @pnbruckner is successful in having this restore bug corrected so then last_triggered can be handled as a datetime object, even at startup.

2 Likes

I’m actually working on that this morning. It’s as simple as adding two lines of code. The harder part (as usual) was updating the tests.

And speaking of that, what I found was the common function that all tests use when testing a restored state is flawed. It doesn’t properly account for what happens to certain data types (such as datetimes :wink:) when they are saved & restored. I fixed that and ran the automation related tests, and surprise! surprise! they failed. And the simple change to the automation code not only lets those test pass, it actually does what it should – i.e., restores last_triggered as a datetime instead of a string.

Now I’m running all the tests to see if anything else fails (with a proper save/restore test function.) And, it just finished. And guess what, the automation related tests were the only ones that failed. So that’s good!

Now for the PR…

EDIT: … which is https://github.com/home-assistant/home-assistant/pull/24951

3 Likes

Thank you for your reply’s :-)… still trying to get my head around it… @argykaraz sorry I’m really new to input_boolean.
Looking at the input_boolean page, it states notify_home: do I use that if mine is a sensor that I’m using? And then initial : off does that stay as off? Sorry if this is simple stuff!!

that is just the name of the input_boolean that was used in the example.

So in that example the input_boolean that will be created will have an entity_id of “input_boolean.notify_home”.

it is only off whenever HA starts/restarts. You don’t have to use that config and if it’s something that you want to keep it’s current state across restarts then you shouldn’t use it.

if you replace that with:

input_boolean:
  my_good_morning_bool:
    name: Already said good morning

then you will get an entity_id of “input_boolean.my_good_morning_bool” with a friendly name of “Already said good morning” that will keep it’s state across restarts

So I’m still struggling with this automation, that only triggers once a day between a time frame. this is my config for the automation, but doesn’t seem to work, can anyone take a look at it, and let me know where I’m going wrong. Thank you

- id: '1562185436507'
  alias: Morning
  trigger:
  - entity_id: binary_sensor.hallway_sensor_1_motion
    platform: state
    to: 'on'
  condition:
  - after: 07:00
    before: 08:30
    condition: time
  - condition: state
    entity_id: input_boolean.my_good_morning_bool
    state: 'on'
  action:
  - data:
      data:
        type: announce
      message: This is a test.
      target:
      - media_player.hallway_dot
    service: notify.alexa_media
  - data:
      entity_id: input_boolean.my_good_morning_bool
    service: input_boolean.turn_off



then my Boolean resets every night.

  • id: ‘1562654261154’
    alias: Already Good Morning Boolean
    trigger:
    • at: ‘23:59’
      platform: time
      condition: []
      action:
    • data:
      entity_id: input_boolean.my_good_morning_bool
      service: input_boolean.turn_on

you need to wrap your times in the time condition in the first automation in quotation marks. And I’m not 100% sure but I think you need to add the seconds also.

So it would look like:

condition:
  - after: '07:00:00'
    before: '08:30:00'
    condition: time

Thanks, so apart from the time my config looks ok and it should all work after I wrap the times?

I think so…the warranty is only good until you finish reading this post. :wink:

If not then come on back and we’ll work it out from there

haha, so we have hit a problem lol, here is my config file again refined.
the automation works great with triggering the text to speech. The only way i can stop it from triggering the text to speech is to manually turn off the input_boolean so i know the boolean works, but problem is, it’s not doing it on its own? or within the automation like it should… Thanks again @finity
Thought i would add a delay after it called the tts service but doesn’t work with or without it.

- id: '1562185436507'
  alias: Morning
  trigger:
  - entity_id: binary_sensor.hallway_sensor_1_motion
    platform: state
    to: 'on'
  condition:
  - after: '20:00:00'
    before: '21:30:00'
    condition: time
  - condition: state
    entity_id: automation.switch_on_boolean
    state: 'on'
  action:
  - data:
      data:
        type: announce
      message: This is a test.
      target:
      - media_player.hallway_dot
    service: notify.alexa_media
  - delay: 0:15
  - data:
      entity_id: input_boolean.my_good_morning_bool
    service: input_boolean.turn_off

  • id: ‘1562701925157’
    alias: Switch On Boolean
    trigger:
    • at: 00:00
      platform: time
      condition: []
      action:
    • data:
      entity_id: input_boolean.my_good_morning_bool
      service: input_boolean.turn_on

You’re checking if the automation is on, not if the input_boolean is on.

why complicate things with the extra input_boolean and extra automation, and not use the test we suggested a few posts earlier, to check if has been triggered already on that day?

1 Like

Do you guys have working and functional automation which is triggered only once a day?

When do you want to trigger it? It can be as simple as this which will fire every day at 9am.

  trigger:
    platform: time
    at: "09:00"

no, it is an automation triggered by a motion under condition between 6 to 8am
So sequence of condition 1st between 6 and 8am and second should check if the automation ran already if not let it run if yes then block it.

  - alias: Morning
    trigger:
    - entity_id: binary_sensor.wyzesense_779334de
      platform: state
      to: 'on'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:00:00'
          before: '09:00:00'
        - condition: template
          value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.morning.attributes.last_triggered | default(0)) | int > 3600)}}'
    action:
    - service: notify.alexa_media
      data:
        data:
          type: tts
        target: 
          - media_player.echo_show
        message: " Good morning you sticky monster again you stinky monster"
{{ (as_timestamp(now()) - as_timestamp(states.automation.morning.attributes.last_triggered | default(0)) | int > 3600)}}

Your condition window is 3 hours long but you’re only checking for a last triggered in the last hour. Change 3600 to 10,800

would that be as simple as replacing 3600 with 10800?

Yes. Sorry see my nina edit.