Dimmer Light Rules / Command Help

Yes, saw those, and just copied and pasted the whole code and tried again, but still nothing.

The quick simple one works though, every time.

Strange.

Really strange :slight_smile:
Is there any error log in openhab.log about the not working rule? You can force logging some values
set this line

logInfo ("fadeout","percent = {}",percent)

right below Light_GF_Living_Planets_1.sendCommand (percent) to see if percent is decremented.

Ah. I just saw a flaw… If the Light is ON, maybe the state isn’t an instance of DecimalType.

[...]
then
    if (Light_IN_Frontroom_Birds.state !instanceof DecimalType && Light_IN_Frontroom_Birds.state != OFF)
        Light_IN_Frontroom_Birds.sendCommand(95)
    percent = ((Light_IN_Frontroom_Birds.state as DecimalType)/5).intValue * 5 //round to 5
    fade_Timer = createTimer(now.plusMillis(500)) [|
        Light_IN_Frontroom_Birds.sendCommand(percent)
        if (percent > 0) {
            percent = percent - 5
            fade_Timer.reschedule(now.plusMillis(500))
        }
    ]
end

Just the interesting part…

Also beware the typo. Shouldn’t that be fade_Timer.reschedule(now.plusMillis(500)) ?

yes, did not see this in first place :slight_smile:

Hey Udo,
Can you help me for the below topic?
I want to setup a Dimmer item, with which I waant to bind the http binding, I can use the rule present in the demo.rules file, but not sure how to send the percentage value of it to my http server running on Raspberry pi3. Any Ideas will be appriciated.

Well, given a dimmer item bound to http, this should be very easy…

Dimmer myDimmer "My http Dimmer is [%d %%]" {http=">[*:POST:http://www.domain.org/home/lights/23871/?status=%2$s&type=text] <[http://www.domain.org/weather/openhabcity/daily:60000:REGEX(.*?<title>(.*?)</title>.*)]"}

(Item definition from documentation)

Now to change the value, either use a slider in Basic UI or something similar, with “real” sliders, or use INCREASE/DECREASE commands and a rule to set the value:

rule "My http dimmer"
when
    Item myDimmer received command
then
   if (receivedCommand == DECREASE) {
        myDimmer.sendCommand((myDimmer.state as DecimalType) - 5)
}
    else if (receivedCommand == INCREASE) {
        myDimmer.sendCommand((myDimmer.state as DecimalType) + 5)
}
end
1 Like

Udo,
Thanks for your reply,
My setup is as below,

Dimmer Led_R “Red” (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/%2$s]"}

and rules,

rule "RED LED"
when
Item Led_R received command
then
var Number b = 0
if(Led_R.state instanceof DecimalType) b = Led_R.state as DecimalType

	if(receivedCommand==INCREASE) b = b + 10
	if(receivedCommand==DECREASE) b = b - 10

	if(b<0)   b = 0
	if(b>100) b = 100
            
            sendCommand(Led_R, b)
			
end

can you point any mistake, currently it is not working

Hey Udo,
I tried your hings and I got the success bu I get never ending loops, can you suggest how to stop them?

I have no idea, why you get a loop. you can try to use

when
    Item myDimmer received command DECREASE or
    Item myDimmer received command INCREASE
then

as the trigger part, but my rule took care of this, because there would have been only a sendCommand if the received Command was INCREASE or DECREASE, this should suffice (in fact my rule will trigger itself, but the received command will be of Type Number, but not INCREASE/DECREASE, so when triggered from itself, it should do nothing at all).
On the contrary, your rule from posting #14 would possibly loop forever, as it will send a command, even if it did not receive INCREASE/DECREASE, but also ON/OFF or 0 to 100.

Thank you very much Udo, don’t know what I did wrong, but it is working with your rule.I changed nothing in it just wrote the whole code allover again and it is working now.

Udo,
when I do the following,

Dimmer Led_R   "Red"  (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/?status=2$s&type=text]"}

it send the the whole link including’?status=2$s&type=text’ instead of the value of b
I want to send it as following,
192.168.0.108:5000/red3/50, instead of 50 there should be the value of variable created in the rule.

If you look at Udo’s example, it has a % symbol right about where you want to substitute the value.
You just have to take care with these things.

I also have included ‘%’ but it is not working.Here are my codes.

Item.

