Help: Automation on Event type state change for x minutes

I was trying to create an automation to inform me when any entity change status to unavailable for 1 minutes.

I have successfully to use blueprint from others and also create automation without blueprint.
But those without for function. Sometimes my device will lose wifi for a few seconds but it will connect back very fast. So that doesn’t feed my needs.

Below is the working version without for x minutes. Anyone can help on this?

trigger:
platform: event
event_type: state_changed
event_data: {}

condition: template
value_template: '{{trigger.event.data.new_state.state == ''unavailable''}}'

action:
service: notify.mobile_app_iPhone
data:
  message: ' {{ trigger.event.data.entity_id }} is not available'

I don’t think you will be able to use the state_changed event. The main reason is that this event is trigged the second the value is changed. So, you’ll always get the state with no delay.

Maybe, the best way to archive what you want is to change that to time trigger, running every 1 minute. And then you do a for loop through all your entities checking the state and the last changed date.

Use that one. It will create a sensor for unavailable devices and then you can set an automation from that.

Note: the sensor will update once every 60 seconds.

Thanks. That’s awsome. It worked nicely.