Wait template using automation last_triggered attribute

My Intent: I am trying to get my washing machine to tell me its in its final spin cycle. I’m using a switch that measures power consumption. When the wattage spikes its in its final spin cycle. The problem is that the wattage can fluctuate in the spin cycle and repeated final spin warnings occur. I’m using numeric state in the automation. I looked for and found a solution here trigger once. Essentially using the last triggered attribute of the automation to stop it from running again in a certain period of time.

My problem In reading this thread I see that is possible to get none/null received when trying to retrieve a time_stamp attribute for an automation for instance on restart of HA. I guess timestamp doesn’t deal well with receiving back null/none and this disables the automation. As per the thread there is code for dealing with this situation essentially making the value false but when I have it in the automation it will not run. When I take it out it runs. I feel like maybe I’m missing and/or logic but I’m not grasping it.

I was just using a motion sensor to test things waving wildly at it …at least I’m making friends with the neighbors

My Automation:

  alias: testing testing wait
  trigger:
  - entity_id: binary_sensor.office_hue_motion_motion_sensor
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ states.automation.testing_testing_wait.attributes.last_triggered == none }}'
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.testing_testing_wait.attributes.last_triggered) | int > 60 }}'
  action:
  - data:
      entity_id: media_player.unisonic_3
      message: hopefully this only goes off once every minute
    service: tts.google_say

The way you have the condition the value of last_triggered has to be ‘none’ and the time between now and last_triggered has to be > 60 seconds. That will never be true.

I think you mean that you want the last_triggered to not be ‘none’. And you don’t need to force the int because a timestamp is already a number.

condition:
  - condition: template
    value_template: '{{ states.automation.testing_testing_wait.attributes.last_triggered != none }}'
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.testing_testing_wait.attributes.last_triggered) > 60 }}'

I’ve done the same for my dryer, dishwasher and washing machine but instead I’ve gone a different direction and used known modes (e.g. rinse, spin, wash, dry), and push them through these modes in only a certain direction using automations.

E.g. washing machine can only do the following:
Switched off -> Idle -> Rinse/Spin -> Wash -> Rinse/Spin -> Idle -> Switched off
it can also do a rinse/spin without a wash
Switched off -> Idle -> Rinse/Spin -> Idle -> Switched off

Each state is defined by a the first time it sees a set power draw amount for a certain amount of time. To date, its been 99% reliable and alerts within 1 minute of the appliance finishing.

Thanks for that. Pardon my ignorance but say the last_triggered does return ‘none’ does that mean the automation would fire? Or is this where I need the “and” logic so both templates need to come out true for this the automation to fire. Sorry for this last I was learning by using the automations editor and looking at the automation.yaml file but in this case it wont accept text into the template box. The automations page does describe conditions but it is a bit sparse. I’m sort of picking things up as I go and Im guessing your telling me I dont need to force the int…eger but that is something I still have to learn

thanks @ callifo maybe a little crawling before I walk

No, conditions aren’t triggers.

A trigger waits for a change in state to some specified state. And you can also specify a from state but that usually isn’t necessary.

A condition checks the current state of something.

if the trigger is set to true then at that single moment in time the condition (or conditions) is checked to see if it is also true. If not then the action won’t occur.

in this case the answer is yes. both conditions need to be met in order to run the action. so, both condition 1 and condition 2 must already be true at the moment that the trigger turns true. You can also have ‘or’ logic which means that if either condition 1 or condition 2 is true at the moment the trigger goes true then the action is performed.

By default, conditions are always ‘and’ logic. So the way your conditions are written they are ‘and’ without needing to specify that.

in this case the conditions are looking for last_triggered to not be ‘none’ and last_triggered to be greater than 60 seconds before ‘now’.

Most of the states in HA are actually strings. Some are date_time objects. And some are actually numbers not just strings that contain numbers. You can’t really compare a string to a number so many times you would have to convert the string (example ‘60’) into a number by using the int or float conversion ( | int or | float). In this case since you are using the as_timestamp method to convert the date_time objects into a true number (seconds) you don’t need to force that timestamp to be a number since it already is.

