However, this doesn’t render how Id like in HA.
This shows a toggle switch in HA, and when I click it, it turns on and then off again after 100ms. This activates the device but the switch shows as off in the UI. I can then click it again and the same UI experience happens, and my device turns off.
How do I have a momentary output on my GPIO, but a stateful toggle switch in the HA UI?
Thanks
Update
OK, I got this far, is this the right way to go about this? However the “My Device” switch turns itself back off about 1s after pressing it
I dont know about your issue, but the first script you wrote, it helped me in my issue. I am connecting it with my gate lock, in which the relay, on turning on provide power to the megnatic lock for a secound and than turn off, in short will open the gate. Thank you for your efforts
This is how i implemented this. I wanted to switch an audio amplifier that is powered on and off with a push button one has to press for about 1 sec.
The schematic is pretty straight forward.
Pin 13 is connected to high (not ground) side of the amplifier power button
Pin 12 is connected to 3.3 V rail that is only powered when the amplifier is switched on (I added a pull down resistor of 10kOhms)
With the code below a switch is shown in home assistant. Even if somewone shuts down the amplifier manually, the switch represents the correct on / off state.
output:
- platform: gpio
pin: 13
id: power
inverted: True
switch:
- platform: template
name: "Simulated Power Switch"
lambda: |-
if (id(power_sensor).state) {
return true;
} else {
return false;
}
turn_on_action:
- output.turn_on: power
- delay: 500ms
- output.turn_off: power
turn_off_action:
- output.turn_on: power
- delay: 500ms
- output.turn_off: power
binary_sensor:
- platform: gpio
pin: 12
id: power_sensor
name: "Amplifier"
device_class: power