Binary Sensor last tripped time issue

Ok, so it isn’t just me that is having the issue. You’ve got quite a bit of configuration there, and to be frank, I’m not really sure what you are trying to accomplish with it. It appears you are basing it off last_tripped_time (which is a relative field that doesnt get set properly), but based on what I see in the logs, I don’t see how that will work, since last_tripped_time is pretty much always inaccurate when the door/sensor is in fact open.

Can you somehow call the exact time that the sensor first tripped? Based on my log entries, looks like if you can call this time, and then do math based on the exact time it could work. Essentially if we grab that the door was opened at 20:34:00 and it is now 20:44:00, we can calculate that the door has been open for 10 minutes. Any thoughts on how to do that? Are those fields even accessible from the configuration?

open time provided in logs:

device_class=door @ 2018-04-23T20:34:06.365900-04:00

close time from logs:

device_class=door @ 2018-04-23T20:52:54.775233-04:00

I use envisalink 3 and 4 on two different HA’s. I tend to use trigger for automation and scripts but use last_changed for binary_sensors. This is one for a envisalink binary sensor that might work for you.

This script opens the garage only if the door leading to the garage hasn’t been open for 360 seconds. It tests whether we are arriving or leaving as we won’t be opening that door if we are arriving.

  open_garage_arrival:
    sequence:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.garage
            state: 'off'
          - condition: template
            value_template: '{% if states.binary_sensor.mud_room_door.last_changed  %}{{(as_timestamp(now())-as_timestamp(states.binary_sensor.mud_room_door.last_changed)) > 360 }}{% else %}True{% endif %}'
      - service: alarm_control_panel.envisalink_alarm_keypress
        data:
          entity_id: 'alarm_control_panel.home_alarm'
          keypress: '*74'
      - service: mqtt.publish
        data:
          topic: "rpi1/input_text/saythispi/state"
          payload: "I am opening the garage door"
      - service: logbook.log
        data_template:
          name: Garage
          message: RPi1**********open**********garage*********sciprt

This seems like it could be promising, but doesnt look like you can reference last_changed in a trigger. Am i missing something?

Or maybe I need an automation to call a script to do the condition? Seems like high overhead to constantly do that though.

You are correct, I’m working based on the last tripped time - which, BTW, seems to have a max. value that translates to something like 3 days and 19 hours if I remember correctly.

And this is what I display in my dashboard:
image

That’s sufficient for what I need.

If you want to show the time when e.g. your front door was last opened I could imagine the following approach:

  • create a datetime input variable for both actions, i.e. one for front_door_opened and one for front_door_closed
input_datetime:
  front_door_opened:
    name: Front Door Last Opened
    has_date: true
    has_time: true
  front_door_closed:
    name: Front Door Last Closed
    has_date: true
    has_time: true
  • push the current date and time to the respective variable whenever the door opens and closes
- alias: Front Door Last Opened
  trigger:
    platform: state
    entity_id: "YOUR ENVISIALINK SENSOR HERE"
# might be the other way round
    from: 'off'
    to: 'on' 
  action:
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.front_door_opened
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'

  • display these variables on your dashboard

Maybe you could extract if from the log file as well, not sure if it’s possible and if it would be any easier.

1 Like

This is helpful. Essentially, we’re storing the last opened time into input_datetime.front_door_opened. From there I would imagine you can reference that in an automation…

if now() - input_datetime.front_door_opened > 10 minutes, trigger automation.

if now() - input.datetime.front_door_opened < 10 minutes, ignore.

Just a matter of figuring out how to put that into an automation and I think that could work around this annoying problem with these binary sensors.

Take a look at what @will has to say here:

I could never get it to work as a trigger directly but it seems to work for him somehow, hope it does for you, too.

This is a template trigger for an automation that will trigger when the envisalink mud room door binary sensor has changed state 360 seconds ago.

automation:
  trigger:
    platform: template
    value_template:  {% if states.binary_sensor.mud_room_door.last_changed  %}{{(as_timestamp(now())-as_timestamp(states.binary_sensor.mud_room_door.last_changed)) > 360 }}{% else %}True{% endif %}
1 Like

That still doesn’t work. It appears that it triggers that automation the first time that envisalink is telling HA that a window is open, and its ignoring the code you’ve provided @RobDYI

  - alias: Window left open longer than 1 minute
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.office_windows
        from: 'off'
        to: 'on'
      - platform: template
        value_template: '{% if states.binary_sensor.office_windows.last_changed  %}{{(as_timestamp(now())-as_timestamp(states.binary_sensor.office_windows.last_changed)) > 60 }}{% else %}True{% endif %}'
    action:
    - service: notify.file1
      data_template:
        message: |
         {{now().strftime("%a %b %d %H:%M:%S %p")}} Windows has been open for a minute1

