Condition question: if 'not_home' state was older than sensor value

Hello everyone,

Sorry for the somewhat cryptic topic name. Not quite sure how to discribe my question.
I have a robot vacuum that cleans when noone is home. It sets a date value to a sensor when it has just that.
Now I’d like to have a TTS announcement when someone gets home. How do I code that with conditional templating? Not sure where to start because I don’t know where I could get such that state’s date/time value.

Here’s what I’ve got so far (yes I know trigger.for does not do what I want…) :

- alias: Report cleaning is done
  trigger:
    - condition: state
      entity_id: group.family
      state: 'home'
  condition:
    - condition: template
      value_template: '{{ (trigger.for) > (states.sensor.monica_last_cleaning.state) }}'
  action:     
    - wait_template: "{{ (is_state('binary_sensor.frontdoor','open')) or (is_state('binary_sensor.backdoor','open')) }}"
      timeout: '00:10:00'
      continue_on_timeout: 'true' 
    - delay: 30
    - service: script.turn_on
      entity_id: script.tts_monica_cleaned_away
  hide_entity: true

what is the purpose of the condition? It seems that no matter what, “now” (when someone gets home) will always be after the sensor date so it seems that the condition is moot.

Correct me if I’m wrong but what it sounds like you want is simply an announcement that the cleaning was done during the time that everyone was not home? Do you want the announcement to only be made if the cleaning was done between the time everyone was not home until someone was home?

And it looks like the indentation/order of items in the action section is kind of jacked up.

1 Like

Well, if we leave the house again for a second time that day, it will announce the cleaning again when we get home, while it didn’t. And maybe cleaning didn’t finish because we were away for a short amount of time (it will only save it cleaned after it did so for at least 40 minutes)

Yes you were right, I messed up some action formatting. Edited the code.

OK, I think I have an idea of what you want.

You only want it to report a cleaning that was actually completed and that was only completed during the most recent time that everyone was gone. If the cleaning announcement was already made (based on a prior completed cleaning) and then everyone leaves again then when someone returns don’t make the announcement if no cleaning was completed again in the interim. Is that about right?

If so what I would do is use an input_boolean to save the fact that a cleaning was completed but not announced and then use the completed announcement event to reset the input_boolean back to off.

So the flow would be like:

  • everyone leaves
  • a cleaning cycle is completed
  • turn on the input_boolean
  • someone comes home
  • check the state of the input_boolean, If it’s on then trigger the announcement that a cleaning was completed, then turn off the input_boolean.
  • if the input boolean is already off then don’t perform the announcement

that way if the input boolean is on then you know that a cleaning was completed but no announcement was yet made. So make the announcement.

If the input boolean is off then either no cleaning has been completed or the announcement has already been made for a completed cleaning. so don’t make an announcement.

1 Like

That actually makes perfect sense, thanks. I’ll go for that. Why haven’t I thought of that before, haha.