[Resolved] Automation on binary_sensor (ping) - condition if only off for more than 30 mins?

I have this Automation on binary_sensor (ping) to track some mobile devices and make announcement when they are back home.
The problem now is that if the phone disconnected from wifi(state: off) and connected back within a minutes, it will trigger the action because it met the condition that the states is from off to on.

How do I make the condition to include if only state off for 30mins or 60mins between off to on?

Thanks!

---------------configuration.yaml--------------

#Binery Sensor
binary_sensor:
  - platform: ping
    host: 192.168.xxx.xxx
    name: "cedric"
    count: 2
    scan_interval: 180

---------------automations.yaml---------------

- id: CedricHome
  alias: CedricHome
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.cedric
    from: 'off'
    to: 'on'
  condition:
  - condition: time
    after: 07:00:00
    before: '23:59:00'
  action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_group
      message: Welcome Back Cedric!
      language: en

Maybe you could add a delay in the definition of your template binary_sensor so it only changes state when it is in the state for a certain amount of time.

delay_off: 3600

The state would only change to off when the device is off continuously for 60 minutes.
When the state changes to on you could be sure it was off for 60 min previously.

or just add “for:” to the trigger:

trigger:
  - platform: state
    entity_id: binary_sensor.cedric
    from: 'off'
    to: 'on'
    for:
      minutes: 30
4 Likes

Hi seanomat, I tried this but I am so dumb and couldn’t make it a success

Hi finity, I have just tested this and it seems worked! Thanks a lot for your helps!

I am surprised, that this is the solution.
This would also trigger if the device was only OFF for one second, and then ON for 30 minutes.
Exactly what you were trying to avoid.

1 Like

Hi Seanomat, you are right. I spoke too soon. I set 45 mins and it announced after 45 mins…haha

could you provide me an example how to blend delay_off: 3600 into my current ping configuration?

Thanks!

I have the impression, that the delay_* parameter is not respected by the ping-platform.
That is why I mentioned the template binary sensor (you did not provide your sensor-definition).

In order to use the delay_off you would have to create a template binary sensor, based on your ping-sensor:

binary_sensor:
  - platform: template
    sensors:
      presence_cedric:
        device_class: presence
        value_template: "{{ is_state('binary_sensor.cedric','on') }}"
      delay_off: 3600

If this is all about presence, you would be much better of, with using the ping-platform with the device-tracker and assign this device tracker to a person.
This integration has all the features you want build-in, and gives you an additional abstarction-layer in case the devices of a person change.

I just tried this

binary_sensor:
  - platform: ping
    host: 192.168.xxx.xxx
    name: "cedric"
    count: 2
    scan_interval: 180
  - platform: template
    sensors:
      presence_cedric:
        device_class: presence
        value_template: "{{ is_state('binary_sensor.cedric','on') }}"
      delay_off: 3600

The check configuration will takes forever with below error found in log:

Logger: aiohttp.server
Source: helpers/config_validation.py:748
First occurred: 8:27:47 PM (1 occurrences)
Last logged: 8:27:47 PM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 118, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 56, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 72, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 129, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/core.py", line 29, in post
    errors = await async_check_ha_config_file(request.app["hass"])
  File "/usr/src/homeassistant/homeassistant/config.py", line 874, in async_check_ha_config_file
    res = await check_config.async_check_ha_config_file(hass)
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 215, in async_check_ha_config_file
    p_validated = platform_schema(p_validated)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 495, in verify
    return cast(Dict, schema(value))
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 386, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 339, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 748, in validator
    if key in config:
TypeError: argument of type 'int' is not iterable

i found out the check configuration passed once i commented out delay_off: 3600.

I’m pretty sure that your delay_off is underindented, it should be at the same level as value_template on the line above, so try this:

binary_sensor:
  - platform: ping
    host: 192.168.xxx.xxx
    name: "cedric"
    count: 2
    scan_interval: 180
  - platform: template
    sensors:
      presence_cedric:
        device_class: presence
        value_template: "{{ is_state('binary_sensor.cedric','on') }}"
        delay_off: 3600
1 Like

Was just about to type the same thing. My example was not indented correctly.

Well, now I actually did :laughing:

Thanks for pointing out this!
I never know that is so important!

Thanks seanomat for resolving my problem!

1 Like