Trigger question / (newbee)

Hi
i want to trigger an automation based on an Event (Garage door open + 10 minutes) - and want to repeat the automation every subsequent 10 minutes when the Event is still valid.
Currently i have only the initial trigger - but how can i implement the reminder?

- id: '1647686414969'
  alias: Alexa Question GarageOpen
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_open
    to: 'on'
    for:
      hours: 0
      minutes: 10
      seconds: 0
  condition: []
  action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: The garage is open since 10 minutes - should i close it?
      event_id: garage_left_open
      alexa_device: media_player.wohnzimmer
  mode: single

I´m open for any advice.
Thank you in advance
Kind regards
Thorsten

Take a look at the Alert Integration, might be exactly what you are looking for.

Here is something to consider. You will need another automation to stop adding 10 minutes. I am not 100% certain about the text template and if HA will evaluate it.

- id: '1647686414969'
  alias: Alexa Question GarageOpen
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.garage_open
    to: 'on'
    for:
      minutes: "{{ states('input_number.time_to_delay') | int(0) }}"
  condition: []
  action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: "{{ 'The garage is open since' ~ states(input_number.time_to_delay')  ~ 'minutes - should i close it?' }}"
      event_id: garage_left_open
      alexa_device: media_player.wohnzimmer
  -  service: input_number.set_value
     data_template:
       entity_id: input_number.time_to_delay
       value: "{{ states('input_number.time_to_delay') | int(0) + 10 }}"
  mode: single

Hi
i get an
2022-05-26 20:25:49 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: template value should be a string for dictionary value @ data[‘action’][0][‘data’]. Got None. (See /config/configuration.yaml, line 8).
Line 8 is the line with the “minutes:”
Do i have to set/pre-define the “input.number.time_to_delay” somewhere?
Best
Thorsten

Hi @cyn
thanks for the hint. Is it possible to use the alert to trigger an automation?
This would be required to trigger my alexa asking me if i want to close it.
Best
Thorsten

Yes you have to setup the input number helper.

Yes, once you set up the alert look in developer tools, states. I defined my alert as alert_control. When you look thru your entries you should see an entity called alert.alert_control. This entity has 3 states, idle, on & off. Use that as your automation trigger.

Any advice how?
(sorry - I´m really new to home assistant and not yet skilled for those)
Thank you in advance.

You could start here:

Hi AllHailJ,
how do i setup the input number helper?
Guessing it´s easy … but I´m not aware.
Best
Thorsten

Two ways - through the UI or in configuration.yaml

In configuration.yaml

input_number:
  zone11:
    name: Zone11
    icon: mdi:clock-start
#    initial: 10
    min: 0.5
    max: 15
    step: 0.5
    mode: slider
    unit_of_measurement: min

or through the UI

go to settings

Screenshot from 2022-05-27 12-21-46

Devices and Services and select helpers at the top right of the page.

Click on Add Helper in the bottom right

This won’t work. A state trigger only triggers when the state changes. If the for option is used then it only triggers after a state change if the entity remains in that same state for that amount of time. You can’t keep adding time to it though, that won’t work. It won’t fire again until the state changes to off and then back on no matter what time_to_delay is set to. Try it on test entities if you don’t believe me.

@thorsten-gehrig so this is actually a bit complicated unfortunately. @cyn is right, the tool for the job is alert. But alert has a problem since it can only call a notification service and you want to call a script, alert can’t do that. So I guess here’s the two options I would recommend, take your pick:

Alert to Script

  1. Create a notification group like this. Yes services is supposed to be empty
notify:
  - platform: group
    name: Alexa actionable notification
    services: []
  1. Create an automation which listens for calls to that notification group and passes them to your script like this:
- alias: Notify to alexa actionable notification
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: alexa_actionable_notification
  action:
    service: script.activate_alexa_actionable_notification
    data:
      text: "{{ trigger.event.data.service_data.message }}"
      event_id: "{{ trigger.event.data.service_data.data.event_id }}"
      alexa_device: "{{ trigger.event.data.service_data.data.alexa_device }}"
  1. Make an alert like this
alert:
  name: Garage door open
  entity_id: binary_sensor.garage_open
  state: 'on'
  skip_first: true
  repeat: 10
  notifiers: 'alexa_actionable_notification'
  message: "The garage is open since 10 minutes - should i close it?"
  data:
    event_id: garage_left_open
    alexa_device: media_player.wohnzimmer

With these 3 the alert will call your notification service 10 minutes after the garage is open and every 10 minutes until it closes. And the automation will listen for calls for that service and pass them to the script.

Automation option

You can do this as a pure automation but it involves a template. Here’s what you could do:

trigger:
  - platform: template
    value_template: >-
      {% set minutes_diff =  ((now() - states.binary_sensor.garage_open.last_changed).total_seconds() / 60) | int %}
      {{ is_state('binary_sensor.garage_open', 'on') and minutes_diff > 1 and minutes_diff % 10 == 0 }}
action:
  - service: script.activate_alexa_actionable_notification
    data:
      text: The garage is open since 10 minutes - should i close it?
      event_id: garage_left_open
      alexa_device: media_player.wohnzimmer

Since this template involves now() its going to be evaluated every minute. And what it will do is first get the total number of minutes between now and the last time the garage changed. If the garage is open and that total minutes is greater then 1 and it’s a multiple of 10 then it sends your notification.

Sorry I wish this was easier but if you really need a script to send your notification I think these are your best options since alert needs a workaround to do that.

2 Likes

I have no reason not to believe you and I am not getting into a pissing contest. I was simply trying to help.

I was going to suggest a repeat - while, to repeat the alexa notification script every ten minutes while the binary_sensor was on. However, I prefer your suggestion because it’s not only more concise but also survives restarts/reloads. :+1:

1 Like

Sorry perhaps that first part came off more aggressive then I intended. I wasn’t trying to start an argument or anything just wanted to make you and the thread author aware of the limitations of state-type triggers.

Missing a colon Mike.

1 Like

Whoops, thanks, fixed it

1 Like

Hi guys
appreciate all your help.
Got it finally working!
I´ve understand the alert calls the notification.
The alert itself “defines” the data “event_id” and “alexa_device” and “message” (to be used finally in the last step).

The “notify” is defined as call the “Alexa actionable notification” -Service - which is empty.
End of this thread.
But the automation does recognize this call for the (empty) notification Service (trigger: call service / Aleca actionable notification) - as trigger - and as action calls the script “script.activate_alexa_actionable_notification” (all the variables found by the template)

Somehow weird - but if it works I´m happy.
What I still confusing me - but guessing that’s another story - while my Alert-condition “binary_sensor.garage_oben” is still state “on” - the alert “alert.garage_door” is switching to Idle after the first call. Shoudnt the status stays “on” while it´s neiter confirmed not cleared?

Best
Thorsten

Yes it should, that’s very odd. I have a lot of alerts and I’ve never seen anything like that. As long as binary_sensor.garage_open has state on then the alert should have state on. Unless you dismissed it, in which case it should have state off. It should only have state idle if binary_sensor.garage_open has a state other then on.

If you are seeing the alert with state idle while binary_sensor.garage_open has state on then you should report a bug. But I would really be scratching my head over here. I have been using alerts for years. I have 27 right now and have had more in the past, have never had an issue like that tbh.