Inverting a simple binary sensor

I’ve read through everything out there and am still super confused. I’m pulling in a door monitor with the rest API as a binary sensor. The issue is the door monitor reports false when open and true when closed (the opposite of what Home Assistant expects).

My current value_template is:

value_template: "{{ value_json.sensors[0].doorClosed }}"

I have tried using is_state and turning this into a conditional statement, but nothing has worked to invert the door open/closed.

I’m sorry if this was posted in the wrong section. Been working at this problem for 3 hours now!

If you do this:

value_template: "{{ value_json.sensors[0].doorClosed == 'false' }}"

it means that if the selected value is false the template will report true (which is what Home Assistant would consider open).

Thanks, I’ve actually tried that as well and for some reason it doesn’t seem to work. And as a bonus then the data doesn’t update and it always shows as closed :sweat_smile:

I’m doing this in my configuration.yaml file, I hope that’s right.

Then are you certain the received data, false, is a string value?

Can you post an example of the JSON data?

Sorry for the messed up formatting below. Looks like it actually is not a string coming in, so that very well might be the issue! Any idea how to convert in line?

`{

"sensors": [
    {
        "id": 21201374977,
        "name": "B Door",
        "doorClosed": true,
        "doorStatusTime": "2020-05-08T03:29:53Z",
    }
]

}`

I’ll be honest and say that I’ve always had difficulty understanding how Home Assistant handles received boolean values.

Let’s say it is preserving the booleans and not converting them to strings. If that’s the case then this template should work for you:

{{ not value_json.sensors[0].doorClosed  }}

If the value is a boolean false then the template will convert it to a boolean true.

On the other hand it if booleans are converted to strings then the previous template I posted should work (except it didn’t).

Tried to reply earlier to say that I sorted it out right after I posted my response. I just removed the quotes around “false” in the code you provided (would have been perfect if I had been getting a string). Thank you so much for your help and your patience with a newbie.

I’ve marked your answer as the solution. Cheers!

1 Like

Thank you!

Just for fun, try the other template I posted (with the leading not) and it, in theory, should also work.

Do you know if ! works instead of not? I tried that previously but definitely had it in the wrong place.

It does not. :wink:

You can confirm it for yourself by pasting this into the Template Editor:

{{ not true }}
{{ ! true }}

1 Like