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.
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.
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.
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.
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