Skip to content

Instantly share code, notes, and snippets.

@pauly7300
Last active March 27, 2019 21:54
Show Gist options
  • Save pauly7300/d20d18b4f8df824d30d79b9ffce09135 to your computer and use it in GitHub Desktop.
Save pauly7300/d20d18b4f8df824d30d79b9ffce09135 to your computer and use it in GitHub Desktop.
fadeup/down on button press
# Define Global variable
globals:
- id: my_global
type: bool
restore_value: no
initial_value: 'true'
# Define momentary button for use with the on_press
binary_sensor:
- platform: gpio
pin:
number: 22
mode: INPUT_PULLUP
inverted: True
id: button1
name: button1
retain: false
on_press:
then:
- if:
condition:
# lambda to ensure the global variable is returning true initially. Just for debug.
lambda: |-
# ESP_LOGD("paul", "Global value is: %d", id(my_global));
return id(my_global);
# When above condition evaluates to true - Fade_up function else Fade_down
then:
# Fade_up function
- while:
condition:
binary_sensor.is_on: button1
then:
- light.turn_on:
id: printer_led
brightness: !lambda 'return id(printer_led).get_current_values().get_brightness() + 0.05;'
- delay: 0.05s
- logger.log: "executing up"
# flips the value of the global variable once the button is released
- lambda: |-
id(my_global) = (false);
else:
# Fade_down function
- while:
condition:
binary_sensor.is_on: button1
then:
- light.turn_on:
id: printer_led
# brightness: !lambda 'return (id(printer_led).get_current_values().get_brightness() == 0) ? 0 : id(printer_led).get_current_values().get_brightness() - 0.05;'
brightness: !lambda 'return id(printer_led).get_current_values().get_brightness() - 0.05;'
- delay: 0.05s
- logger.log: "executing down"
# flips the value of the global variable once the button is released
- lambda: |-
id(my_global) = (true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment