Sensor last_changed as variable

I need some help with my script. I want to determine how long the window has been open using last_changed. However, I don’t want to enter a fixed sensor like binary_sensor.kitchen_window but a placeholder that recognizes which sensor (window) has triggered and how long it has been open.
I hope I have expressed myself reasonably understandable, my English is rusty.


alias: Fenster offen Außentemperatur unter 10 Grad
sequence:
  - service: sonos.snapshot
    data:
      entity_id: "{{ entity }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    target:
      entity_id: "{{ entity }}"
    data:
      media_content_id: /local/Sonos/Gong.mp3
      media_content_type: music
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
  - service: media_player.volume_set
    data:
      volume_level: 0.2
    target:
      entity_id: "{{ entity }}"
  - service: tts.cloud_say
    data:
      message: >-
        Das {{ fenster }} ist seit {{ (as_timestamp(now()) -
        as_timestamp(states.????????.last_changed)) |
        timestamp_custom ('%M Minuten',false) }} geöffnet.
      entity_id: "{{ entity }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
  - service: sonos.restore
    data:
      entity_id: "{{ entity }}"
mode: single
icon: mdi:window-open-variant


Greetings
Moss

Great, thank you very much for your help.

Greetings
Moss

Unfortunately, it does not work for me. My sensors always contain the text “fenster” so I logically replaced window from your example with fenster. Up to my gong (see above my code) is played but the actual message is not. Did I do something wrong or misunderstood?

Here are a few examples of my sensors.

binary_sensor.ankleidezimmer_fenster_rechts

binary_sensor.badezimmer_fenster

binary_sensor.buro_fenster

binary_sensor.kuchenfenster_links


      message: >-
        Das {{ fenster }} ist seit {{ (now().timestamp() - states[ fenster
        ].last_changed.timestamp()) | timestamp_custom('%M Minuten', false) }}
        geöffnet.

Thanks
Moss

Does the fenster variable return the sensor’s entity_id, “friendly” name, or something else?

It would be helpful if you would share the automation or script that is firing this script so we can see where the variables you are passing are coming from.

Also, be aware that timestamp_custom('%M Minuten', false) will return inaccurate results if the time sensor’s last change was greater than an hour ago.

The variable fenster is included in the entity_id and friendly name or what do you mean?

I’m aware of the time over an hour thing, but thanks for pointing it out anyway.

example:

entity_id: binary_sensor.kinderzimmer_fenster_rechts

friendly_name: Kinderzimmer Fenster Rechts

Greetings
Moss

I had a thinking error, if I take the binary_sensor as trigger in the automation and this is recognized as opened I can enter supplementary opened since 5 minutes.
Then the last_changed in the script is absolutely superfluous since it is logically always 5 minutes and I can change the message to “Opened since 5 minutes”.
Sorry for the effort only because I thought wrong.

Greetings
Moss

Works fine now.

For my understanding, would you still explain to me what was meant by the variable?

Thanks
Moss

In your message block you have a variable {{ fenster }} that is undefined in the script, so we assume it is being passed from somewhere else. Without seeing that source, or you telling us what specific information is being passed, we can only guess the contents.

Ok now I know at least what is meant. I have created this script to select this by automation, trigger is always another window which is open for some time.
This works with my following automations also perfectly, on the basis of the variable is recognized which window is open. Also I can select the voice output by variable “{{ entity }}” differently. Now I had the idea to choose a variable for the time period where the window is open, just for the purpose if I would choose different time periods in my automations. But there was my thinking error, I have to set a time since otherwise nothing is triggered.
Here is my automation for clarity.


alias: "TTS: Fenster WC offen und Aussentemperatur ist niedrig"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.wc_fenster
    id: Fenster WC offen
    to: "on"
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: numeric_state
    entity_id: sensor.temperature_bingen_buedesheim
    below: 11
  - condition: state
    entity_id: climate.wc
    state: "off"
action:
  - service: script.fenster_offen_trotz_niedriger_aussentemperatur
    data:
      fenster: "{{ trigger.to_state.attributes.friendly_name }}"
      entity: media_player.esszimmer_sonos
mode: single

Greetings
Moss