Dimmer Led_R			     "Red"		 (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/?status=%2$s&type=text]"}

and the rules,

rule "dimmer Led-R"
when
    Item Led_R received command
then
   if (receivedCommand == DECREASE) {
        Led_R.sendCommand((Led_R.state as PercentType) - 5)
}
    else if (receivedCommand == INCREASE) {
        Led_R.sendCommand((Led_R.state as PercentType) + 5)
}
        
end

I want to send the following link via http binding to my server,
192.168.0.108:5000/red3/(the value from dimmer )
can you suggest any changes?

Oh, come on. “not working” is so unhelpful. What, if anything, does it send now? How are you sending INCREASE/DECREASE commands? Does the rule trigger?

I send the Increase, decrease command through Slider -dimmer item which I posted above.
It does not send the value of ‘b’, but it sends the whole link “192.168.0.108:5000/red3/?status=%2$s&type=text” I have also posted my rules and Item settings in the previous comment.

Maybe you have to set the label of the item, so please try

Dimmer Led_R "Red [%d %%]" (FF_Nishit, Lights){http=">[*:GET:http://192.168.0.108:5000/red3/%2$s]"}

(removed spaces for better readability)

Udo,
When I move the slider from the Ios app, my python scripts replies ’ Could not convert string to float’
Kindly note that I need to use float value from the dimmer,
I also tried int, but when I move the slider, my python script replies 'invalid literal for int90 base 10 : 2$s’
also tried using 2$d, but getting the same error,
So how to send the int or float value directly?

Udo,
Can you help me with making a Main voltage dimmer
i has got the module done but iam not so good in programming,
I have an arduino sketch for the dimmer.
but i dont know how to make it work for the openhab2,
This is my script for 8channel dimmer.
i wanted to control each channel

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/
 
// MAC Adresse des Ethernet Shields
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
// IP des MQTT Servers
byte server[] = { 192, 168, 1, 12 };
// Ethernet Client zur Kommunikation des MQTT Clients
EthernetClient ethClient;
EthernetClient Client;
IPAddress broker(192,168,1,12);
// MQTT Client zur Kommunikation mit dem Server
// Server - Variable des Types byte mit Serveradresse
// 1883 - Ist der Standard TCP Port
// callback - Function wird aufgerufen wen MQTT Nachrichten eintreffen. Am ende des Sketches
// ethClient - Angabe des Ethernet Clients
PubSubClient client(ethClient);
 
// Timervariable für eine Verzögerung. Als alternative zu delay was die verarbeitung anhält.
int timer = 0;
int EndTimer = 200; 
 
// Pins des DIMMER
int dim1=4;
int dim2=5;
int dim3=6;
int dim4=7;
int dim5=8;
int dim6=9;
int dim7=10;
int dim8=11;
// Übermittelte Farbwerte
int dim1Value = 0;
int dim2Value = 0;
int dim3Value = 0;
int dim4Value = 0;
int dim5Value = 0;
int dim6Value = 0;
int dim7Value = 0;
int dim8Value = 0;
// Gerade gesetzte Farbe
int CURdim1Value = 0;
int CURdim2Value = 0;
int CURdim3Value = 0;
int CURdim4Value = 0;
int CURdim5Value = 0;
int CURdim6Value = 0;
int CURdim7Value = 0;
int CURdim8Value = 0;
void setup()
{
 // Setzen der PINS als Ausgang
 pinMode(dim8, OUTPUT);
 pinMode(dim7, OUTPUT);
 pinMode(dim6, OUTPUT);
 pinMode(dim5, OUTPUT);
 pinMode(dim4, OUTPUT);
 pinMode(dim3, OUTPUT);
 pinMode(dim2, OUTPUT);
 pinMode(dim1, OUTPUT);
 
 // Bei start Farbe Blau setzen
 analogWrite(dim1, 0);
 analogWrite(dim2, 0);
 analogWrite(dim3, 0);
 analogWrite(dim4, 0);
 analogWrite(dim5, 0);
 analogWrite(dim6, 0);
 analogWrite(dim7, 0);
 analogWrite(dim8, 0);
 // Initialisierung des Ethernets
 if (Ethernet.begin(mac) == 0) {
 
 // Wenn DHCP fehlschlägt dann rot setzen und aufhören
 analogWrite(dim1, 0);
 analogWrite(dim2, 0);
 analogWrite(dim3, 0);
 analogWrite(dim4, 0);
 analogWrite(dim5, 0);
 analogWrite(dim6, 0);
 analogWrite(dim7, 100);
 analogWrite(dim8, 100);
 while (true);
 }
 else {
 // Wenn DHCP OK ist dann grün setzen
 dim2Value = 100;
 dim7Value = 100
 }
 
}
 
void loop()
{
 // Aufbau der Verbindung mit MQTT falls diese nicht offen ist.
 if (!client.connected()) {
 client.connect("arduinoClient");
 // Abonieren von Nachrichten mit dem angegebenen Topics
 client.subscribe("/dd16/dimmer/1/#");
 client.subscribe("/dd16/dimmer/2/#");
 client.subscribe("/dd16/dimmer/3/#");
 client.subscribe("/dd16/dimmer/4/#");
 client.subscribe("/dd16/dimmer/5/#");
 client.subscribe("/dd16/dimmer/6/#");
 client.subscribe("/dd16/dimmer/7/#");
 client.subscribe("/dd16/dimmer/8/#");
 // Alternative Abonierung aller Topics unter /openHAB/Nachtlicht
 // mqttClient.subscribe("/openHAB/Nachtlicht/#");
 }
 
 if (timer <= EndTimer) timer++;
 else {
 timer = 0;
 
 if (dim1Value < CURdim1Value) CURdim1Value--;
 else if (dim1Value > CURdim1Value) CURdim1Value++;
 
 if (dim2Value < CURdim2Value) CURdim2Value--;
 else if (dim2Value > CURdim2Value) CURdim2Value++;
 
 if (dim3Value < CURdim3Value) CURdim3Value--;
 else if (dim3Value > CURdim3Value) CURdim3Value++;

 if (dim4Value < CURdim4Value) CURdim4Value--;
 else if (dim3Value > CURdim3Value) CURdim3Value++;

 if (dim5Value < CURdim5Value) CURdim5Value--;
 else if (dim5Value > CURdim5Value) CURdim5Value++;

 if (dim6Value < CURdim6Value) CURdim6Value--;
 else if (dim6Value > CURdim6Value) CURdim6Value++;

 if (dim7Value < CURdim7Value) CURdim7Value--;
 else if (dim7Value > CURdim7Value) CURdim7Value++;

 if (dim8Value < CURdim8Value) CURdim8Value--;
 else if (dim8Value > CURdim8Value) CURdim8Value++;
 }
 
 analogWrite(dim1, CURdim1Value);
 analogWrite(dim2, CURdim2Value);
 analogWrite(dim3, CURdim3Value);
 analogWrite(dim4, CURdim4Value);
 analogWrite(dim5, CURdim5Value);
 analogWrite(dim6, CURdim6Value);
 analogWrite(dim7, CURdim7Value);
 analogWrite(dim8, CURdim8Value);
 client.loop(); // Schleife für MQTT
 
}
 
// ===========================================================
// Callback Funktion von MQTT. Die Funktion wird aufgerufen
// wenn ein Wert empfangen wurde.
// ===========================================================
void callback(char* topic, byte* payload, unsigned int length) {
 // Zähler
 int i = 0;
 // Hilfsvariablen für die Convertierung der Nachricht in ein String
 char message_buff[100];
 
 // Kopieren der Nachricht und erstellen eines Bytes mit abschließender \0
 for(i=0; i<length; i++) {
 message_buff[i] = payload[i];
 }
 message_buff[i] = '\0';
 
 // Konvertierung der nachricht in ein String
 String msgString = String(message_buff);
 
 // Überprüfung des Topis und setzen der Farbe je nach übermittelten Topic
 if (String(topic) == "/dd16/dimmer/1") dim1Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/2") dim2Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/3") dim3Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/4") dim4Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/5") dim5Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/6") dim6Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/7") dim7Value = round(msgString.toInt() * 2.55);
 if (String(topic) == "/dd16/dimmer/8") dim8Value = round(msgString.toInt() * 2.55);
}

Sorry I don’t use mqtt yet, so I could only guess. But of course you will have to use a mqtt broker (e.g. mosquitto) for communication, the mqtt binding and the correct settings. There is lot of stuff about mqtt in this forum…

Oké no problem,
The mqtt setup is done ect