markdown
Passing Fan Speed to Event
I’m working on a Node-RED automation to control a ceiling fan connected to a GE Smart Fan Control switch. My goal is to increment the fan speed when a button on my remote is pressed. Here’s how I approached the solution:
Step 1: Verify Fan Speed Attribute
First, I checked the fan entity in Home Assistant to ensure the speed attribute is exposed. This is crucial for retrieving the current speed.
Step 2: Create Node-RED Flow
I created the following flow in Node-RED:
- Input Button: Simulates the button press from my remote.
- Function Node: Retrieves the current fan speed and increments it.
- Debug Node: Logs the new speed for testing.
- Service Call Node: Sends the new speed to the fan.
Step 3: Function Node Code
Here’s the code for the function node:
javascript
const fanEntity = “fan.my_fan_level”;
const speed = msg.payload;
// Get current fan speed
const currentState = global.get(‘fan_speed’) || ‘low’;
const speeds = [‘low’, ‘medium’, ‘high’];
let currentIndex = speeds.indexOf(currentState);
// Increment speed, wrapping around if necessary
currentIndex = (currentIndex + 1) % speeds.length;
const newSpeed = speeds[currentIndex];
// Update global variable
global.set(‘fan_speed’, newSpeed);
// Prepare message for debug
msg.payload = {
current_speed: currentState,
new_speed: newSpeed
};
return msg;
Step 4: Testing
I tested the flow to ensure it correctly increments the fan speed. If issues arose, such as the speed not updating, I reviewed the nodes and connections to identify and fix the problem.
Conclusion
This setup allows me to control the fan speed using my remote, making my home automation more convenient and user-friendly.