Skip to content

Instantly share code, notes, and snippets.

@WarriorRocker
Last active December 27, 2022 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WarriorRocker/08bd03ff71cb26d43efaac9f5b50e6d4 to your computer and use it in GitHub Desktop.
Save WarriorRocker/08bd03ff71cb26d43efaac9f5b50e6d4 to your computer and use it in GitHub Desktop.
ESPHome Lego Power Functions
alias: Holiday Train On every Hour
description: ""
trigger:
- platform: time_pattern
minutes: "0"
seconds: "0"
condition:
- condition: time
before: "22:30:00"
after: "08:30:00"
action:
- service: script.holiday_train
data: {}
mode: single
alias: Holiday Train and Sound
sequence:
- service: esphome.lego_box_dfplayer_set_volume
data:
volume: 25
enabled: true
- service: number.set_value
data:
value: "70"
target:
entity_id: number.lego_box_port_1_speed
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- service: esphome.lego_box_dfplayer_play
data:
file: 1
- delay:
hours: 0
minutes: 0
seconds: 4
milliseconds: 0
- service: switch.turn_on
data: {}
target:
entity_id: switch.lego_box_port_1
- service: esphome.lego_box_dfplayer_play
data:
file: 2
- delay:
hours: 0
minutes: 0
seconds: 7
milliseconds: 0
- service: number.set_value
data:
value: "77"
target:
entity_id: number.lego_box_port_1_speed
- service: esphome.lego_box_dfplayer_play_loop
data:
loop_: true
file: 4
- delay:
hours: 0
minutes: 0
seconds: 30
milliseconds: 0
- service: esphome.lego_box_dfplayer_play
data:
file: 1
- delay:
hours: 0
minutes: 0
seconds: 4
milliseconds: 0
- service: esphome.lego_box_dfplayer_play_loop
data:
loop_: true
file: 4
- delay:
hours: 0
minutes: 0
seconds: 30
milliseconds: 0
- service: number.set_value
data:
value: "60"
target:
entity_id: number.lego_box_port_1_speed
- service: esphome.lego_box_dfplayer_play
data:
file: 5
- delay:
hours: 0
minutes: 0
seconds: 4
milliseconds: 0
- service: switch.turn_off
data: {}
target:
entity_id: switch.lego_box_port_1
- service: esphome.lego_box_dfplayer_stop
data: {}
mode: single
esphome:
name: lego-box
esp8266:
board: esp01_1m
# Enable logging
logger:
baud_rate: 0
# Enable Home Assistant API
api:
encryption:
key: !secret lego-box_enc_key
services:
- service: dfplayer_play
variables:
file: int
then:
- dfplayer.play: !lambda 'return file;'
- service: dfplayer_play_loop
variables:
file: int
loop_: bool
then:
- dfplayer.play:
file: !lambda 'return file;'
loop: !lambda 'return loop_;'
- service: dfplayer_set_volume
variables:
volume: int
then:
- dfplayer.set_volume: !lambda 'return volume;'
- service: dfplayer_reset
then:
- dfplayer.reset
- service: dfplayer_stop
then:
- dfplayer.stop
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Lego-Box Fallback Hotspot"
password: !secret cap_password
# Enable Captive Portal for fallback hotspot
captive_portal:
# Enable web server interface
web_server:
# Set up mp3 module pins
uart:
tx_pin: GPIO5
rx_pin: GPIO3
baud_rate: 9600
# Enable DFPlayer Mini
dfplayer:
on_finished_playback:
then:
- logger.log: "Playback finished"
# Set up motor driver pins
output:
- platform: esp8266_pwm
id: port_1_pin_1
pin: GPIO14
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_1_pin_2
pin: GPIO12
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_1
pin: GPIO13
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_2
pin: GPIO4
frequency: 20000 Hz
zero_means_zero: true
# Set up switch inputs
switch:
- platform: template
id: port_1
name: "Port 1"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_1_reverse
name: "Port 1 Reverse"
icon: "mdi:sync"
optimistic: true
restore_state: true
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_2
name: "Port 2"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
- platform: template
id: port_2_reverse
name: "Port 2 Reverse"
icon: "mdi:sync"
optimistic: true
restore_state: true
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
# Set up number inputs
number:
- platform: template
id: port_1_speed
name: "Port 1 Speed"
icon: "mdi:speedometer"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_1_update
- platform: template
id: port_2_speed
icon: "mdi:speedometer"
name: "Port 2 Speed"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_2_update
# Set up motor controller scripts
script:
- id: port_1_update
then:
- if:
condition:
- switch.is_on: port_1
then:
- if:
condition:
- switch.is_on: port_1_reverse
then:
- output.turn_off: port_1_pin_1
- output.turn_on: port_1_pin_2
- lambda: id(port_1_pin_2).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_on: port_1_pin_1
- output.turn_off: port_1_pin_2
- lambda: id(port_1_pin_1).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_off: port_1_pin_1
- output.turn_off: port_1_pin_2
- id: port_2_update
then:
- if:
condition:
- switch.is_on: port_2
then:
- if:
condition:
- switch.is_on: port_2_reverse
then:
- output.turn_off: port_2_pin_1
- output.turn_on: port_2_pin_2
- lambda: id(port_2_pin_2).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_on: port_2_pin_1
- output.turn_off: port_2_pin_2
- lambda: id(port_2_pin_1).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_off: port_2_pin_1
- output.turn_off: port_2_pin_2
# Set up motor controller using Fan component
# fan:
# - platform: hbridge
# id: port_1
# name: "Port 1"
# pin_a: port_1_pin_1
# pin_b: port_1_pin_2
# decay_mode: slow
# speed_count: 100
# - platform: hbridge
# id: port_2
# name: "Port 2"
# pin_a: port_2_pin_1
# pin_b: port_2_pin_2
# decay_mode: slow
# speed_count: 100
esphome:
name: lego-box
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: !secret lego-box_enc_key
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Lego-Box Fallback Hotspot"
password: !secret cap_password
# Enable Captive Portal for fallback hotspot
captive_portal:
# Enable web server interface
web_server:
# Set up motor driver pins
output:
- platform: esp8266_pwm
id: port_1_pin_1
pin: GPIO14
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_1_pin_2
pin: GPIO12
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_1
pin: GPIO13
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_2
pin: GPIO4
frequency: 20000 Hz
zero_means_zero: true
# Set up switch inputs
switch:
- platform: template
id: port_1
name: "Port 1"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_1_reverse
name: "Port 1 Reverse"
icon: "mdi:sync"
optimistic: true
restore_state: true
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_2
name: "Port 2"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
- platform: template
id: port_2_reverse
name: "Port 2 Reverse"
icon: "mdi:sync"
optimistic: true
restore_state: true
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
# Set up number inputs
number:
- platform: template
id: port_1_speed
name: "Port 1 Speed"
icon: "mdi:speedometer"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_1_update
- platform: template
id: port_2_speed
icon: "mdi:speedometer"
name: "Port 2 Speed"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_2_update
# Set up motor controller scripts
script:
- id: port_1_update
then:
- if:
condition:
- switch.is_on: port_1
then:
- if:
condition:
- switch.is_on: port_1_reverse
then:
- output.turn_off: port_1_pin_1
- output.turn_on: port_1_pin_2
- lambda: id(port_1_pin_2).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_on: port_1_pin_1
- output.turn_off: port_1_pin_2
- lambda: id(port_1_pin_1).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_off: port_1_pin_1
- output.turn_off: port_1_pin_2
- id: port_2_update
then:
- if:
condition:
- switch.is_on: port_2
then:
- if:
condition:
- switch.is_on: port_2_reverse
then:
- output.turn_off: port_2_pin_1
- output.turn_on: port_2_pin_2
- lambda: id(port_2_pin_2).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_on: port_2_pin_1
- output.turn_off: port_2_pin_2
- lambda: id(port_2_pin_1).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_off: port_2_pin_1
- output.turn_off: port_2_pin_2
# Set up motor controller using Fan component
# fan:
# - platform: hbridge
# id: port_1
# name: "Port 1"
# pin_a: port_1_pin_1
# pin_b: port_1_pin_2
# decay_mode: slow
# speed_count: 100
# - platform: hbridge
# id: port_2
# name: "Port 2"
# pin_a: port_2_pin_1
# pin_b: port_2_pin_2
# decay_mode: slow
# speed_count: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment