jdeneef / ha_gpiod

gpiod based RPI gpio switches and sensors
https://github.com/jdeneef/ha_gpiod
MIT License
23 stars 4 forks source link

Initial state for binary sensors with inverted logic is On #3

Closed f-mosquera closed 5 months ago

f-mosquera commented 6 months ago

When restarting the system all binary sensors that have inverted logic appear as triggered (On state). For example, I have a fire detector which has inverted logic and previously would initialise correctly to Off State, but now, when I boot, the fire sensor appears triggered. If I manually set it to Off it stays like that and only changes when actually triggered. Sensors that are not inverted boot up correctly in the Off State.

jdeneef commented 5 months ago

Had to test to make sure, but both the switch and binary_sensor operate completely inverted with invert_logic which makes sense? The default initialization state for an inverted switch and binary_sensor is ON where as switching or pushing the button returns OFF. Are you sure it has inverted logic? Could you describe what you would expect?

binary_sensor without invert_logic:

With invert_logic the logic of the binary_sensor is inverted, for my test button with inverted_logic:

If the binary_sensor is not initialized with the inverted value the first push of the button would not do anything.

f-mosquera commented 5 months ago

Thanks for looking into it and performing the test.

Although the “standard” logic is On when the input is high, and Off when the input is low, in the example of the fire detector I have, due to the electrical design it’s the opposite, when there is no fire detected the sensor outputs a high voltage and when it is triggered the voltage changes to 0. Hence the expected behaviour is the following in this case where the logic is inverted:

Input - GPIO signal / Output - Sensor State in Home Assistant High (idle) / Off Low (triggered) / On

This is working correctly when set to inverted logic. But when the system is initialised or there is no input signal you don’t want Home Assistant to show you that there is a fire, the default should be that the Sensor is considered Off, regardless of the logic being the “standard” one or inverted. The expected behaviour is therefore always:

Input - GPIO signal / Output - Sensor State in Home Assistant Initialization / Off

As you don’t want the system and the user to believe a sensor has been triggered without a real input that drives it.

In the original implementation the inverted logic sensors would also initialise to Off.

You point out that in your example of the push button if it is not initialised with the inverted value the first push of the button would not do anything and this would be correct, because if you push the button it should report Off, and if it was initialised to Off there is no change necessary. In reality if it was initialised to Off and the button was not being pushed it would immediately change to On as it would be receiving a low signal in the GPIO input (if this were the electrical design). And when the button is pushed it will change to Off.

jdeneef commented 5 months ago

new update might fix it, initializing input from the gpiod port as also suggested by @dukrat, not sure though this will fix your specific case. it's an improvement anyway, and I fixed an issue in cover implementation.

f-mosquera commented 5 months ago

I just tested the updated code and it works great! That was really quick, fantastic work, many thanks!!!

jdeneef commented 5 months ago

Perfect!