Invert Global Boolean Variable

I have a global variable declared:

  - id: dp1
    type: bool
    restore_value: no
    initial_value: 'true'

Now in a switch turn_on I want to invert this global variable. Is this going to be correct:

  - platform: gpio
      number: 32
    id: sw1
    name: "Switch 1"
    icon: "mdi:power"
    - lambda: return !id(dp1);
    - delay: 50ms
    - switch.turn_off: sw1

Or do I need to call it this way to change the global variable … or are both variants achieving the same?

    - lambda: id(dp1) = !id(dp1);

I did solved it now with:

    - lambda: |-
        id(dp3) = !id(dp3);

The other approach (return) did not work.

1 Like