How to make a direction sensor

Spitball’n for how I’d create a direction sensor e.g. Sensor 1 on then sensor 2 on; out. Sensor 2 on then sensor 1 on; in. Anyone got any thoughts on some code to do that?

Going to attempt to make my Sureflap door a bit smarter by adding a second sensor that will allow me to determine whether the cat went in or out but has many other possible use case scenarios.

I’d say this should be quite possible using the last_changed times in a template.

You could create an input_select and have it switched between ‘in’ or ‘out’ based on which sensor was triggered before the other.

I’ve actually been planning to use similar logic to predict peoples movements inside my house and turn lights on ahead of them rather than waiting for the motion sensor in the room they are walking to to detect them.

EDIT: as per finity’s comment, I had mistakenly said ‘last_triggered’ rather than ‘last_changed’. Fixed my post so not to cause confusion later.

Thanks, that makes sense and yes, I’ve had that use case in mind too. Based on which sensor triggered first, are they walking down the hall or up the hall? Very useful for automation prediction.

Let me know if you dream something up as coding is not my strength.

Some motion sensors have that feature.
That they can see if you walk towards it of away from it.

Not sure if they exist in the type “we” want, but the one we have at work is probably 10-15 year old wired version.

Probably a counter (step +) and (step -), two sensors as a gateway, could be used. I’m also interested and looking for information because I don’t know where to start. The problem is that if one sensor does not detect the object, the counter will shift and stop working unless it is manually reset.

I think that @sparkydave might be on to it. I’m trying to figure out how to compare two last_triggered values with < >. Looking into that this evening, I see that only my scripts and automations report last triggered so not sure how to extract that attribute from a sensor.

If you could just say if sensors A’s trigger time is < (or >) than sensor B’s trigger then…

If you have sensor 1 as the trigger, then action is wait for second sensor with a short timeout.
If the second sensor also trips the the action in the wait for is the switch in/out.
Or light on.

Agreed. I’ve also thought about that as I use a couple of wait templates now and they are very handy.

Here’s some food for thought. This returns true if the first input turns on before the second.

"{{ states('input_boolean.cat_door_state') > states('input_boolean.cat_door_aux_sensor')}}"

And conversely, this works the other way (or simply reverse the <). Each line briefly reports true only when the correct sensor is turned on first.

"{{ states('input_boolean.cat_door_aux_sensor') > states('input_boolean.cat_door_state')}}"

Thinking about my cat door scenario, the problem I’ll have is that the sensor built-in to the Sureflap only updates based on the scan_interval which by default is 3 minutes. I’ve set mine to 1 minute but that still ain’t going to cut it. I’ll need two real time sensors in order to detect cat direction.

So the door only says “opened” not in which direction?

What about a zigbee device or ESP in deep sleep that notice when the door opens inwards?

That way door opened = out.
Door opened and [other devices triggered] = in.

There is no last_triggered for a sensor.

But there is last_changed and last_updated.

so try something like this:

input_text:
  cat_direction:
    name: Cat Direction

automation:
  - alias: direction
    trigger:
      - platform: state
        entity_id: sensor.cat1
        to: 'on'
      - platform: state
        entity_id: sensor.cat2
        to: 'on'
    action:
      - service: input_text.set_value
        data:
          entity_id: input_text.cat_direction
          value: >
            {% if as_timestamp(states.sensor.cat1.last_changed) > as_timestamp(states.sensor.cat2.last_changed) %}
              in
            {% else %}
              out
            {% endif %}

Now you will have an input_text that has a state of ‘out’ or ‘in’.

There might be some tweaking you need to do because I don’t know what the failure points are (cat triggers one sensor then turns around and never triggers the other sensor, …) but it might get you started.

2 Likes

Thank you! That will definitely get me started and be very useful for others wanting direction determination.

1 Like

Further to this, do you know how I’d pull status (outside/inside) and the last time from this attribute data? My templating skills are rubbish and I’ve been trying different scenarios for over an hour without success.

Wife would like a sensor that reports ‘cat’ is (outside/inside) since xxx and that data is presented below but I can’t seem to figure out the template for that.

<template TemplateState(<state binary_sensor.pet_heston=off; since=2021-03-17 05:47:24, where=Outside, friendly_name=Pet Heston, device_class=presence @ 2021-03-17T18:47:53.675843+13:00>)

try:

cat is {{ state_attr('binary_sensor.pet_heston', 'where') }} since {{ state_attr('binary_sensor.pet_heston', 'since') }}

Thank you! I just cracked this too.

Heston is {{states('sensor.heston_status')}} as of {{relative_time(states.sensor.heston_status.last_changed)}} ago
1 Like

I actually like yours better but how can I display the time only in local time? It’s showing GMT at the moment (13hrs out). “Heston is Outside since 2021-03-17 05:47:24

Should say “Heston is Outside since 18:47”

cat is {{ state_attr('binary_sensor.pet_heston', 'where') }} since {{ as_timestamp(state_attr('binary_sensor.pet_heston', 'since')) | timestamp_custom('%H:%M') }}

I think that should convert it to local time.

Thanks but no. Still GMT.

Got it! Pulled from the other sensor.

cat is {{ state_attr('binary_sensor.pet_heston', 'where') }} since {{ as_timestamp(states.sensor.heston_status.last_changed) | timestamp_custom('%H:%M')}}

I’m keen to see your final solution in full. I might do the same for a future dog I don’t have.