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:
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?
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.
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?
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)
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?
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:
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.
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:
Check that your switch is the one you want to test
Check if it is giving you on/off value, nothing exotic
Test your template in the Developer Tools > Template to see if it is what you expect or not
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.