I am sharing my sketch. Based on the sketch below, I am using this led light to show air quality status (good for green, normal for yellow and bad for red). This sketch is just a switch so you should make some automation in HA.
https://www.amazon.com/Traffic-Display-Module-Arduino-Mini-Traffic/dp/B07SZMRSDN
#define MY_DEBUG
#define MY_RADIO_RF24
#define MY_NODE_ID 16
#define MY_RF24_PA_LEVEL RF24_PA_LOW
#include <MySensors.h>
#define CHILD_ID_GREEN 1
#define CHILD_ID_YELLOW 2
#define CHILD_ID_RED 3
#define GREEN_PIN 3
#define YELLOW_PIN 4
#define RED_PIN 5
bool greenState = false;
bool yellowState = false;
bool redState = false;
MyMessage msgGreen(CHILD_ID_GREEN, V_LIGHT);
MyMessage msgYellow(CHILD_ID_YELLOW, V_LIGHT);
MyMessage msgRed(CHILD_ID_RED, V_LIGHT);
void setup() {
Serial.begin(115200);
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
wait(200);
}
void presentation() {
// Initialize the MySensors communication
sendSketchInfo("Traffic Light Node", "1.0");
present(CHILD_ID_GREEN, S_LIGHT);
present(CHILD_ID_YELLOW, S_LIGHT);
present(CHILD_ID_RED, S_LIGHT);
}
void loop() {
// Send initial values of the LEDs to the controller
send(msgGreen.set(greenState ? 1 : 0));
send(msgYellow.set(yellowState ? 1 : 0));
send(msgRed.set(redState ? 1 : 0));
// Other tasks in the loop if needed
// ...
// Add a delay to control how often the initial values are sent
delay(5000); // Send initial values every 5 seconds (adjust as needed)
}
void receive(const MyMessage &message) {
// Check which LED to control based on the message received
if (message.sensor == CHILD_ID_GREEN && message.type == V_LIGHT) {
greenState = !greenState; // Toggle the state
digitalWrite(GREEN_PIN, greenState ? HIGH : LOW); // Set the pin accordingly
send(msgGreen.set(greenState ? 1 : 0)); // Report back the new state
} else if (message.sensor == CHILD_ID_YELLOW && message.type == V_LIGHT) {
yellowState = !yellowState;
digitalWrite(YELLOW_PIN, yellowState ? HIGH : LOW);
send(msgYellow.set(yellowState ? 1 : 0));
} else if (message.sensor == CHILD_ID_RED && message.type == V_LIGHT) {
redState = !redState;
digitalWrite(RED_PIN, redState ? HIGH : LOW);
send(msgRed.set(redState ? 1 : 0));
}
}