(Yet another) ignore previous "unavailable" state

I’m using the Github integration to keep track of any Os-Agent updates. I’ve setup an automation to create a persistent notification whenever the tag attribute of the relevant sensor changes (which means there is an update).
But whenever there is a wifi hiccup or a reboot or something out of the ordinary the tag briefly turns to “unavailable”. Which in turn creates the wrong notification that there is an update, since the tag has indeed changed briefly.
I’ve tried to setup a bunch of conditions which prevent the execution to no avail.
I’ve seen this in multiple threads for various reasons and it seems there is a request of many to just add an “ignore the previous state of the sensor if it is unavailable” or equals to whatever we want.
Examples (there are at least 10-15):

Can someone offer a relative straightforward solution for this that just works without having to write a bunch of complicated stuff? “Ignore the previous state of the sensor if state=whatever” so that it’s added as a condition and the automation prevented. Thanks!

1 Like

From what value to what value?

The value should always be numeric (let’s say 1.2 changes to 1.3). Then the automation should be fired.
But it should not be fired when it changes from “unavailable” to “numeric”.

If you don’t have discrete states to test for you have to use conditions to reject the unwanted triggers.

condition:
  - "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.from_state.state != trigger.to_state.state}}"

It seems that a value template condition, such as
"value_template: '{{ trigger.from_state.state != "unavailable" }}'
is the recommended way but the condition passes through when I’m coming from “unavailable”, I don’t know why.

Hmm. Maybe because you are looking at an attribute of the sensor, not the state.

Try this:

condition:
  - "{{ trigger.from_state.attributes.your_attribute_here not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.to_state.attributes.your_attribute_here not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.to_state.attributes.your_attribute_here != trigger.from_state.attributes.your_attribute_here }}"

/host/info: agent_version is an endpoint - docs

You could compare the actual sensor to the github sensor.

Hmm, that could be it. You mean triger.from_state obviously.

I mean both. You don’t want it triggering when it goes from or to unavailable. I updated my post.

Sharing your actual trigger would help too.

Yup. That’s the answer. So trigger.from_state.state goes for the whole entity, but trigger.from_state.attributes.your_attribute_here is for a single attribute?
Where is trigger.from_state.attributes.your_attribute_here actually documented? I can’t find it here Automation Trigger Variables - Home Assistant

I cover going to unavailable with another simple “state not unavailable” condition.

1 Like

Yes.

A combination of here: https://www.home-assistant.io/docs/automation/templating/#numeric-state and here: https://www.home-assistant.io/docs/configuration/state_object/

Ok, great I’d missed the second one. Thanks a lot!

I know this must be documented somewhere but final question: Is there a proper way (syntax wise) to add both “unavailable” and “unknown” in a single value template condition (in visual editor)? Or 2 conditions must necessarily be created?
I tried putting:

- "{{ trigger.from_state.attributes.your_attribute_here not in ['unknown', 'unavailable'] }}"
- "{{ trigger.to_state.attributes.your_attribute_here not in ['unknown', 'unavailable'] }}"

in a single condition and the automation is always stopped (but without an error, just stopped).

Each one of those is a full template condition. It is just written in shorthand:

https://www.home-assistant.io/docs/scripts/conditions/#template-condition-shorthand-notation

I’m trying just
{{ trigger.from_state.attributes.tag != "unavailable", "unknown" }}
within visual editor but it does not work.
My syntax is wrong.
I’m trying to have just a single condition instead of 2 (for educational purposes :grinning: )

:man_shrugging: Dunno. I don’t use the automation editor. What does it generate in the yaml view?

condition: template
value_template: "{{ trigger.from_state.attributes.tag != \"unavailable\", \"unknown\" }}"

Seems definitely wrong :grinning:

condition: template
value_template: |-
  - {{ trigger.from_state.attributes.tag != "unavailable" }}
  - {{ trigger.from_state.attributes.tag != "unknown" }}

in yaml does not work either. I feel an “and” or “or” has to be placed somewhere.

Not like that. Like this:

condition:
  - {{ trigger.from_state.attributes.tag != "unavailable" }}
  - {{ trigger.from_state.attributes.tag != "unknown" }}

Or like this:

condition:
  - condition: template
    value_template: "{{ trigger.from_state.attributes.tag != 'unavailable' }}"
  - condition: template
    value_template: "{{ trigger.from_state.attributes.tag != 'unknown' }}"

Or like this:

condition:
  - {{ trigger.from_state.attributes.tag != "unavailable" and trigger.from_state.attributes.tag != "unknown"  }}
1 Like