[Help] Create a Binary Sensor Template for 'state off for X seconds'

Hi,

I’m trying to wrap my mind around the Binary Sensor Templates. I looked through the documentation and searched a lot, but can’t find the correct variables. Basically, I want to create a Binary Sensor switches on as soon as a different binary sensor has been off for X amount of time.

Normally I just use the built in automation for this, since you can trigger based on state time. However I need this in Node-RED. I haven’t found an easy way in Node-RED to do this. So I figured why not create a Binary Sensor for this and use that as the trigger in Node-RED.

Can anyone help me with this?

entity_id event (off) -> X minute timer -> your other action
entity_id event (on) -> cancel timer ^

Don’t have my NR available, but it’s a pretty basic and common pattern for what you’ve requested from NR.

I can update later with more specifics later if requested.

1 Like

Can you help me with this when you have the time? I think I need to use the stoptimer. It states it can stop the timer with a payload STOP. But I don’t know how to set that up.

Doing this though NR is going to be clunky and hacky. There is no need to be constantly running a timer. Using binary_sensor and the state engine is the way to go. Try this:

- platform: template
  sensors:
    your_sensor:
      friendly_name: Your Sensor
      value_template: "{{ is_state('binary_sensor.trigger_sensor', 'off') and (states.trigger_sensor.last_changed < (now() - timedelta(minutes=10))) }}"

Replace trigger_sensor with your trigger sensor. The time delta should be how long ago the state changed for the sensor to now be true.

The format for timedelta is:
seconds = 30
minutes = 5
hours = 10
etc.

Edit: Fixed the operator.

4 Likes

It’s pretty simple once you get your head around Node Red basics, and not “clunky” or “hacky” using standard and basic operators.

  • Start with the Home Assistant Event node to monitor for changes to your binary_sensor
  • Just use a Switch node to split “on” vs “off”
  • the “on” (in your case) goes to a Change node and sets the msg.reset which will disable the delay from firing
  • Both of those point to a Delay node and not a Stoptimer.

Having said all of that, the Automation may be the way to go if you don’t need to do much else with that binary_sensor

1 Like

I suppose that works if you only need to trigger a flow once per time its turned off, but there is no way to check whether the delay was exceeded in subsequent runs of any flow/automation. I guess it depends on OP’s needs.

You would need something else counting up from there, true.
Supposedly OP wanted something else to trigger which could also be tracked :slight_smile:

Just use delay_on:

binary_sensor:
  - platform: template
    sensors:
      washing_machine:
        friendly_name: "Washing Machine"
        delay_on: "00:05:00"
        value_template: >-
          {{ states('sensor.washing_machine_power')|float > 0 }}
6 Likes

Thanks all! I see a great solution with both methods (binary sensor template and Node-RED). Binary sensor template is more robust, while the Node-RED method is flexible. I’ll test and see which one works best in this case.

I saw the documentation with delay, but was lost with defining the binary template state itself as code. The part about float got me confused I think this should work then judging by both template methods:

  - platform: template
    sensors:
      beweging_badkamer_cooldown:
        friendly_name: Beweging Badkamer Cooldown
        delay_on: "00:00:10"
        value_template: "{{ is_state('binary_sensor.beweging_badkamer', 'off') }}"   

The Node-RED method is also godsend as for other automations I have setup that I require. So thank all of you :smiley: !

1 Like

5 posts were split to a new topic: Help with a template sensor