Was the doggy automation ever triggered before?
It has been triggering throughout the day, dog goes in and out constantly.
Wait, are you talking about a automation condition or a template sensor? And what piece of code are you using where?
Yes, it would be a template sensor, that then would be used as an automation condition.
I’m still very new at this so please forgive my errors, but I believe it would look along the lines of
- platform: template
sensors:
doggy_door_pressure_mat_last_trigger:
friendly_name: Doggy Mat Last Trigger
value_template: >-
{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}
icon_template: mdi:timer-alert
The indentation is not correct, it has to be 2 tabs:
- platform: template
sensors:
doggy_door_pressure_mat_last_trigger:
friendly_name: Doggy Mat Last Trigger
value_template: >-
{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}
icon_template: mdi:timer-alert
Thank you. I’ve made the correction to what you had. However I’m still getting an “unavailable” for the State. And looking at the template editor I’m getting an error.
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
If you are creating new Template Sensors you should consider using the modern format of configuration as opposed to the example you posted which is in legacy format.
template:
- sensor:
- name: Doggy Mat Last Trigger
state: "{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}"
icon: mdi:timer-alert
Agree, but does this explain why he is getting the error note? @Blitzzz13 , are you shure that your automation is named automation.open_doggy_door_with_mat
?
The problem isn’t due to the format (but given that modern format has certain advantages over legacy format, it’s advisable to employ modern format when the opportunity arises).
The problem is that the state_attr()
function is producing none
. It’ll do that if the automation’s last_triggered
has no value or the specified automation’s entity_id is incorrect (i.e. it doesn’t exist).
Given that the automation has allegedly triggered several times (therefore it has a last_triggered
value), the problem must be due to an incorrect entity_id.
Hi! Again thank you both for the immense help, I know I’m dense here. I do greatly appreciate you both.
I’ve adjusted the code based on the modern format and I’ve renamed the automations to ensure they match (which they were previously).
The automation entity_id is as follows: automation.open_doggy_door_via_mat_button
Here is the new code as suggested by @123:
template:
- sensor:
- name: Doggy Mat Last Trigger (Button)
state: "{{ now() - state_attr('automation.open_doggy_door_via_mat_button', 'last_triggered') > timedelta(seconds=15) }}"
icon: mdi:timer-alert
The code in the file editor seems to accept it, however now I do get the following error when I check the configuration:
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 13).
And here is the configuration.yaml code(line 13 being the template line):
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
#Yaml File Locaitons##
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template: !include templates.yaml
sensor: !include sensors.yaml
Delete template:
in your templates.yaml.
This template approach is way to complicated. Create a timer helper. Be sure to set the timer option to continue the timer when HA restarts.
When you see motion, start or restart the timer with the required wait time. At any time you want you can test as a condition that the timer should not be running. The upside: you can easily see if the timer is running, do something if the timer ends, cancel the timer if it is no longer needed, or restart the timer to the original length.
Last July, I had suggested to create a Template Condition in the automation (to prevent the automation from triggering more than once within a given time period). Is there a particular reason why you have decided to create a “custom sensor”, with the suggested template, instead of a Template Condition?
What do you get when you put this {{ now() - state_attr('automation.open_doggy_door_via_mat_button', 'last_triggered') > timedelta(seconds=15) }}
in Dev Tools, Templates?
That aside, here is how I broke out my templated entities:
configuration.yaml
:
template: !include_dir_merge_list templates/
Hope that helps!
Hi! I also have an automation with an delay in the end, but it still triggers.
My goal was only to close it once a day. But if it is closed to 40% and you move it to another position it still triggers if the temperature is in the right range.
So the delay seems not to work properly.
Any ideas what is wrong ?
alias: close roller shutter
description: ""
trigger:
- platform: state
entity_id: sensor.temperature
condition:
- condition: time
after: "08:30:00"
before: "18:00:00"
- condition: or
conditions:
- condition: state
entity_id: sensor.season
state: spring
- condition: state
entity_id: sensor.season
state: summer
- condition: numeric_state
entity_id: sensor.temperature
above: 22.5
below: 23
action:
- device_id: b24ae8d6dc40efa9cff1bdcf687f9331
domain: cover
entity_id: cover.shelly_shsw_25_40915145e7cd
type: set_position
position: 40
- delay:
hours: 10
minutes: 0
seconds: 0
milliseconds: 0
mode: single
When is last_triggered updated? I assume at the completion of the run?
To flesh out my question, I have a condition inside an automation. I only want a couple of actions to run if its been more than 5 minutes since the last time the automation ran. I’m using this from this thread as a condition:
{{now() - state_attr('automation.presence_home_mode_home_away', 'last_triggered') > timedelta(hours=0, minutes = 5)}}
However, in the automation it evaluates as false. I’m assuming that’s because last_triggered
gets set immediately. If that’s the case, is there a way to do this with templates alone and without creating an external helper?
The value of an automation’s last_triggered
attribute is updated when the automation begins to execute its action
section.
Therefore the Template Condition posted above is meant for use in an automation’s condition
section (where it’s evaluated before last_triggered
is updated).
You can’t use it in the action
section because at that point last_triggered
has been updated so its new value is merely a few microseconds prior to now()
.
Any way to do it with a template? I really don’t want to add another helper. Maybe more than 5 minutes, but less than 5 seconds or something?
edit: Not sure of an easy way to test it, but would this work:
{{ (now() - state_attr('automation.presence_home_mode_home_away', 'last_triggered') > timedelta(minutes = 5)) or (now() - state_attr('automation.presence_home_mode_home_away', 'last_triggered') < timedelta(seconds = 2))}}