Help writing template for when device is unavailable!

Hi!

I’m struggling using templates and would appreciate if someone could give some help on the syntax needed.

I have a Shelly device I want to use to monitor when the power for a machine is turned on. When the machine is off and the device is “Unavailable” I want a Template Binary Sensor to report “OK”.

When the machine is turned on (and the connected Shelly device is powered on and connect to the wifi) I want the Template Binary Sensor to show “Problem”. I shouldn’t matter if the Shelly device is turned on or off, just that it’s available again.

I have figured out the Template Binary Sensor should have device_class: problem. I don’t understand how the rest of the condition should look like.

platform: template
sensors:
  burner:
    friendly_name: "Burner"
    device_class: problem
    value_template: "{{ if is_state ('switch.shelly_shsw_1_ba20d9', '') }}"
platform: template
sensors:
  burner:
    friendly_name: "Burner"
    device_class: problem
    value_template: "{{ not is_state('switch.shelly_shsw_1_ba20d9', 'unavailable') }}"

This binary sensor will be ‘on’ (shows ‘problem’ in the frontend) when the shelly is NOT unavailable and will be ‘off’ (shows ‘OK’ in the frontend) when the shelly is unavailable.

1 Like

Great! Thank you! Just trying to learn more, if I wanted the reversed condition, I should have used

platform: template
sensors:
  burner:
    friendly_name: "Burner"
    device_class: problem
    value_template: "{{ is_state('switch.shelly_shsw_1_ba20d9', 'unavailable') }}"

When the device is “unavailable” the Template device is turned on (and show “Problem” based on device_class)?

platform: template
sensors:
  burner:
    friendly_name: "Burner"
    device_class: problem
    value_template: "{{ is_state('switch.shelly_shsw_1_ba20d9', ['unavailable', 'unknown', 'none']) }}"

To cover all the bases. :wink:

1 Like

Yes exactly.

1 Like