How to use "unavailable" as a trigger/status

Or use a state trigger instead of a device trigger.

1 Like

See if this image makes any sense.

I think you just lead me to my solution. A bit late now so will test tomorrow. I had assumed it wasn’t working properly, since I was expecting a drop down menu when I clicked on “from”, but seems I can type from “unavailable” to “off”. Guess just have to read up on how to add “ands” and “ors”.

From Unavailable to on or off, would change monitor status to on, and other automation from on or off, to unavailable would change monitor status to off.

you don’t need to have both from: and to:, you can just have from: 'unavailable' and not worry about whether it goes to on or off.

1 Like

If im not mistaken, triggers are always or. Condition can be or / and

And like dave stated, you don’t have to enter both states

Did you get this working? I’m having the same problem where I can’t seem to get an “unavailable” state to trigger an automation.

Make a binary_sensor that is on if the sensor is available or off if it’s not. Then trigger off that

1 Like

unavailable is just another word. There’s nothing special about it. If an entity’s state changes to unavailable, then it should trigger an automation that is looking for that state (just like on, off, open, unknown, or any other state string.)

Maybe you could share your automation and tell us a little about the entities used in its trigger.

So, I’m using the UI editor for automations and it looks like when I set to to unavailable, the config/automations.yaml has it saved as:

  - entity_id: switch.garage_freezer_power
    for: 00:01:00
    platform: state
    to: unavailable

That doesn’t appear to trigger it. But if you put the string unavailable in quotes, then it works. You can’t do this using the editor, so you have to edit the file manually. Of course every time any automation is saved you have to go back and do it again.

2 Likes

Sorry, that doesn’t make sense. unavailable is not a YAML reserved word, so it doesn’t have to be quoted (like, e.g., on and off do.) I suspect your issue has more to do with not reloading the automations after editing the YAML file, or some weird interaction between the Automation Editor UI and the YAML file. (I don’t use the UI so can’t speak to that.) I guarantee you it will work with unavailable not quoted.

EDIT: FWIW, I just tested it and it works fine without quotes.

I can also add that I have used unavailable in automations successfully, entered using yaml (I don’t like the UI editor either) however after looking back at the old code (it’s an old automation I no longer use) I did have ‘unavailable’ in quotes. Not to say that it needed to be, but that was how I had it and it did work.

@pnbruckner I agree it doesn’t make sense, but that’s the behavior of my system right now. If I use the UI editor it does not quote unavailable and it doesn’t work.

I created a package for my custom yaml, placed the exact same automation in there, quoted it so it was 'unavailable' and it works fine now.

So, like you said, maybe some weird interaction between the UI and the file.

Thanks for the help.

FWIW, I just tried it with the Automation Editor, too. Here’s what I created:

image

And here’s what was written to automations.yaml:

- id: '1593564962691'
  alias: New Automation
  description: ''
  trigger:
  - entity_id: sensor.xyz
    for: 00:01:00
    platform: state
    to: unavailable
  condition: []
  action:
  - data:
      message: test
    service: persistent_notification.create

Notice no quotes.

Then I went to the STATES page and created sensor.xyz with a state of 1. Then I changed it’s state to unavailable. Then I waited one minute, and sure enough, the automation triggered and I got the persistent notification.

So, can’t explain why it’s not working for you.

1 Like

If you have a spare USB port on the monitor, you could try this.

1 Like

I can’t explain it, but it may have something to do with the fact that my device actually becomes “unavailable” - it is a Wifi switch that loses its Wifi periodically. So if you go look at the device in HA you see some errors, though the switch state clearly says “unavailable”.

With custom automation in a package, where the automation includes a quoted version of the word ('unavailable') it seems to be working. So I’m all set.

Thanks for the help.

1 Like

Glad you have it working. Doesn’t really matter to me if you quote or don’t quote the word in your YAML code. I’m only trying to do my part to minimize misinformation, and if there is a real problem, try to get to the bottom of it so it can be addressed. :smiley:

I hate to raise an old thread from the dead, so apologies. However, I am on v2021.4.6 and still having this problem. I have 3 chromecast audios that constantly drop off the network so I have a controlled outlet to flap them on/off. Using the UI/automations.yaml the ‘unavailable’ state absolutely does not work for me as a trigger.

I moved the automation to a custom package and used quotes around the state ‘unavailable’ which then it works fine. I have a fair amount of automations as well, so i’m pretty comfortable making them, however this one seems like a genuine bug.

What code have you got that doesn’t work?

This worked for me (I no longer use it but have it saved away for reference)

alias: 'Notify if light unavailable'
initial_state: 'off'
trigger:
  - platform: state
    entity_id: light.bedroom_lamp, light.couch_light, light.dining_table_light, light.living_room_light, light.walkin_robe_light
    to: 'unavailable'
    for:
      seconds: 22
action:
  - service: notify.pushbullet
    data_template:
      message: >
        '{{ trigger.from_state.attributes.friendly_name }} has gone unavailable'

Here is my automation; I just tested it by unplugging the chromecast physically and waiting for it to go ‘unavailable’. This does not work in the GUI/automations.yaml, but now since I put it in a package and put quotes around unavailable, it works fine.

automation:
  alias: Chromecasts - Reboot
  description: Reboot the chromecast audio devices when they become unavailable
  trigger:
    - platform: state
      entity_id: media_player.kitchen_ceiling
      to: "unavailable"
    - platform: state
      entity_id: media_player.master_bedroom_speaker
      to: "unavailable"
    - platform: state
      entity_id: media_player.patio_speaker
      to: "unavailable"
  condition:
    - condition: state
      entity_id: input_boolean.automations
      state: "on"
    - condition: not
      conditions:
        - condition: state
          entity_id: media_player.kitchen_ceiling
          state: playing
    - condition: not
      conditions:
        - condition: state
          entity_id: media_player.master_bedroom_speaker
          state: playing
    - condition: not
      conditions:
        - condition: state
          entity_id: media_player.patio_speaker
          state: playing
  action:
    - service: switch.turn_off
      target:
        entity_id: switch.chromecasts
    - delay:
        hours: 0
        minutes: 0
        seconds: 30
        milliseconds: 0
    - service: switch.turn_on
      target:
        entity_id: switch.chromecasts
  mode: single

2 Likes

this would chekc when the TV is OFF, but we want the reverse usually. What I have done is just list all the states the ChromeCast can be in as state: [“on”, “off”, “idle”, “playing”] as per the docs, the state can be a list.
That covers it and is basically the equivalent of ‘not unavailable’, which can’t seem to be reproducible with the jinja template.