Hello all!
I have a light that has 4 states. 1 is on, 2 is pulse, 3 is sound reactive, and 4 is off.
Can anyone pass me some example code of how to get this working on the esphome side or could you review my code below.
As of now, my issue is the light comes on as soon as I hook the gpio up to the + side of the physical button/ - up to the ground on the device. GPIO I have connected to positive side of button, ground to negative.
Chat gpt got me to here and I feel like I know less now than I did. Thank you all again for guidance. My end goal is to be able to create automations that will set the light to the proper setting. (1-4). Each button press increments the state by 1 as detailed above.
I took out all of the encryption keys, wifi setup etc.
If you all need any pics, I am ok with providing but I’m pretty sure I did a good job of explaining what I’m trying to do.
Thank you all for any assistance, guidance, examples. I have never done anything like this.
Project 2:
I have one other project similar to this that I would like to do next. Essentially on it for everytime I push a button/ switch or whatever the best way to do it is, in home assistant, I would like it to send 2 electrical signals through the gpio to a button. I noticed it took to button triggers on this other device before it made its sound.
The code below is my attempt at project 1:
# Global variable to track the state
globals:
- id: button_counter
type: int
restore_value: no
initial_value: '1'
# Define the GPIO output to simulate button presses, with inverted state to start low
output:
- platform: gpio
pin: GPIO4 # Use GPIO4 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 ON");
break;
case 2:
ESP_LOGD("main", "State 2: Device PULSING");
break;
case 3:
ESP_LOGD("main", "State 3: Device REACTS TO SOUND");
break;
case 4:
ESP_LOGD("main", "State 4: Device OFF");
break;
}
// Simulate the button press
id(simulated_button).turn_on();
delay(300); // 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);
```