Help with "Feed the dog" automation - how to use condition to look for "no movement last 3 hours"?

I’m setting up a reminder system so that if we forget to feed the dog in the morning or evening, we’ll be alerted. Home Assistant 2021.12.9 on Raspberry Pi 4.

I’ve added a helper input boolean input_boolean.dog_needs_food which turns on at 9am and 4pm using an automation. It should detect if we’ve fed the dog in the past 3 hours by looking for movement on a multi-sensor mounted to the dogfood container (binary_sensor.dogfood_container_accelerometer).

However, at 9am the trace shows that it fails on the condition that the dog food container sensor hasn’t moved in the past 3 hrs…even though I confirmed in LogBook for this sensor it hasn’t moved for 17 hours (last entry shows “Dogfood Container accelerometer turned off (17hrs ago)”).

I must be using the wrong method in my automation to detect that we’ve already fed the dog, or my syntax is incorrect (though I used the GUI for this one). Screenshot of failing trace is included below.

(Not shown here, but I have an alert to look/notify for an “on” dog_needs_food input boolean for more than 30 min. I haven’t gotten to that testing yet since the automation doesn’t yet work.)

Thanks for the help!

- id: '1644452151182'
  alias: DogFed - Alert
  description: ''
  trigger:
  - platform: time
    at: '09:00:00'
  - platform: time
    at: '16:00:00'
  condition:
  - type: is_not_moving
    condition: device
    device_id: f7262518deddf2ad9596a5013af505ec
    entity_id: binary_sensor.dogfood_container_accelerometer
    domain: binary_sensor
    for:
      hours: 3
      minutes: 0
      seconds: 0
  action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.dog_needs_food
  mode: single

Trace shows:

I don’t like device based conditions, you can better rely on state condition as;

platform: state
entity_id: binary_sensor.xyz
to: 'off'
for: '03:00:00'

I’ll give that a try, @anon63427907. Is there a way somewhere in HA to test if the condition would be true or false? Meaning, test a given set of YAML state condition.

As mentioned it’s better to use state rather than device.

You could also consider using a template condition, something similar to this should work…

{{is_state('binary_sensor.dogfood_container_accelerometer', 'off')
and (states.binary_sensor.dogfood_container_accelerometer.last_changed < (now() - timedelta(minutes=180))) }}

I don’t have any accelerometers so don’t know what state they report but assuming it’s on or off then the above should work.

Also the benefit in answer to your follow up question is you can paste the above on to developer tools templating section and check if it reports true or false.

You can use developer tools. I did the test like this

{{ (now() - states.light.bar_pendant_light.last_changed).seconds > 10800 }} 

Also you need to be aware that a restart or reload of automations will kill any time based automation/thing that is running at the time of restart.

so if you happened to restart or reload automations during that three hours that condition will fail.

Learned that today too. Great point to bring up!

Perfect @AllHailJ ; dev tools template editor is what I was looking for to test out different code and conditions thx!