Best way to get time since last motion from Zwave motion sensor

I’ve got a sensor that I use to decide when to run a fountain in my back yard. I have a template sensor that takes into account the temperature and motion on the back porch within the last 30 min.

It works great, except for the fact that I’m using the last_changed attribute of the Zwave motion sensor, which is updated when HA restarts. So the fountain always runs whenever HA restarts (as long as it’s warm enough).

I have a sensor that tells me HA’s uptime, but I don’t necessarily want to “lock” the fountain sensor for 30 minutes on each HA restart.

Is there a simple way to determine the last time that the motion was true, ignoring restarts? My alarm panel has a handy “last_tripped”. I’d like to avoid setting up a separate sensor just to track when the last motion state was since I use a similar technique for several other things (running fans when there’s motion in a room, etc)

My current sensor’s template looks like ( i added a few newlines here for readability…):

{% if states.binary_sensor.back_porch_motion_sensor %}
 {{ ( 
    as_timestamp(now()) - 
    as_timestamp(states.binary_sensor.back_porch_motion_sensor.last_changed) < 1800
    and states("sensor.back_porch_temperature") | int > 40 }}
{% else %}
{% endif %}

I do notice a couple of fields in my z-wave entity that may work. I have had my device off for a while.

  "sentTS": "2019-04-20 19:13:48:181 ",
  "receivedTS": "2019-04-20 19:13:48:213 "

I do not currently have a motion sensor to test with.

receivedTS will give you the last time that info was received from the device, but since it’s a multisensor, that could be temp or lux.

Horrible cludge:

Disable whatever automation uses this sensor for 31 minutes using:

    platform: homeassistant
    event: start

Far from ideal as it wont work for that long after a restart.

Maybe this would be better off as a direct automation trigger rather than a sensor. Trigger on your template but add the condition that the movement sensor must be on. Nope not what you want. You want it to work if triggered in the last 30 min.

Right - I could do that with my uptime timer (check to see that it’s over 30 min) but that’s what I’m trying to avoid. For now, I’m going to go with what you suggested in your removed message and have the “on” automation only happen when the motion sensor is “on”. I’d hoped to handle this all in the sensor, but a condition on the automation is acceptable.

It’s a really tricky one. I’m interested to see if anyone comes up with an alternative solution that allows for the light to be on if movement was detected in the last 30 min (+ temperature but that’s the easy bit) but not a restart.

The motion sensors associated with my alarm panel have a handy “last_tripped_time” attribute. It’d be nice if the binary_sensor created by zwave motion sensors had the same.

I haven’t been able to find a good way to do this either. For now I’ve just added another sensor, exactly what you want to avoid doing :wink: