Creating a binary sensor with availability_template

Some feedback after a few days with a somehow limping setup: I was using a Qualitel Wifi Switch (south-african copy of a Sonoff Basic R1) with identical casing but running a non-ESP chip with the Tuya integration. That thing is so unreliable, was"unavailable", but still could be switch. I changed it to a "real Basic R2. Should be better now.
Otherwise, how do I express the following in the value_template:

If state of the switch is unavailable or unknow, the state of the sensor is off.
If state of the switch is on or off, the state of the sensor is on.

I do not have a clue how to get this into that code. Or is the device_class simply wrong for that?

{{ states('switch.xyz') in ['on', 'off'] }}

Device class simply changes the translated states and the icon. It does nothing more. Everything that oliver said above is his own findings which are not correct. Under the hood the state of a binary sensor or switch will always be 1 of the 4 following states: on, off, unknown, unavailable. device class will change the icon based on those 4 states, and it will change the translation you see in the frontend. For example, if your language is set to English, a device class of door will translate on to Open and off to Closed. The icon will also be a door opening or a door closing. The result of the template does not need to be on or off. It can be any verbiage that home assistant considers on or off. Personally, I always return true/false because it requires no extra code. The code above returns true/false if the state of the switch is on or off. All other states (unknown unavailable) will be ‘off’.

Just keep in mind, whenever you reload any integration, the entities that the integration creates will all have a state of unknown, regardless of your template. This is unavoidable. You simply need to account for that in your automations.

1 Like

You are awesome: you take your time to explain so well to somebody you do not even know. I guess that’s how the forum works, but again and again I am amazed to see people helping me here.
We are having load shedding again… the state of the switch is off despite not having electrity. Weird. So I thought of using the sensor’s SSID state as detection:

value_template: "{{ states('sensor.myswitch_ssid) == "MyWifi" }}"

If the switch does have a Wifi connection, the state of the sensor is on. But I am missing bring in the “on”. How would that be possible please?

value_template: "{{ is_state('sensor.myswitch_ssid', 'MyWifi') and states('switch.xyz') in ['on','off'] }}"
1 Like

It is working! Thank you so much, also for the hint with the unknown state after restarting addons. Your input gave me some momentum to carry with the (sometimes frustrating) programming yaml’s.

Hey petro

Just an addition: since I implied the line

value_template: "{{ is_state('sensor.myswitch_ssid', 'MyWifi') and states('switch.xyz') in ['on','off'] }}"

the binary sensor is working (showing on/off like expexted). But the state of the switch.xyz is behaving weird: even when it does not have electricity it shows off and can be switched manually. But in the logs the right state “unavailable” is show. I never had this before, that a switch is showing an uncorrect state.
That line above is just reading the state of the switch.xyz, but not changing it?

Correct, templates are read only, no writing