Using the "Selector switch" in Domoticz (tutorial)


  • Hero Member

    Domoticz features a special switch type named "Selector switch" . This switch type can serve many MySensors purposes. In this walk through it is used for selecting different light patterns in my Wall mounted 'mood light'.
    A "button set"
    0_1474613749068_upload-5c2bdf83-e135-4b01-a842-f8bf16b2c468

    or "select menu"
    0_1474613812167_upload-ff85302a-8b41-4033-8654-f3d85a0a2351

    Essentially it is (or can be) a switch which controls another device. In this case I use it to control a dimmer. Let's start.

    1. Create a virtual hardware device (if you have not already done so before)

    0_1474614096927_upload-fdbc81ca-84db-4f95-8a4c-36d9434bde72

    1. Create a new "Virtual Sensor" with type "Selector Switch" by pressing the "Create Virtual Sensors" button.
      0_1474614259118_upload-dfadaa31-681c-4100-9f65-7e9b77705850
      you can also use the "Manual Light/Switch" button in the "Switches" tab.
      0_1474614203400_upload-fc866b82-9544-43ac-ad46-280721ae59eb

    This will create a new Virtual selector switch.
    0_1474614396110_upload-9189aef8-4502-43ce-bbb5-386dc84d6cb1

    I will use this to control a dimmer device which output is used to set the light patterns in the mood light
    0_1474614481046_upload-803f08fb-dda1-4142-8677-d9fb706ad9bd

    1. In the devices tab find the "Dimmer you want the Selector attached to (in this case RGB Wall Pattern W 62" with index 971
      0_1474614623251_upload-a4eb98fa-e59f-4915-a4aa-a970f8ab57c6

    "Edit" the newly created Selector switch and for each of the selector states attach a command to be executed.

    The commands here can be pretty much anything but here I used the Domoticz JSON command to set the Dimmer to one of the 16 possible states (Yes, a dimmer can only asume 16 states). You need to put your own domoticz URL/IP and port number in there. "Idx" is the dimmer device index from the previous step.

    A maximum of 10 switch values can be assigned
    ❗ don't forget to save 😓

    0_1474614939249_upload-825741bf-c2ee-4538-ad15-7881f12fc4ca
    I deliberately did not set "level 1" , because this translates to "0" and is associated with the off state in my case (room light)

    for the copycats (don't forget to include your own values):

    http://192.168.2.110:8080/json.htm?type=command&param=switchlight&idx=971&switchcmd=Set%20Level&level=0
    

    That's it. Now when you press a selector button it switches the dimmer value.

    In your sketch you can translate the dimmer values to differenct actions. Have a look at the Wall light sketch for an example.


  • Hero Member

    To make the tutorial complete, below the MySensors receive routine to handle the input ( 😉 @Reza )

    // Incoming messages from MySensors
    // Template to demonstrate use of V_PERCENTAGE for Selector Switch in Domoticz
    void receive(const MyMessage &message) {
        int ID = message.sensor;
    	int mySwitch = 0 ;
        Serial.print("Sensor: ");
        Serial.println(ID);
        switch (ID){											// If different sensors for this node
            case sensorID:				                        // the number of the V_PERCENTAGE sensor to be controlled
                if (message.type == V_PERCENTAGE) {             //  Percentage indicates the pattern
                    mySwitch = map(message.getInt(), 0, 100, 0, 15); // mapper dimmer values to Switch states 0..9  and wrap
    				switch (mySwitch){
    					case 0: 
    						{ } // whatever on switch action 0 
    					break ;
    					case 1: 
    						{ } // whatever on switch action 1 
    					break ;
    					case 2: 
    						{ } // whatever on switch action 2 
    					break ;
    					case 3: 
    						{ } // whatever on switch action 3 
    					break ;
    					case 4:
    						{ } // whatever on switch action 4
    					break ;
    					// etc. max switch actions in Domoticz = 10
    				}
                break ;
            }
        }
    }
    


  • @AWI
    my friend , I'm embarrassed , answer in forum
    i'am sorry .
    so i upload this sketch in ir sensor :

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_RF24_CHANNEL 0
    
    
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <IRLib.h>
    
    int RECV_PIN = 8;
    
    #define CHILD_1  3  // childId
    
    IRsend irsend;
    IRrecv irrecv(RECV_PIN);
    IRdecode decoder;
    //decode_results results;
    unsigned int Buffer[RAWBUF];
    MyMessage msg(CHILD_1, V_VAR1);
    
    void setup()  
    {  
      irrecv.enableIRIn(); // Start the ir receiver
      decoder.UseExtnBuf(Buffer);
    
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("IR Sensor", "1.0");
    
      // Register a sensors to  Use binary light for test purposes.
      present(CHILD_1, S_LIGHT);
    }
    
    void loop() 
    {
      if (irrecv.GetResults(&decoder)) {
        irrecv.resume(); 
        decoder.decode();
        decoder.DumpResults();
    
        char buffer[10];
        sprintf(buffer, "%08lx", decoder.value);
        // Send ir result to gw
        send(msg.set(buffer));
      }
    }
    
    
    
    void receive(const MyMessage &message) {
        int ID = message.sensor;
        int mySwitch = 
        Serial.print("Sensor: ");
        Serial.println(ID);
        switch (ID){                                            // If different sensors for this node
            case sensorID:                                      // the V_PERCENTAGE sensor to be controlled
                if (message.type == V_PERCENTAGE) {             //  Percentage indicates the pattern
                    mySwitch = map(message.getInt(), 0, 100, 0, 15); // mapper dimmer values to Switch states 0..9  and wrap
                    switch (mySwitch){
                        case 0: 
                            { } // whatever on switch action 0 
                        break ;
                        case 1: 
                            { } // whatever on switch action 1 
                        break ;
                        case 2: 
                            { } // whatever on switch action 2 
                        break ;
                        case 3: 
                            { } // whatever on switch action 3 
                        break ;
                        case 4:
                            { } // whatever on switch action 4
                        break ;
                        // etc. max switch actions in Domoticz = 10
                    }
                break ;
            }
        }
    

    and in selector switch in Selector actions put this :
    http://192.168.2.110:8080/json.htm?type=command&param=switchlight&idx=971&switchcmd=Set Level&level=0

    so where write the ir codes ?
    thank you


  • Hero Member

    @Reza
    I'm not going to spell it out for you, there needs to be something to investigate yourself... the comments are rather self explaining. So put your actions between the curly brackets..

    case 0:
         { } // whatever on switch action 0 
        break ;


  • @AWI
    thank you , you are kind friend



  • Greetings!

    So i have a ledstrip and i am using your sketch straight off.
    I do not have a physical button connected, but the code for the button is still there.

    I have setup a Selector Switch in domoticz like in your example, but i doesnt work with the values 2,3,4 etc. the patterns doesnt start.
    If i change it to http://192.168.1.30:8090/json.htm?type=command&param=switchlight&idx=157&switchcmd=Set Level&level=25 i get the alarm blinking red and white.

    what are the values for each pattern in your "moodlight" sketch?
    Or do i have to add something to the sketch for it to work?



  • I did the selector <> dimmer mapping in lua:

    --
    -- Heat pump mode selector <> dimmer mapping with lua
    --
    
    commandArray = {}
    
    selectorName = 'Pumppu_Mode'
    selectorId = '332' -- needed for UpdateDevice
    dimmerName = 'Pumppu_ModeSW2'
    
    -- selector -> dimmer
    if(devicechanged[selectorName]) then
      iDimLevel = tonumber(otherdevices_svalues[selectorName])
      if(iDimLevel < 60) then -- not needed anymore?
        commandArray[dimmerName]='Set Level '..iDimLevel
      end
    end
    
    -- dimmer-> selector. UpdateDevice is not triggering this script again
    if (devicechanged[dimmerName]) then
      if (devicechanged[dimmerName] == 'Off') then
        commandArray['UpdateDevice'] = selectorId..'|0|0'
      else
        iCurrentDimLevel = 0
        if(otherdevices[dimmerName] == 'On') then -- can be "On" or something else
          iCurrentDimLevel = tonumber(otherdevices_svalues[dimmerName])
        else
          iCurrentDimLevel = tonumber(string.match(otherdevices[dimmerName], "Set Level: (%d+) %%"))
        end
        
        commandArray['UpdateDevice'] = selectorId..'|'..iCurrentDimLevel..'|'..iCurrentDimLevel
      end
    end
    
    return commandArray```


  • Hello AWI , is it possible to post the hole example sketch ?


Log in to reply
 

Suggested Topics

  • 5
  • 2
  • 4
  • 8
  • 1
  • 5

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts