Creating a binary sensor with availability_template

Hello there

I am trying to create a binary sensor from the state of a wifi switch. The idea is to use the availability of the switch to measure if there is electricity or not (it is not used to switch devices):

  • Black out/no electricity → switch is unavailable → binary sensor is off.
  • Electrity available → switch is available → binary sensor show on.

It is the first time I am writing a sensor myself and not just copy & paste. I thought a availability_template would be the right approach:

binary_sensor:
  - platform: template
    sensors:
      eskom_status:
        friendly_name: "Eskom Status"
        availability_template: "{{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}"

If I check the configuration.yaml, I get the following:

Invalid config for [binary_sensor.template]: required key not provided @ data[‘sensors’][‘eskom_status’][‘value_template’]. Got None. (See ?, line ?).

I am sure I am missing something with the filter at the end of the availability_template. Would someone please be so kind and push me in the right direction?

Look at the solution as I suspect that your black out/no electricity is supposed as rare as my smoke detection.

And if it is your solution as well, don’t forget to like Taras’s post describing it.

Thank you Olivier for your fast reply. I am not sure if I really understand your answer, which solution do you mean?
I am in South Africa… we have scheduled black outs called “load shedding” every day. Days without load shedding are rare.

Change availability_template to value_template

Then do a standard template and correct your yaml as @petro suggested.

Dear petro

Thank you so much. I changed it (and added a device class):

binary_sensor:
  - platform: template
    sensors:
      eskom_status:
        friendly_name: "Eskom Status"
        device_class: power
        value_template: "{{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}"

Now the yaml is passing the test, but the state of binary_sensor.eskom_status is “unavailable” while no electricity. Is it somehow possible to give it an “off” instead?

So your solution is needed for devices that rarely change state? That is why you reload your templates?

Yes and no.

My issue was that even thou the template is supposed to work (with value_template), in my case it was sometime unknown which is normally impossible. It is probably a bug but the solution provided by Taras is solving this as a workaround.

It was important for me because I have an automation that send notifications to the whole world, sounds alarm and blink all lights in red if it is detecting smoke.

But becoming unknown was a cause of false negatives.

In your case, it is ok to do a template as the one you did (or like mine in my initial question in the linked post)

Are you sure you’re looking at the correct entity? Do you have multiple binary_sensor sections in configuration.yaml?

Indeed, there is another section:

binary_sensor:
  - platform: trend
    sensors:
      rainfall_trend:
        entity_id: sensor.rainfall_today
        max_samples: 2

Can I put them together like this?

binary_sensor:
  - platform: trend
    sensors:
      rainfall_trend:
        entity_id: sensor.rainfall_today
        max_samples: 2

  - platform: template
    sensors:
      eskom_status:
        friendly_name: "Eskom Status"
        device_class: power
        value_template: "{{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}"

Yes you have to put them together like that.

Ok, everything together now. Meanwhile, the electricity is back, I can find the device and switch it on or off. But the binary sensor did not change from unavailable (no power) to on. Do you think something is wrong with the last line?

value_template: "{{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}"

I really don’t have a clue about the ‘on’ in there… or is it because of the device class? Sorry for taking your time here.

You can always check your template in Developer Tools > Template
What it is doing is

is_states(sensor, value) will compare sensor’s state with the value and tell if it is true or false

In your case, the template will evaluate to true if switch.qualitel_wifi_switch_switch_1 is on (false otherwise).

You can check what is the value of your sensor, also in Developer Tools > States in the entity column, juste write part of your sensor name to do a filter. Be sure that it is on, not true or anything similar.

Binary_sensor of device_class power must be on or off, not true or false.

So, to summarise and take my configuration as reference:

binary_sensor:
  - platform: trend
    sensors:
      rainfall_trend:
        entity_id: sensor.rainfall_today
        max_samples: 2
  - platform: template
    sensors:
      eskom_status:
        friendly_name: Eskom Status
        device_class: power
        value_template: >
          {{ iif(is_states('switch.qualitel_wifi_switch_switch_1', 'on'), 'on', 'off') }}

this is not true. Take a look at the template binary sensor documents. Many different template resolutions will return either true or false for your template.

Your template can be simplified to

        value_template: >
          {{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}

But because it’s a switch, you can simplify it futher by just outputting the switches state

          {{ states('switch.qualitel_wifi_switch_switch_1') }}

Like my binary_sensor that should never be unknown (link above), I had countless issues with binary sensors not providing the expected values even if “it should work”, so in my opinion it is a good habit to do so.

EDIT : In any case:

  1. Check that your switch is the one you want to test
  2. Check if it is giving you on/off value, nothing exotic
  3. Test your template in the Developer Tools > Template to see if it is what you expect or not

But it’s not required in the least bit for template binary sensors. If you want to remove unknown you simply use

        value_template: >
          {{ is_states('switch.qualitel_wifi_switch_switch_1', 'on') }}

as unknown and unavailable will resolve to off.

But stating that a power device class requires ‘on’ or ‘off’ from the template is not correct. It accepts a number of output’s that will resolve true.

That is not true, please have a look at my post linked in reply #2

Yes it is, the code posted only returns true when the state of the switch is ‘on’.

You’re not understanding what I’m telling you.

EDIT: IF you’re referring to the reload on a template integration that’s discussed in the solution on your second post, that’s 100% unrelated to the topic at hand and all you’re doing is adding confusion to the topic. The template in that situation isn’t executed, so the code never runs. Even with your code posted above to “account for this” will not solve that issue.

I do, but we are far from the solution of @Elmbob’s issue.

I was just pointing out that even if is_states, according to documentation, can only be true or false, I can prove that it is not true, I have in my history some is_states(blabla, ‘on’) that are unknown and it is a bug. Hence my advise to explicitly use the iif to ensure the off or false or whatever is evaluated to off, by experience.

But you’re right, it is not mandatory, it should be enough to reduce the template to the is_states call. It is just not working in my HA (always latest version).

No, that’s coming from the reloads, not the template. This is what you are not understanding. Using iif will not solve that. is_state() only returns true or false, nothing else.