If I understand it correctly, the way you coded it, it treats your triggers as ‘or’ conditions, you might want to re-code it to make it an ‘and’ condition.

  - alias: Window left open longer than 1 minute
    trigger:
      - platform: template
        value_template: '{% if states.binary_sensor.office_windows.last_changed  %}{{(as_timestamp(now())-as_timestamp(states.binary_sensor.office_windows.last_changed)) > 60 }}{% else %}True{% endif %}'
    condition:
      condition: state
      entity_id: binary_sensor.office_windows
      state: 'on'

This is just like state trigger for 1 minute.

@RobDYI - We are getting closer.

If I open the window, one notification takes place immediately upon opening the window, and then another notification happens after 60 seconds. Technically it’s a little after 60 seconds - at least 60 seconds plus the delay to get the next reading from the envisalink.

How do we get rid of the action notification from the initial state when we go from off to on? I only want this to take action when it has been open for 60 seconds.

and this doesn’t work?

- alias: Window left open longer than 1 minute
  trigger:
    platform: state
    entity_id: binary_sensor.office_windows
    to: 'on'
    for:
      minutes: 1

We are almost there, but it fires off alerts twice once the trigger happens. Might be a result of the zonedump_interval f the Envisalink, which is currently set to an interval of 15. I wonder if perhaps I need to adjust 300 seconds to be just over 300 or just under 300, since perhaps that is getting in the way here.

Here is my current automation code.

  - alias: Window left open longer than 5 minute
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.office_windows
        from: 'off'
        to: 'on'
        for:
          minutes: 5
      - platform: template
        value_template: '{% if states.binary_sensor.office_windows.last_changed  %}{{(as_timestamp(now())-as_timestamp(states.binary_sensor.office_windows.last_changed)) > 300 }}{% else %}True{% endif %}'
    condition:
      condition: state
      entity_id: binary_sensor.office_windows
      state: 'on'
    action:
    - service: notify.file1
      data_template:
        message: |
         {{now().strftime("%a %b %d %H:%M:%S %p")}} Windows has been open for 5 minutes!!

Log file (relevant lines):

2018-04-25 09:22:40 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.office_windows, old_state=<state binary_sensor.office_windows=off; last_tripped_time=36325, friendly_name=Office Windows, device_class=window @ 2018-04-24T23:17:46.365349-04:00>, new_state=<state binary_sensor.office_windows=on; last_tripped_time=0, friendly_name=Office Windows, device_class=window @ 2018-04-25T09:22:40.756377-04:00>>

2018-04-25 09:27:39 INFO (MainThread) [homeassistant.components.envisalink] Envisalink sent a zone update event. Updating zones…

2018-04-25 09:27:39 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.office_windows, old_state=<state binary_sensor.office_windows=on; last_tripped_time=10, friendly_name=Office Windows, device_class=window @ 2018-04-25T09:22:40.756377-04:00>, new_state=<state binary_sensor.office_windows=on; last_tripped_time=15, friendly_name=Office Windows, device_class=window @ 2018-04-25T09:22:40.756377-04:00>>

2018-04-25 09:27:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=Window left open longer than 5 minute, message=has been triggered, domain=automation, entity_id=automation.window_left_open_longer_than_5_minute>

2018-04-25 09:27:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.window_left_open_longer_than_5_minute, old_state=<state automation.window_left_open_longer_than_5_minute=on; last_triggered=2018-04-24T22:25:17.068872-04:00, friendly_name=Window left open longer than 5 minute @ 2018-04-24T21:55:58.141402-04:00>, new_state=<state automation.window_left_open_longer_than_5_minute=on; last_triggered=2018-04-25T09:27:41.204928-04:00, friendly_name=Window left open longer than 5 minute @ 2018-04-24T21:55:58.141402-04:00>>

2018-04-25 09:27:52 INFO (MainThread) [homeassistant.components.envisalink] Envisalink sent a zone update event. Updating zones…

2018-04-25 09:27:52 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.office_windows, old_state=<state binary_sensor.office_windows=on; last_tripped_time=15, friendly_name=Office Windows, device_class=window @ 2018-04-25T09:22:40.756377-04:00>, new_state=<state binary_sensor.office_windows=on; last_tripped_time=10, friendly_name=Office Windows, device_class=window @ 2018-04-25T09:22:40.756377-04:00>>

2018-04-25 09:27:52 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=Window left open longer than 5 minute, message=has been triggered, domain=automation, entity_id=automation.window_left_open_longer_than_5_minute>

