sebi
September 18, 2024, 8:16am
1
How to configure a switch combining BOTH momentary and GPIO features?
My current configuration.yaml includes:
switch:
- platform: momentary
name: Momentary Switch
toggle_for: 0.5
- platform: rpi_gpio
switches:
- name: RPi GPIO Switch
port: 22
This configuration is based on two HACS integrations: Momentary Switch Component and Raspberry Pi GPIO.
This configuration yields two distinct switches represented in Lovelace this way:
These two switches work as expected as Momentary Switch falls back to off after 0.5s and RPi GPIO Switch controls a GPIO on my Raspberry Pi.
But how to configure a SINGLE switch combining both momentary and rpi_gpio platform features?
Am a newbie. Any help would be greatly appreciated!
Here, catch. This is straight from my water heater ESP device and has been working solidly for the past 3 years:
switch:
- platform: gpio
name: "Water Heater Switch"
pin: 5 #BrownWhite D1
inverted: yes
id: whrelay1
restore_mode: ALWAYS_OFF
on_turn_on:
then:
- delay: 3s
- switch.turn_off: whrelay1
You only really need the last 4 lines if you already have it set up as a switch under the gpio
platform - just change the delay to suit your needs.
sebi
September 18, 2024, 9:29pm
3
Thanks for the help, but unfortunately it doesn’t work as you might confuse gpio
(in ESPHome?) with rpi_gpio
?
Hence I get this error message:
Configuration warnings
Invalid config for 'rpi_gpio' from integration 'switch' at configuration.yaml, line 44:
'on_turn_on' is an invalid option for 'switch.rpi_gpio', check: switches->0->on_turn_on
…after modifying configuration.yaml
this way:
switch:
- platform: rpi_gpio
switches:
- name: RPi GPIO Switch 22
port: 22
unique_id: rpi_gpio_switch_22
on_turn_on:
then:
- delay: 0.5s
- switch.turn_off: rpi_gpio_switch_22
sebi
September 20, 2024, 11:27pm
4
switch:
- platform: rpi_gpio
switches:
- name: RPi GPIO Switch
unique_id: rpi_gpio_switch
port: 22
- platform: template
switches:
momentary_switch:
friendly_name: Momentary Switch
value_template: >-
{% if is_state('switch.rpi_gpio_switch','off') %}
off
{% elif is_state('switch.rpi_gpio_switch','on') %}
on
{% else %}
unavailable
{% endif %}
turn_on:
- service: switch.turn_on
target:
entity_id: switch.rpi_gpio_switch
- delay:
milliseconds: 500
- service: switch.turn_off
target:
entity_id: switch.rpi_gpio_switch
turn_off:
- service: switch.turn_off
target:
entity_id: switch.rpi_gpio_switch
… seems to do the job.
The HACS integration called Momentary Switch Component is not required anymore.
Hiding RPi GPIO Switch from the auto-generated lovelace panel allows to see Momentary Switch only.