Problem with timeout filter on binary template sensor

I’m using a motion sensor that broadcasts at 433 MHz, with a RF Bridge. When the sensor triggers, the Bridge publishes a state of true for the associated sensor. But the sensor doesn’t send anything when there is no motion, so I want the sensor to timeout to false if no code has been received within a set time. Here is the code so far:

rf_bridge:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'return format_hex(data.sync);'
            low: !lambda 'return format_hex(data.low);'
            high: !lambda 'return format_hex(data.high);'
            code: !lambda 'return format_hex(data.code);'
      - lambda: |-
          if (data.code == 0x5f3577) {
            id(living_room_motion).publish_state(true);
          }

binary_sensor:
  - platform: template
    id: living_room_motion
    name: "Living Room Motion"
    device_class: motion
    filters:
      - timeout: 10s
          value: false

There are two problems. First, the compiler reports a mapping error on the value line. I also tried this way with the sensor:

  - platform: template
    id: living_room_motion
    name: "Living Room Motion"
    device_class: motion
    filters:
      - timeout:
          timeout: 10s
          value: false

but that gives an invalid option, though this is the example given in the docs. so my first question is, what is the proper format for this sensor to timeout after 10 seconds and update to false after timeout?

If I write the sensor this way:

  - platform: template
    id: living_room_motion
    name: "Living Room Motion"
    device_class: motion
    filters:
      - timeout: 10s

then it will compile and flash, but the sensor never updates to NaN or false, just remains at true. I don’t know why. Any help is appreciated.

Timeout doesn’t have value option.
And only line ever updates your template sensor is:

has this:

which gives the value options. Is that deprecated and, if so, is there a way to have a template sensor revert to a default value if time passes without an updated state being published?

You are confusing binary sensor and sensor.

But the correct solution to your problem is to use a script as a timer.

binary_sensor:
  - platform: template
    name: "Motion sensor"
    id: motion_sensor

# Script in restart mode with delay
script:
  - id: restart_script
    mode: restart  # Restarts if triggered again while running
    then:
      - binary_sensor.template.publish:
          id: motion_sensor
          state: ON
      - logger.log: "Output sensor set to TRUE"
      - delay: 5s  # Delay for 5 seconds
      - binary_sensor.template.publish:
          id: motion_sensor
          state: OFF
      - logger.log: "Output sensor set to FALSE"

rf_bridge:
  on_code_received:
    then:
      - if:
          condition:
            lambda: return data.code == 0x5f3577;
          then:
            script.execute: restart_script

Whatever approach you choose, template binary sensor needs to be updated by your code with .publish_state or by lambda within the sensor itself. Otherwise it’s completely dormant.

I shouldn’t have assumed that binary template sensor inherited the timeout options. I’m using the script. Thanks to both for your help.

1 Like