Template Binary Sensor - delay_off not triggering automation?

Hi,
I need help with the automation. I have a sonoff rf bridge running Tasmota, and a Sonoff PIRII.
Automation 1: Motion detected, turn on lights is working. Automation to turn the light off is not triggering. Since the PIRII does not send a off command, using a template sensor will be the way to go in Home Assistant?
Config:
Sensor
- platform: mqtt
name: “sonoff_433bridge”
state_topic: “rfbridge/tele/RESULT”
value_template: ‘{{ value_json.RfReceived.Data }}’

Binary Sensor
- platform: template
sensors:
kitchen_pir:
friendly_name: “Kitchen PIR”
value_template: “{{ is_state(‘sensor.sonoff_433bridge’, ‘EB62CE’) }}”
delay_off: ‘00:01:00’
device_class: motion

Automation not triggering
id: ‘1558410909280’
alias: Kitchen Motion - Turn off
trigger:
- entity_id: binary_sensor.kitchen_pir
for: 00:03:00
from: ‘on’
platform: state
to: ‘off’
condition: []
action:
- data:
entity_id: light.sonoff2_3
service: light.turn_off

Hey there, please format your code correctly, by adding 3 times ` in the start and another 3 at the end, so we can read it clearly :slight_smile:
Like this: image

- platform: mqtt
  name: "sonoff_433bridge"
  state_topic: "rfbridge/tele/RESULT"
  value_template: '{{ value_json.RfReceived.Data }}' 
  delay_off: '00:00:06'

- platform: template
  sensors:
   kitchen_pir:
    friendly_name: "Kitchen PIR"
    value_template: "{{ is_state('sensor.sonoff_433bridge', 'EB62CE') }}"
    delay_off: '00:01:00'
    device_class: motion      

- id: '1558410909280'
  alias: Kitchen Motion - Turn off
  trigger:
  - entity_id: binary_sensor.kitchen_pir
    for: 00:03:00
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id: light.sonoff2_3
    service: light.turn_off

so you sensor only sends the motion detected message, and never motion detected end?
Your template will not work as it’ll wait for the condition to no longer be true, add a delay, then turn off.

You might be better off creating a binary sensor with an automation that turns it on upon motion message received, and turns it off 1 min later provided it’s been more than [say] 59sec since you received the on message.

having had an issue with delay_off myself, I tried to find the docs on this, but couldn’t find it. Could you point me to that link please?

I have an automation using this delay_off in the wait_template which might be useful:

  - alias: 'Switch Masterbed outlet when movement'
    id: 'Switch Masterbed outlet when movement'
    initial_state: off
    trigger:
      platform: state
      entity_id: sensor.master_bedroom_motion_sensor
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{ is_state ('sensor.activity_selection', 'Naar bed')}}
      - condition: template
        value_template: >
          {{is_state('switch.master_bed_outlet', 'off')}}
    action:
      - service: switch.turn_on
        entity_id: switch.master_bed_outlet
#      - wait_template: >
#          {{as_timestamp(now()) | int - 
#            as_timestamp(states.sensor.master_bedroom_motion_sensor.last_changed) | default(0) | int > 120 }}
      - wait_template: >
          {{ is_state('binary_sensor.master_bedroom_motion_sensor_timed','off')}}
#          {{ is_state('sensor.master_bedroom_motion_sensor', 'off') }}
#      - delay:
#          minutes: 1
      - service: switch.turn_off
        entity_id: switch.master_bed_outlet
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: You've walked safely lift. Signing off.

and binary_sensor:

binary_sensor:
  - platform: template
    sensors:
      master_bedroom_motion_sensor_timed:
        friendly_name: 'Master bedroom motion sensor timed'
        value_template: >
          {{ is_state('binary_sensor.master_bedroom_motion_sensor','on')}}
        delay_off:
          minutes: 1
        device_class: motion

this works fine. trying to recreate another with the exact same format would work, and would switch off at the exact moment motion was detected. Which is quite the opposite of what I was trying to do…

so, reading up in the docs would be helpful;-)

it’s in the template binary_sensor doc:

This relies on the condition going to false for a period of time (so if your sensor returns on and off states, it waits for the state to be off for the time defined in delay_on
if your sensor never goes to off, then as above I would create an input_boolean to track motion.
Your automation cannot update the input_boolean based on the sensor’s state since the state will never go to off before going to on, you need to instead watch the events. So a trigger like the below could be used to turn the input_boolean on

  trigger:
    - platform: event
      event_type: signal_received
      event_data:
        entity_id: sensor.motion_sensor

your actions could be something like:

  • turn input_boolean on
  • delay while you consider motion is active (like 1 min)
  • condition that the automation has not been triggered in the last min (=motion is still present)
  • turn input_boolean off

And use your input_boolean as your base for motion detection in all your other automations

thanks! I knew I had read it , but couldn’t find it anymore…

so in my case, if the motion sensor is not ‘on’ for 1 minute, it triggers the automation. Which is what I need. As long as there is motion, the lights must stay on.
If everyone’s gone or a sleep, no motion is detected, the delay starts to count and after 1 minute, the lights can turn_off. When motion is detected within that delay_off timeframe, it restarts.

The Template Sensor’s indentation is incorrect. It should use two spaces, not one.

Screenshot%20from%202019-05-23%2009-51-21

Been busy for a while and only came back to this issue recently.
I ended up using NodeRed for my automation. I subscribed to the MQTT topic that the Rf bridge transmits the codes to, once the PIR signal is received it checks if the light is on/off, turns on the light if needed, and starts a stop timer to turn it back off (re-triggering the motion sensor, resets the stop timer)

I know hassio automations can do the same, but I like the visual flow layout of NodeRed more… (And you can see the automation running from node to node)