I’m not sure what you mean here.

Yes thanks I think I do understand the difference between a condition and trigger but I was being a little foolish. I guess what I was asking was if Either condition was true if the trigger would fire the automation. In any case, you are telling the Default is And logic so both conditions must be true for the trigger to fire the automation. The attribute might return ‘none’ for whatever reason but the trigger will not proceed to run the automation unless the time has exceeded the set “waiting period” from the point in time it was previously run.

I’m running out of steam but will come back and read what you have written on integers and strings and then keep and eye out for how at it might have been written without the unnecessary forcing

The built in automations editor in the front end seems to have a bug (a quick search showed a report on it) when you try to add a condition with a template it simply wont accept anything in that box
Thannks a bunch for taking the time to teach

Here is my final automation if it helps somebody

  alias: alert washing machine spin cycle
  trigger:
  - above: '450'
    entity_id: sensor.wemopower
    platform: numeric_state
  condition:
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.alert_washing_machine_spin_cycle.attributes.last_triggered) | int > 1800 }}'
  - condition: template
    value_template: '{{ states.automation.alert_washing_machine_spin_cycle.attributes.last_triggered != none }}'
  action:
  - data:
      entity_id: media_player.unisonic_3
      message: The Washer is in its final spin cycle
    service: tts.google_say

It did help me, this is the refined automation using state_attr()

alias: alert washing machine spin cycle
  trigger:
  - above: '450'
    entity_id: sensor.wemopower
    platform: numeric_state
  condition:
  - condition: template
    value_template: '{{ as_timestamp(now()) - (as_timestamp(state_attr("automation.alert_washing_machine_spin_cycle", "last_triggered")) or 0) > 1800 }}'
  action:
  - data:
      entity_id: media_player.unisonic_3
      message: The Washer is in its final spin cycle
    service: tts.google_say
1 Like

I’m having an issue where I keep getting true back from template gui
{{ as_timestamp(now()) - (as_timestamp(state_attr("automation.30e_alarm_system_notify", "last_triggered")) or 0) > 10 }}

Only if I do the following do I get false.
{{ as_timestamp(now()) - (as_timestamp(state_attr("automation.30e_alarm_system_notify", "last_triggered")) or 0) > 51000 }}

I check developer tools >> states and see

last_triggered: 2022-06-24T00:02:33.097122+00:00
mode: single
current: 0
id: 1655359381822
friendly_name: 30e - Alarm Notification

The time 00:02:33.09 is 12 hours ago (I live in New Zealand GMT+12)
Looking at system settings its set to New Zealand and the time appears correct, same via cli

[core-ssh ~]$ date
Fri Jun 24 12:09:29 NZST 2022

How do I get last_triggered to be in local time so automation works?

try this in dev tools template editor and see what the result is:

{{ state_attr("automation.30e_alarm_system_notify", "last_triggered") }}

{{ state_attr("automation.30e_alarm_system_notify", "last_triggered") | as_local }}

one should be in UTC (+00:00) and the other should be in your local time (+12:00)

Just what I was looking for :grinning:

And how can this been edited as ‘%H:%M:%S’ ?

based on what question?

but assuming it was the “as_local”:

{{ as_timestamp(state_attr("automation.30e_alarm_system_notify", "last_triggered") | as_local)| timestamp_custom(‘%H:%M:%S’)}}

actually it doesn’t matter which you use to display it. you can use either of those and should get the same result.

the as_timestamp converts it to a unix timestamp based on the TZ info provided then timestamp_custom converts it to local time.

Thanks, but…

I already did that, but using the timestamp_custom (underscore !) give me a 2 hours difference :

And indeed, with or without the “| local” gives the same result.

sorry. typo.

try this:

{{ as_timestamp(state_attr("automation.30e_alarm_system_notify", "last_triggered") | as_local)| timestamp_custom('%H:%M:%S', true)}}

true should be the default but maybe it’s getting hung up somewhere.

I assume your timezone is +02:00?

Yes, that’s the solution…thank you !

I live in Belgium, so yes +2 :grinning:

1 Like