Call/Trigger custom functions/effects from Home Assistant



  • Hey!
    I'm currently playing around mysensors lights and Home Assistant being able to control rgb color values and brightness, and it works great with the below sketch!

    However, does anyone know how you can call your own "functions" if I add them? Like I want to add rainbow effects, different mood color animations etc, but how would I call for it from Home Assistant? Do I add a custom "mymessage"? I would love if you could have a list in Home Assistant when clicking the light (that is currently added with the sketch below) where you can trigger different effects. I am not sure how to approach this and how to trigger them with a mysensors setup. If anyone is familiar with this I would appreciate it.

    // Enable debug prints
    #define MY_DEBUG
    
    #define MY_RADIO_NRF24
    
    #define MY_NODE_ID 6
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <FastLED.h>
    
    #define CHILD_ID 5                  // Child Id's
    
    const int stripPin = 4 ;                  // pin where 2812 LED strip is connected
    const int numPixel = 2 ;                  // set to number of pixels
    
    CRGB leds[numPixel];
    
    char actRGBvalue[] = "000000";               // Current RGB value
    uint16_t actRGBbrightness = 0xFF ;         // Controller Brightness 
    int actRGBonoff=0;                        // OnOff flag
    
    MyMessage dimmerMsg(CHILD_ID, V_PERCENTAGE);
    MyMessage lightMsg(CHILD_ID, V_STATUS);
    MyMessage rgbMsg(CHILD_ID, V_RGB);
    
    MyMessage lastColorStatusMsg(CHILD_ID,V_VAR1);
    
    void setup() {
      FastLED.addLeds<NEOPIXEL, stripPin >(leds, numPixel); // initialize led strip
    
     // begin(incomingMessage, NODE_ID, false);      // initialize MySensors
    
      // Flash the "hello" color sequence: R, G, B, black. 
      colorBars();
    
      //Request the last stored colors settings
      request(CHILD_ID, V_VAR1);
      wait(2000);
      
      send(lightMsg.set(actRGBbrightness > 0 ? 1 : 0));
      send(dimmerMsg.set(actRGBbrightness));
      send(rgbMsg.set(actRGBvalue));
      send(lastColorStatusMsg.set("0"));
    }
    
    void presentation() {
      sendSketchInfo("RGBLigtTest", "1.1");
      present(CHILD_ID, S_RGB_LIGHT);        // present to controller
    }
    
    void loop() {
    }
    
    void receive(const MyMessage &message) {
        if (message.type == V_RGB) {            // check for RGB type
        actRGBonoff=1;
        strcpy(actRGBvalue, message.getString());    // get the payload
        SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
        SendLastColorStatus();
      }
      else if (message.type == V_DIMMER) {           // if DIMMER type, adjust brightness
        actRGBonoff=1;
        actRGBbrightness = map(message.getLong(), 0, 100, 0, 255);
        FastLED.setBrightness( actRGBbrightness );
        SendLastColorStatus();
      }
      else if (message.type == V_STATUS) {           // if on/off type, toggle brightness
        actRGBonoff = message.getInt();
        FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
        SendLastColorStatus();
      }
      else if (message.type==V_VAR1) {            // color status
        String szMessage=message.getString();
        strcpy(actRGBvalue, getValue(szMessage,'&',0).c_str());
        actRGBbrightness=atoi(getValue(szMessage,'&',1).c_str());
        actRGBonoff=atoi(getValue(szMessage,'&',2).c_str());
        SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
        FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
      }
      FastLED.show();
      }
    
    void colorBars()
    {
      SendColor2AllLEDs( CRGB::Red );   FastLED.show(); delay(500);
      SendColor2AllLEDs( CRGB::Green ); FastLED.show(); delay(500);
      SendColor2AllLEDs( CRGB::Blue );  FastLED.show(); delay(500);
      SendColor2AllLEDs( CRGB::Black ); FastLED.show(); delay(500);
    } 
    
    void SendColor2AllLEDs(const CRGB lcolor)
    {
      for(int i = 0 ; i < numPixel ; i++) {
        leds[i] = lcolor;
      }
    }
    
    void SendLastColorStatus()
    {
      String cStatus=actRGBvalue+String("&")+String(actRGBbrightness)+String("&")+String(actRGBonoff);
      send(lastColorStatusMsg.set(cStatus.c_str()));
    }
    
    String getValue(String data, char separator, int index)
    {
     int found = 0;
      int strIndex[] = {0, -1};
      int maxIndex = data.length()-1;
      for(int i=0; i<=maxIndex && found<=index; i++){
      if(data.charAt(i)==separator || i==maxIndex){
      found++;
      strIndex[0] = strIndex[1]+1;
      strIndex[1] = (i == maxIndex) ? i+1 : i;
      }
     }
      return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
    }
    
    void FastledCustomEffect1()
    {
    // add custom fastled effect
    }
    
    void FastledCustomEffect2()
    {
    // add custom fastled effect
    }
    

  • Plugin Developer

    @Emil-Besirevic

    Hi!

    You can present a "dummy" binary device, S_BINARY, in the same sketch as your RGB light. The dummy device will be shown as a switch in the HA gui, and you can turn it on/off. You add logic in the receive/incomingMessage function in the sketch to take appropriate action when a message for that device comes in. You'll basically add parts of this sketch:
    https://home-assistant.io/components/switch.mysensors/#switch-sketch



  • That makes sense! Awesome! Thanks for the quick reply! 🙂


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 2
  • 5
  • 3
  • 8

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts