ESPHome MCP23017 Pin state on boot

Hi all. I recently decided to take the leap from Domoticz and espeasy to home assistant.
I’m struggling for a while now on a coding ussue. Say i have a microSwitch to read the state of a door or window and output to a led the state. Closed is on and open is off.
I get that done with:

>    on_state:
>       - switch.toggle: mcp23017_pin_b0

Problem is when Door or window is clossed at boot, the status led works in reverse.
Closed is off and open is on.

On espeasy i had a rule that checked the pin state on startup and changed the output to the status leds
from there.
Rule from espeasy…

> on System#Boot do
> monitor,mcp,17
> endon
> 
> on DeurVoordeur#Switch do
> if [DeurVoordeur#Switch]=1
>  mcpgpio,5,0
> else 
>  [DeurVoordeur#Switch]=0
>  mcpgpio,5,1
> endif
> endon

what i have in ESPHome…

> i2c:
>   sda: 4
>   scl: 5
>   scan: True
>   frequency: 800kHz
> 
> mcp23017:
>   - id: 'mcp23017_1'
>     address: 0x21    
> 
> # Individual outputs
> switch:
>   - platform: gpio
>     pin:
>       mcp23xxx: mcp23017_1
>       number: 8
>       mode:
>         output: true
>       inverted: True
>     id: mcp23017_pin_b0
> 
> # Individual inputs
> binary_sensor:
>   - platform: gpio
>     pin:
>       mcp23xxx: mcp23017_1
>       number: 7
>       inverted: true
>       mode:
>         input: true
>         pullup: true
>     name: "Living Room Window"
>     device_class: window
>     filters:
>       - delayed_on: 10ms
>     on_state:
>       - switch.toggle: mcp23017_pin_b0

I have tried allot of different ways, but can’t get it to work the right way.
Any push in the right way will be appreciated.

Thanks
Nuke

Maybe on_press: and on_release: are better choices, with appropriate switch.turn_on and switch.turn_off actions - that way the output will always follow the state

Otherwise there is the on_boot: configuration option in core, where you could test the current state and perform an appropriate action.

Hi zoogara.

Thanks allot for push in right direction.
after some playing and new stuff learned i got it to work :grinning:

will this be the most optimal way to do it?

on_boot:
    then:
      - if:
          condition:
            - binary_sensor.is_off: roomdoor
          then:
            switch.turn_on: status2_out9
      - if:
          condition:
            - binary_sensor.is_off: roomwindow
          then:
            switch.turn_on: status2_out8
binary_sensor:

  - platform: gpio
    pin:
      mcp23xxx: mcp23017_1
      number: 7
      inverted: False
      mode:
        input: true
        pullup: true
    name: "Living Room Window"
    id: roomwindow
    device_class: window
    filters:
      - delayed_on: 10ms
    on_press: 
        then: 
          - switch.turn_off: status2_out8
    on_release: 
      then:
        - switch.turn_on: status2_out8

Cheers :beers:

Looks fine. You may want to explicitly tell it what priority (stage of startup) to execute at. The default is 600, if you want to ensure your sensors have read you might choose a lower value, or even priority: -100.

Priority is discussed on the page I linked above,