I want to filter "unavailable" from an automation

I’m trying to use the not condition to filter out the events when the device goes to an unknown or unavailable state. My code looks like this:

- id: send_message_on_ups_changes
  initial_state: true
  alias: Send Message on UPS Changes
  trigger:
    platform: state
    entity_id:
      - sensor.wiring_closet_ups_status
      - sensor.living_room_ups_status
      - sensor.office_ups_status
  condition:
    condition: not
    conditions:
      - condition: state
        entity_id:
          - sensor.wiring_closet_ups_status
          - sensor.living_room_ups_status
          - sensor.office_ups_status
        state:
          - unknown
          - unavailable
  mode: queued
  max: 4
  action:
    service: notify.vapid
    data_template:
      title: >
        {{ trigger.to_state.name }}: from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
      message: >
        Wiring Closet: {{ states("sensor.wiring_closet_ups_status") }}
          Charge:  {{ states("sensor.wiring_closet_ups_battery_charge") }}%   Load:    {{ states("sensor.wiring_closet_ups_load") }}%   Runtime: {{ states("sensor.wiring_closet_ups_battery_runtime") }}s
        Living Room: {{ states("sensor.living_room_ups_status") }}
          Charge:  {{ states("sensor.living_room_ups_battery_charge") }}%   Load:    {{ states("sensor.living_room_ups_load") }}%   Runtime: {{ states("sensor.living_room_ups_battery_runtime") }}s
        Office:  {{ states("sensor.office_ups_status") }}
          Charge:  {{ states("sensor.office_ups_battery_charge") }}%   Load:    {{ states("sensor.office_ups_load") }}%   Runtime: {{ states("sensor.office_ups_battery_runtime") }}s

but when the entities go to an unavailable state I still get the notification! Why does that happen and how can I fix it?

Should I use a template trigger and can I still look at the trigger.from_state.state and trigger.to_state.state within it?

Thanks,
-Greg

Because when you list multiple entities in a condition they all have to be in that state.

However, according to the docs, the not condition:

Passes if all embedded conditions are not valid.

Shouldn’t it work like I’m thinking? I haven’t gotten any unknown entries since adding this (which usually happen when restarting HA) but I still get unavailable ones (which happen when I restart the devices which are monitoring the UPS devices).

I want all the entities to NOT be in the listed states.

Yeah, so all 3 have to not be unavailable or unknown.

this sounds like you would be better off with

condition:
  >
    {{trigger.to_state is not none and 
      trigger.from_state is not none}}

or, if must:

condition:
  >
    {{trigger.to_state is not none and 
      trigger.from_state is not none and
      trigger.to_state.state not in ['unknown','unavailable']
1 Like