Binary Sensor - Inverting Status and re-instate state after Start-Up


#1

HI guys,

I think I’m not the only one with this problem :slight_smile:

My Binary Sensor shows me my windows as open when they’re closed and as closed when they’re open. Is it possible to invert that somehow?

Also: After I restarted HA the sensor state is gone. Means. If a window was showed open it´s now closed. Is there a way to reinstate the state?

Thanks a million!


#2

Did you use:
device_class: opening

Which platform is used by your binary_sensor? MQTT? Something else?


#3

Hi 123,

The Platform is KNX and I use device_class: window

Thanks for your support!


#4

I searched the community forum for ‘invert binary sensor’ and the commonly suggested solution is to create a template binary_sensor to invert the output of your KNX sensor.

There is also a Feature Request to provide binary_sensors with an invert: true option. You may wish to vote for it.

The reason I asked if you were using MQTT Binary Sensor is because you can invert it by modifying its payload_on and payload_off options.


#5

Yes I found this too and tried it but it didn´t work.

If this is the solution I need to try it further. I will post the error later when the kids are sleeping.

Again: Thanks for your help Taras! Without it I would have surrender after the first few days :slight_smile: Exceptional community here!


#6

Could it be, that I need to create a complete new yaml file for that?


#7

Where do you define your existing binary sensors? In configuration.yaml under a section called binary_sensor: or in a separate file (like binary_sensors.yaml)?


#8

In configuration.yaml under binary sensor:


#9

I had the same problem with MQTT Binary sensors.

Not the most elegant solution, but I ended up using the sensor to trigger an input_boolean that way the input_boolean will keep the sensor state after a restart.

You could also use the input_boolean to invert your open / close states.


#10

Thanks Wills. Definitely worth a shot. Two questions as I still try to figure out all these texting, coding and scripting stuff:

What do you mean with

This is setup as a package security.yaml

Do I need to create a new file?

This code snippet:

value_template: '{{ trigger.payload_json.RfReceived.Data == "1234EE" }}

Can I basically use this too? 1234EE is the name of one of your sensors I guess?

And can I put this part:

binary_sensor:
  - platform: template
    sensors:
      master_bedroom_window:
        friendly_name: "Master Bedroom Window"
        device_class: window
        value_template: >-
          {{ is_state('input_boolean.master_bedroom_window', 'on') }}

      master_bedroom_window_alarm:
        friendly_name: "Master Bedroom Window Alarm"
        device_class: problem
        value_template: >-
          {{ is_state('input_boolean.master_bedroom_window_alarm', 'on') }}

Directly in my configuration.yaml?

Sorry for the tons of questions


#11

There is more information on packages here

A package is like combing automations.yaml configuration.yaml groups.yaml for that single subject into a single package.

So you might have packages for heating, door sensors it’s another way to stopping your configuration.yaml and automations.yaml turning into a several thousand line file over time.

You can view my setup in my GitHub, you can see I use a combination of configuration.yaml and automations.yaml along with packages.

The part 1234EE is the RF data received from the door switch. I don’t know how KNX sensor look so you might be able to replace it with:

- alias: master_bedroom_window_open
  trigger:
    platform: state
    entity_id: switch.knx
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: input_boolean.master_bedroom_window

If it is inverted you could change
to: ‘on’
to
to: ‘off’

If you can provide what a KNX sensor looks like within HA I can probably give you a better reply how you might be able to adapt my example.


#12

Yes, that’s the way to do it.


#13

Great thanks a lot guys!

Here is an example:

- platform: knx
    name: Fenster Küche
    address: '8/1/14'
    device_class: window

Nothing too fancy :slight_smile:


#14

I have a question for you about the KNX binary sensor. This is not related to inverting the state.

Let’s say you open the window being monitored by the KNX binary sensor representing address ‘8/1/14’. Theoretically, the window’s state is ‘activated’. Now what happens if, while the window is still open, you restart Home Assistant? After restarting does Home Assistant report the window is no longer ‘activated’?

I’m asking these questions because I don’t know how KNX works and I’m wondering if the ‘activated’ command is persistent, meaning it is reported for as long as the window is open (or closed), or is momentary, meaning it reports the window’s state to anyone listening but you have to be present and listening to hear it (if you arrive after the event occurred you won’t hear what happened). For MQTT, “momentary” is the default mode of operation and you have to use an option to change the behavior to persistent.


#15

Happy to answers questions too :slight_smile:

Now what happens if, while the window is still open, you restart Home Assistant?

Let me give you a full insight: When I restart HA all sensors are in an “Off” state, the state they should have when the window is open. When I turn the handle to open the window physically it stays “off”. As soon as I turn the handle to close the window the sensor is going into an “on” state.

Now to your question: When the window is open, is in the “off” state and I restart HA then of course it´s still shown as off (because HA is resetting the status)

The thing is now: as I mentioned in other posts a few times I’m coming from a comercial solution named Symcon. I still have it and use it as some sort of control mechanism (and it was to expensive to get completely rid of it). Symcon is keeping the state of the sensors.

I don’t have the KNX configuration software here so I can’t check it but at least with HA it seems is momentary.

I hope this answers your question. Happy to help :slight_smile:


#16

Guys,

Do i need to create a Boolean dummy first?


#17

Hi guys,

just to let you know: as it seems I can invert the status directly in the actor. Just need to configure it via the ETS. Will keep you posted.

On the other side I struggle with the booleans as I dont really know where to start here. Do I need to configure the boolean first?