2018-04-25 09:27:52 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.window_left_open_longer_than_5_minute, old_state=<state automation.window_left_open_longer_than_5_minute=on; last_triggered=2018-04-25T09:27:41.204928-04:00, friendly_name=Window left open longer than 5 minute @ 2018-04-24T21:55:58.141402-04:00>, new_state=<state automation.window_left_open_longer_than_5_minute=on; last_triggered=2018-04-25T09:27:52.309112-04:00, friendly_name=Window left open longer than 5 minute @ 2018-04-24T21:55:58.141402-04:00>>

I think all you need is this

- alias: Window left open longer than 5 minute
  trigger:
    platform: state
    entity_id: binary_sensor.office_windows
    to: 'on'
    for:
      minutes: 5
   action:
     - service: notify.file1
       data_template:
         message: |
          {{now().strftime("%a %b %d %H:%M:%S %p")}} Windows has been open for 5 minutes!!

I think this finally works.

This is pretty annoying though, since I spent so much time troubleshooting this for such a stupidly small issue in the automation code.

I was using this as reference:
https://community.home-assistant.io/t/left-open-automation/3688

Only difference that really had to be done was instead of saying "state: ‘on’ " I needed to use "to: ‘on’ " to account for the way that the Envisalink doesn’t use last_tripped_time properly.

Thanks for all the help from everyone!

Although, I would like to be able to alert on multiple windows being opened for 5 minutes independent of others.

If I change it from

entity_id: binary_sensor.office_windows

to

entity_id: 
- binary_sensor.office_windows
- binary_sensor.guest_windows

it will require that both the office_windows AND guest_windows are opened for 5 minutes. How do i automate this where it will view it as an OR condition instead of an AND condition?

I thought triggers are normally ‘or’ by default, maybe it has to do with your ‘for 5 minutes’ entry.

First thing I would try is to use:

entity_id: binary_sensor.office_windows, binary_sensor.guest_windows

Or, it might be as simple as:

    - entity_id: binary_sensor.office_windows
      to: 'on'
      for:
        minutes: 5
    - entity_id: binary_sensor.guest_windows
      to: 'on'
      for:
        minutes: 5

If neither works you might want to take a look here:

Originally, I was thinking about doing something similar but then I noticed the odd behavior I described above, i.e. when I have many windows/door open the Envisalink reports them as being closed for a while and then being open again without the windows/doors having been touched at all. If that happens in your setup as well it might screw with your automations.

Would be interested so see how it goes, though.

I know this is a little off topic based on the final solution but since something I wrote earlier was mentioned, I thought I chime in.

It is inconsistent and I’ve moved away from using it because it works fine with some states and not with others but you really don’t know which state it will work with.

For example, using it with the sun state, the now() comparison works great and has been for two months.

This works fine:

{{ (( as_timestamp( states.sun.sun.attributes.next_dusk ) | int ) - 180) < ( as_timestamp( now() ) | int ) }}

However, when I went to build a data/time drop down selection that would then be used to take my Nest out of vacation mode, now() didn’t work. I ended up having to create a date_time sensor as outlined under Time & Date.

This is what I used to trigger my Nest out of vacation mode:

{{ ( states.input_datetime.nest_away_end.attributes.timestamp | default(0) | int | timestamp_custom("%Y-%m-%d, %H:%M", True) ) == states.sensor.date__time.state }}

It is hard to guess what exactly is going because I am not familiar with the source code.

I have no problem with multiple entities in the state trigger although my spacing is different.

image

I use it in different automations but I know this one works

automation basement_hall_movement:
  - alias: basement_hall_movement
    trigger:
      - platform: state
        entity_id:
         - binary_sensor.basement_hall_motion
         - binary_sensor.exercise_rooom_motion
        to: 'on'
      - platform: state
        entity_id:
         - binary_sensor.basement_hall_motion
         - binary_sensor.exercise_rooom_motion
        to: 'off'
        for:
          minutes: 2
    action:
      service_template: '{% if trigger.to_state.state == "on" %}homeassistant.turn_on{% elif trigger.to_state.state == "off" %}homeassistant.turn_off{% endif %}'
      entity_id: light.basement_hall_light

Revisiting this again, as life got in the way.

I tried what @RobDYI showed with the multiple entity_id records, but no dice.

Here is what I have. It will only fire off if BOTH binary sensors go from closed to open. If I try to list entity_id twice under the platform: state field, it tells me that entity_id is duplicated. Any more ideas?

#Door left open longer than 5 minutes
  - alias: Window or Door left open longer than 5 minute
    trigger:
      - platform: state
        entity_id: 
         - binary_sensor.carport_door
         - binary_sensor.office_windows
        to: 'on'
        for:
          minutes: 5