Check previous state of automation trigger in condition

Hi,

I’m trying to create an automation condition for my automation that runs when my robot vacuum dockes (after finishing cleaning). For that I have this trigger:

platform: device
device_id: xxx
domain: vacuum
entity_id: vacuum.xxx
type: docked

This works. However, sometimes the vacuum becomes “Unavailable” (Cloud connection issues, WiFi down, sometimes random etc.). This means the above state I am using to trigger also becomes “Unavailable”. Then, when the connection comes back, the state gets set to “Docked” - this fires the automation which I don’t want because the vacuum hasn’t been cleaning before (just disconnected).

I am trying to prevent the automation running with a condition, in which I check the previous state of the trigger. If that previous state is “Unavailable”, then the automation should not trigger. This is the condition I came up with:

condition: template
value_template: '{{ trigger.from_state.state != "Unavailable" }}'

However I’m not sure if that would work that way. At least when testing the condition I get:

template value should be a string for dictionary value @ data[‘value_template’]. Got None

Any hints on how I can achieve this or what I’m doing wrong? I read some threads in the forum but couldn’t get it working.

Cheers!

1 Like

Your check seems fine to me. Perhaps post the full automation so that we can see if there’s another error. Remember that if you manually trigger the automation it simply runs the actions as a script and there will be no trigger variable (could be the reason you’re getting None). You should also make it lowercase "unavailable".

You can also wait for the next release, which will include a not_from for state triggers, which should neatly solve your problem.

1 Like

Gotcha, that makes sense that there is no trigger available when testing. I just assumed it would get the previous state of the device in the trigger.

I changed it to lowercase now and will just wait for the next outage. Usually that happens every few days. I’ll then report back.

The whole automation looks like this:

alias: Vacuum Finished
description: ''
trigger:
  - platform: device
    device_id: xxx
    domain: vacuum
    entity_id: vacuum.xxx
    type: docked
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != "unavailable" }}'
action:
  - service: tts.cloud_say
    data:
      message: Vacuum has finished cleaning
      entity_id: media_player.googlehome7451
  - service: notify.all_phones
    data:
      message: clear_notification
      data:
        tag: cleaning
mode: single

Also that next release sounds sweet if I can’t get this to run!

Your automation uses a Device Trigger which doesn’t report from_state in the trigger variable so that makes the Template Condition you designed ineffective.

Use a State Trigger.

alias: Vacuum Finished
description: ''
trigger:
  - platform: state
    entity_id: vacuum.xxx
    to: docked
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != "unavailable" }}'
action:
 .. etc ...

Ensure the desired state is named docked by checking the entity’s state in Developer Tools > States.

3 Likes

Good hint with the state trigger! I modified your idea to this using from:

trigger:
  - platform: state
    entity_id: vacuum.xxx
    to: docked
    from: cleaning
  - platform: state
    entity_id: vacuum.xxx
    from: returning
    to: docked

So now I don’t need a condition at all, the unavailable state just never triggers the automation in the first place. And it works! Thanks!

1 Like

If you wish, you can consolidate the State Triggers like this:

trigger:
  - platform: state
    entity_id: vacuum.xxx
    from:
      - returning
      - cleaning
    to: docked
2 Likes

Cheers, learning more every day!

Hello,

I have a similar issue, but I’m using a numeric_state for trigger.

This is the conditon:

{{ states('sensor.casavas_realfeel_temperature_max_0d') | float(0) < 21 }}

In Dev Tools - Template I’m getting the desired answer - “false”.

But when I test in automation it gives the follow error:

- id: '1642617173272'
  alias: VMC - >N1 - Heat if <21ºC
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: number.ventilation_speed_set_2
    above: '1'
    for:
      hours: 0
      minutes: 0
      seconds: 10
  condition:
  - condition: numeric_state
    entity_id: sensor.casa_temperatura_insuflacao
    below: '21'
  - condition: template
    value_template: '{{ states(''sensor.casavas_realfeel_temperature_max_0d'') | float(0)
      < 21 }}'
  action:
  - service: number.set_value
    data:
      value: '22'
    target:
      entity_id: number.temperature_set_2
  mode: single

So, what is causing this error?
Thanks

Did you ever sort this out?

Also looking for a variant of @Taras 's Check previous state of automation trigger in condition - #4 by 123 but with a numeric_state trigger.

I need to filter previous “unavailable” state which creates false positives for my automation.

Did you ever sort this out?

Also looking for a variant of @Taras 's Check previous state of automation trigger in condition - #4 by 123 but with a numeric_state trigger.

I need to filter previous “unavailable” state which creates false positives for my automation.

Additional challenge: my numeric_state trigger uses

for:
  hours: 0
  minutes: 0
  seconds: 10
above: 5

which I think is not possible. All attempts to “check sensor state exactly before or within last 10 seconds” in a template failed unfortunately.


Edit:
Maybe this could work:

{{ states('sensor.current_consumption') == 'unavailable' and as_timestamp(now()) - as_timestamp(states.sensor.current_consumption.last_updated) <= 10 }}

By the way what the heck is going on with the forum, loading forever, can not save edits etc. etc. …

Show one of your attempts.

Added it to the previous post.

What I need now as ONE trigger template (combination) is:

  • “when sensor is > 5 for at least 10 seconds” (the stuff a for in a numeric_state trigger does) AND
  • “when sensor has not been unavailable in last 10 seconds”

I think only the first one is missing, I know the last one and how to combine them.


Edit after spending way too much time on this:

  • Checking if a sensor was unavailable in the last X seconds using a template is not possible. So this one of me is crap:
{{ states('sensor.current_consumption') == 'unavailable' and as_timestamp(now()) - as_timestamp(states.sensor.current_consumption.last_updated) <= 10 }}
  • The only working check is the well-known {{ trigger.from_state.state == "unavailable" }} template
  • Therefore I had to adjust my automation and lost the former for: feature of my numeric_state trigger. Now I
    1. Use a numeric_state trigger and
    2. Filter the unavailable state (only last/previous one)

So I had to trade the for (time dimension) for finally efficiently filtering unavailable states, which is OK for me.