Switch for ignoring "Unavailable" in conditions

Hi,

I have a condition looking like this:

condition: state
entity_id: switch.kellerlueftung_motor
state: 'on'
for: '02:00:00'

So the motor should be turned of after running 2 hours. Unfortunately it often is not, because the sensor is occasionally unavailable due to bad wifi connection.

An action with a delay is another way to get the wanted behavior, but is less reliable, because e.g. if I restart HA in between, the motor is never turned off.

Request:
Add an option for conditions to ignore the “unavailabe” state of a list of defined states.

Something like:

ignore_states: 
  - "unavailable"
  - "foo"

Then you should fix that instead of creating a workaround.

The same goes for your automation, when you restart HA in between it will also not work.

Quick and dirty solution

trigger:
  platform: template
  value_template: "{{ is_state('switch.kellerlueftung_motor', 'on') or is_state('switch.kellerlueftung_motor', 'unavailable') }}"
  for:
    hours: 2
1 Like

I use defined transitions (off to on) to avoid this.

trigger:
  - platform: state
    entity_id: switch.kellerlueftung_motor
    from: 'off'
    to: 'on'
    for:
      hours: 2

This doesn’t solve OPs request. OP has the problem that his switch is unavailable sometimes, but he wants to ignore it. Your code will not work for him as it would only teigger when the state is on for 2 hours, but not if it was unavailable inbetween.

That doesn’t fix the problem. If you turn the switch on, then it goes unavailable, then becomes available (states go off, on, unavailable, on) then it won’t fire the automation because the state change was unavailable to on, not off to on.

Edit - Ugh, ninja’d :wink:

@Burningstone
Even assuming to have a perfect wifi connection is not a realiable way to solve this. Wifi will always have some hickups, by whatever reasons. Unavailability occasionally should not break automations in my opinion, since it can always happen. (in general, not only wifi)

Second, the solution with the condition WOULD work if I restart HA in between, in fact, it already has. It ran more than 2h, but at least turned off eventually. The only thing remaining, which would make it a truly robust solution, is the issue with the unavailable state.

@anon43302295
Thanks, I also found this template solutions, which fixes it, and I will use it. I just think it would be a nice feature to have this states_ignored list for convenience. I think it should be a quite common use case and templating is not for everyone.

1 Like

It’s a (little) bit more involved but for this specific case you can also start a timer (instead of using for) when it turns on and have an automation to turn it off when the timer expires. It will also cover the restart scenario if you turn on the timer from any other state to on and check that it’s not already running. So, two automations instead of one.