[SOLVED] Template Fan Unavailable Entity

I have set up a template fan based on a switch entity and in the value template I use:

value_template: "{{ states('switch.office_fan') }}"

All works OK but if the fan is unplugged I get:

Received invalid fan is_on state: unavailable. Expected: on, off.

I tried

value_template: "{{ is_state('switch.office_fan', 'on') }}"

but get an error something like:

Received invalid fan is_on state: true or false. Expected: on, off.

Is there a way to catch the unavailable error ?

Try this:

value_template: >
  {% set s = states('switch.office_fan') %}
  {{ s if s in ['on', 'off'] else 'off' }}

The second line reports the fan’s state if it’s either on or off otherwise (like if it’s unknown) it will report off.

2 Likes

Perfect, thanks very much

1 Like