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.
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.
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.
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.
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.
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”