Hello all,
Can someone give me a bit of guidance on the subject.
I am using a gpio to essentially short a switch so that I can control it with home assistant. The physical light has 4 states in the following order. off, on, pulsing, sound reactive.
I can cycle through them using a home assistant switch (it toggles from home assistant state of on to off-- it doesn’t stay on or off because it is essentially showing what the gpio is doing). I also have an input select that allows me to move it to a specific setting.
Sadly ChatGPT got me to this point. Could someone help me get this last part figured out if it is doable.
I would like to be able to include this device in my light routines since it is a light rather than have to create new line items in all of my light automations for this one specific device.
Below is my current esphome code and how it looks in HA:
# Enable Home Assistant API
api:
services:
- service: set_light_state
variables:
state: string
then:
- lambda: |-
if (state == "Device OFF") {
id(button_counter) = 1;
} else if (state == "Device ON") {
id(button_counter) = 2;
} else if (state == "Device PULSING") {
id(button_counter) = 3;
} else if (state == "REACTS TO SOUND") {
id(button_counter) = 4;
}
// Update the sensor for Home Assistant
id(button_counter_sensor).publish_state(id(button_counter));
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Supermariolight Fallback Hotspot"
password: "pPrZpEH1vZK0"
captive_portal:
# Global variable to track the state
globals:
- id: button_counter
type: int
restore_value: yes
initial_value: '1'
# Define the GPIO output to simulate button presses
output:
- platform: gpio
pin: GPIO5 # Use GPIO5 to simulate the button press
id: simulated_button
inverted: True # Inverts the signal, making it low by default
# Template switch to simulate button presses and track the counter
switch:
- platform: template
name: "Simulated Button"
turn_on_action:
- lambda: |-
// Increment the counter
id(button_counter) += 1;
// Reset the counter after state 4
if (id(button_counter) > 4) {
id(button_counter) = 1;
}
// Log the state with meaningful names
switch (id(button_counter)) {
case 1:
ESP_LOGD("main", "State 1: Device OFF");
break;
case 2:
ESP_LOGD("main", "State 2: Device ON");
break;
case 3:
ESP_LOGD("main", "State 3: Device PULSING");
break;
case 4:
ESP_LOGD("main", "State 4: REACTS TO SOUND");
break;
}
// Simulate the button press
id(simulated_button).turn_on();
delay(1000); // Simulate the physical button press duration
id(simulated_button).turn_off();
// Update the sensor value after every press
id(button_counter_sensor).publish_state(id(button_counter));
# Virtual reset button to reset the counter
- platform: template
name: "Reset Button"
turn_on_action:
- lambda: |-
// Reset the counter to 1
id(button_counter) = 1;
ESP_LOGD("main", "Button counter reset to 1");
// Optionally, update the sensor state to reflect the reset
id(button_counter_sensor).publish_state(id(button_counter));
# Expose the button_counter as a sensor to Home Assistant
sensor:
- platform: template
name: "Button Press Counter"
id: button_counter_sensor
lambda: |-
return id(button_counter);
on_value:
then:
- homeassistant.service:
service: input_select.select_option
data:
entity_id: input_select.super_mario_light_state
option: !lambda |-
if (x == 1) {
return "Device OFF";
} else if (x == 2) {
return "Device ON";
} else if (x == 3) {
return "Device PULSING";
} else if (x == 4) {
return "REACTS TO SOUND";
} else {
return "Device OFF";
}