Trigger TTS on motion and tell me how long since last (previous) motion was detected

Hi Guys,

I’m a total newbie. I’m trying to get the time since last motion was detected. The tricky part is… I want this to be triggered by that motion sensor.
In other words when the motion will be activated I would like to get TTS saying how long ago the previous motion was detected, something like: ‘last motion was detected 4 hours ago’.

My current code is:

- id: '16185643614'
  alias: czujka1
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 21dfc4fdc4ff89de968023ef36dc28
    entity_id: binary_sensor.czujka1_ruch
    domain: binary_sensor
  condition: []
  action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.sypialnia_speaker
      message:  Last motion detected {{ relative_time(states.binary_sensor.czujka1_ruch.last_changed) }}  ago
  mode: single

but that automation returns ‘Last motion detected 0 seconds ago’ since the trigger… changes the last_changed to current timestamp. How can I get the time between now and previous state change?

1 Like

not solution to your code but a suggestion :slight_smile:
? why dont you use -service: tts.google_say
you would need to store the last_trigger in a diferent sensor, as the trigger indeed was triggerred 0 seconds ago .

1 Like

have the automation set an input_datetime to now() as the last step of the actions.

then you can call the value of the input_datetime in the announcement for the “ago” part.

then the input_datetime is updated to the new now()

- id: '16185643614'
  alias: czujka1
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 21dfc4fdc4ff89de968023ef36dc28
    entity_id: binary_sensor.czujka1_ruch
    domain: binary_sensor
  condition: []
  action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.sypialnia_speaker
      message:  Last motion detected {{ relative_time(state_attr('input_datetime.last_motion', 'timestamp')) }}  ago
  - service: input_datetime.set_datetime
    entity_id: input_datetime.last_motion
    data:
      timestamp: "{{ now().timestamp() }}"
  mode: single

But I’m not sure if relative time can work with a timestamp. So you can adjust that part as needed.

Thanks @finity! I have tried proposed solution, however I get ‘Last motion NaN ago’ and in the logs I can see:

2021-04-18 10:22:04 WARNING (MainThread) [homeassistant.helpers.service] Unable to find referenced entities input_datetime.last_motion

I thought maybe it’s because of the order - I first call the service to read state of entity_id: input_datetime.last_motion and after that I call the service to actually set it up - therefore I got NaN.
And I know the order is important, since I want to read timestamp of previous executions; But just for testing I switched the order like that:

action:
  - service: input_datetime.set_datetime
    entity_id: input_datetime.last_motion
    data:
      timestamp: "{{ now().timestamp() }}"
  - service: tts.google_translate_say
    data:
      entity_id: media_player.sypialnia_speaker
      message: Last motion {{ state_attr('input_datetime.last_motion', 'timestamp') }} 

I still got the same warning in the logs. - Unable to find referenced entities input_datetime.last_motion

Since this service create a new entity with entity_id: input_datetime.last_motion - Should I be able to find it in Developers Tools -> States -> Entities and be able to read it’s state and attributes?

Have you first created this entity?

1 Like

@sparkydave thanks! That’s it!
I’ve added to configuration.yaml:

input_datetime:
  last_motion:
    name: last_motion
    has_date: true
    has_time: true

Rebooted and… It works like a charm!

Sorry, I guess I should have been more explicit that you needed to create the input_datetime entity first.