Toggle state of an output gpio from lambda, why is it so complicated?

I am having a speed fan and its oscillation_output required a child of BinaryGpioOutput. So I made an output with platform gpio, and it worked. But now I want to control this swing from lambda. What I found is gpio output doesn’t have state attribute, so id(my_output).state don’t work. There is only method for setting the state, not read the current state. Then I create an switch with platform output. From lambda I can toggle the state of this switch, but then it only turn off the output if it is currently on, not vice versa.

output:
  - platform: esp8266_pwm
    pin: D5
    id: 'BLDC_output'

  - platform: gpio
    pin: D6
    id: swing

switch:
  - platform: gpio
    pin: D8
    id: buzzer
    name: "${device_name} buzzer"
    internal: True
    on_turn_on:
      - delay: 30ms
      - switch.turn_off: buzzer

  - platform: output
    output: swing
    name: "${device_name} swing"
    id: swing_switch

fan:
  - platform: speed
    output: BLDC_output
    name: "${device_name} Fan"
    id: bldc_fan
    oscillation_output: swing
    on_speed_set:
      - logger.log: "Speed changed!"
      - output.esp8266_pwm.set_frequency:
          id: BLDC_output
          frequency: !lambda |-
            int pwm = 0;

            pwm = 150 + x;

            ESP_LOGD("main", "I am at execution number %d", pwm);

            return pwm;
      - text.set:
          id: display_text
          value: !lambda |-
            return to_string(x).c_str();

id(your-gpio-output).turn_on();

What if it is already on?

Leave it on… :sweat_smile:

Ok, I think I understood, the problem is to verify state?
I don’t think there is function for that, but you can work around with help of global variable

or maybe

binary_sensor:

  • platform: gpio
    pin:
    number: $YOUR_PIN
    mode:
    input: true

Yes exactly, there is bunch of solution but I just wonder why there is no get status method for this gpio output

It’s not only for verifying, the simple case is I want to toggle the output, but toggle method is not available as well as get state method. So how can we toggle without knowing the current state?

I agree… And why there is not numeric input in webserver???

have a look:

It’s fairly well documented in the docs:

1 Like

It’s the same approach as mine, create a switch with the given output. First, it’s over complicate compared with directly getting the state of the output. Second, it doens’t work for me. I used lambda to toggle the state of the switch, it updated on the frontend ui, but the actual output pin does not turn on. If I use the frontend to toggle the switch, the output turn on.

It’s not overly complicated. The pin is set to output mode and thus you can’t read the input. Why complicate the output component with unnecessary state handling when there’s other components that already does that.

I don’t see any lambda turning on the switch in the config you’ve posted?

1 Like

Oh I got the idea about that concept, I just simply thought that because of this switch I have to add the switch: component, which lead to a lot of file will be use while compiling, I don’t like that. Now it become rational.
Thanks for your info.

I dont put the lambda code because it can be anywhere :v