Rule to slowly fading in or out any given item based on variables

Tags: #<Tag:0x00007f6188dfd680> #<Tag:0x00007f6188dfd540>

Yea it is a bit strange, what I tried is to edit the command to universaldimmer.sendCommand(“Dimmer,80,10000,3,Light_DimmerTafellamp”)

Then the dimming stops at 80, so it seems it just using the targetValue as mybrightness but I can’t figure out where it goes wrong.
If I use my trigger item which is just a switch to trigger the test fade rule, it also sets the value to the targetValue that is set in the command…

Assuming your current rule is something like the last version posted,
this is the part of your timer code that should allow escape from a dimming cycle

	// Check if finishes
	if (brightness != targetValue) {
		// if not finished dim again 

Clearly if brightness never equals targetValue there will be no escape. So those values are of interest, let’s find out what they are at each dimming step:

	// Check if finishes
	logInfo("dimtimer", "brightness {} targetValue {}", brightness , targetValue) 
	if (brightness != targetValue) {
		// if not finished dim again

Here is the output of that:

2020-08-30 11:15:09.175 [ome.event.ItemCommandEvent] - Item 'Tafellamp_dimmer' received command 25

2020-08-30 11:15:09.181 [nt.ItemStatePredictedEvent] - Tafellamp_dimmer predicted to become 25

2020-08-30 11:15:09.224 [ome.event.ItemCommandEvent] - Item 'universaldimmer' received command Dimmer,50,6000,2,Light_DimmerTafellamp

2020-08-30 11:15:09.237 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 6 to 25

2020-08-30 11:15:09.289 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 6 to 25

2020-08-30 11:15:10.200 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 39.2 to 40.8

2020-08-30 11:15:13.235 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 40.8 to 41.3

2020-08-30 11:15:16.268 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.3 to 41.9

2020-08-30 11:15:21.324 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.9 to 42.4

2020-08-30 11:15:22.337 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 42.4 to 41.9

2020-08-30 11:15:23.350 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.9 to 42.4

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:24.522 [INFO ] [me.model.script.home.universaldimmer] - Item to dimm: Light_DimmerTafellamp

2020-08-30 11:15:24.542 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 27

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:24.565 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 27

2020-08-30 11:15:24.568 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 27

2020-08-30 11:15:24.581 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 25 to 27

2020-08-30 11:15:24.641 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 25 to 27

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:25.136 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 27 targetValue 50

2020-08-30 11:15:25.172 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 29

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:25.210 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 29

2020-08-30 11:15:25.218 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 29

2020-08-30 11:15:25.241 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 27 to 29

2020-08-30 11:15:25.310 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 27 to 29

2020-08-30 11:15:25.369 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 42.4 to 41.9

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:25.759 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 29 targetValue 50

2020-08-30 11:15:25.777 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 31

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:25.793 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 31

2020-08-30 11:15:25.796 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 31

2020-08-30 11:15:25.809 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 29 to 31

2020-08-30 11:15:25.869 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 29 to 31

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:26.358 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 31 targetValue 50

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:26.395 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.9 to 41.3

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:26.420 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 33

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:26.455 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 33

2020-08-30 11:15:26.461 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 33

2020-08-30 11:15:26.481 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 31 to 33

2020-08-30 11:15:26.552 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 31 to 33

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:27.011 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 33 targetValue 50

2020-08-30 11:15:27.033 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 35

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:27.055 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 35

2020-08-30 11:15:27.057 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 35

2020-08-30 11:15:27.071 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 33 to 35

2020-08-30 11:15:27.137 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 33 to 35

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:27.613 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 35 targetValue 50

2020-08-30 11:15:27.648 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 37

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:27.682 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 37

2020-08-30 11:15:27.687 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 37

2020-08-30 11:15:27.706 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 35 to 37

2020-08-30 11:15:27.760 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 35 to 37

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:28.232 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 37 targetValue 50

2020-08-30 11:15:28.248 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 39

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:28.264 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 39

2020-08-30 11:15:28.267 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 39

2020-08-30 11:15:28.282 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 37 to 39

2020-08-30 11:15:28.348 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 37 to 39

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:28.832 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 39 targetValue 50

2020-08-30 11:15:28.862 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 41

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:28.892 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 41

2020-08-30 11:15:28.896 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 41

2020-08-30 11:15:28.925 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 39 to 41

2020-08-30 11:15:28.985 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 39 to 41

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:29.445 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 41 targetValue 50

2020-08-30 11:15:29.461 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 43

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:29.479 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 43

2020-08-30 11:15:29.483 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 43

2020-08-30 11:15:29.497 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 41 to 43

2020-08-30 11:15:29.560 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 41 to 43

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:30.042 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 43 targetValue 50

2020-08-30 11:15:30.081 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 45

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:30.120 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 45

2020-08-30 11:15:30.126 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 45

2020-08-30 11:15:30.141 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 43 to 45

2020-08-30 11:15:30.227 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 43 to 45

2020-08-30 11:15:30.448 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.3 to 40.8

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:30.677 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 45 targetValue 50

2020-08-30 11:15:30.699 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 47

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:30.715 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 47

2020-08-30 11:15:30.719 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 47

2020-08-30 11:15:30.734 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 45 to 47

2020-08-30 11:15:30.784 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 45 to 47

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:31.280 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 47 targetValue 50

2020-08-30 11:15:31.293 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 49

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:31.310 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 49

2020-08-30 11:15:31.314 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 49

2020-08-30 11:15:31.344 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 47 to 49

2020-08-30 11:15:31.388 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 47 to 49

2020-08-30 11:15:31.455 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 40.8 to 41.9

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:31.875 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 49 targetValue 50

2020-08-30 11:15:31.912 [INFO ] [me.model.script.home.universaldimmer] - Dimm Light_DimmerTafellamp (Dimmer) to 50

==> /var/log/openhab2/events.log <==

2020-08-30 11:15:31.945 [ome.event.ItemCommandEvent] - Item 'Light_DimmerTafellamp' received command 50

2020-08-30 11:15:31.953 [nt.ItemStatePredictedEvent] - Light_DimmerTafellamp predicted to become 50

2020-08-30 11:15:31.987 [vent.ItemStateChangedEvent] - Light_DimmerTafellamp changed from 49 to 50

2020-08-30 11:15:32.042 [vent.ItemStateChangedEvent] - Tafellamp_dimmer changed from 49 to 50

2020-08-30 11:15:32.465 [vent.ItemStateChangedEvent] - LocalRaspberry_Sensors_CPUTemperature changed from 41.9 to 40.8

==> /var/log/openhab2/openhab.log <==

2020-08-30 11:15:32.502 [INFO ] [ipse.smarthome.model.script.dimtimer] - brightness 50 targetValue 50

The dimmer was at 6, then I changed the item Tafellamp_dimmer to 25 what triggered the rule Test Fade:

rule "Test fade"

when

Item Tafellamp_dimmer received command

then

universaldimmer.sendCommand("Dimmer,50,6000,2,Light_DimmerTafellamp")

end

I changed the targetValue to 50 because 100 is really bright in here :slight_smile:
The other strange thing is that it takes several seconds before the universal dimmer rule gets executed, at that time the light is already changed to it’s new brightness.

That all seems to have done everything it was told, and stopped at 50.
If you think there is a problem with 100 it would probably be smart to test again at 100.

There is always a first-time rule run delay after editing (or a reboot), it has to compile or something.

Again I think it has done what it was told. So far as I can see your Items Tafellamp_dimmer and Light_DimmerTafellamp are linked to the same physical device? I deduce that because when one updates, so does the other.

So if you send Tafellamp_dimmer a command, it gets passed to the device, and that will presumably do as it was told.
Meantime the command has also triggered a rule that in turn kicks off the autodimming.

I can’t see what you are trying to achieve here at all.

Well it almost does what I would like to see it do. What I thought the rule/script was for, it to dim in/out to every given value when you change the light.
So for example, when I change the light with the dimmer in HABpanel from 6 to 60, I would love to see it slowly dim the light up instead of a sea of light all of a sudden.

Will something like that be possible with my dummy Dimmer testD that I mentioned before? That item isn’t linked to a physical lamp, but, for example when I change the testD dimmer from 4 to 25, I would like to fire the Test Fade rule but the command must be universaldimmer.sendCommand(“Dimmer,25,6000,2,Light_DimmerTafellamp”) then.

So the targetValue should use the value off the testD item.
Can you do something like that with a var number statement?

Yes. You’d want to uncouple the UI slider or switch or whatever from the real device.

Sure.
Your “intermediate” rule would trigger off commands that you send to your testD from UI. It would get the numeric value of the command (you really do not care about testD state). You would substitute that numeric value into the string that you send to the autodimming item. You can make strings up with methods like
"this part," + numeric.toString + ", that part"

When watching what happens in your events.log, bear in mind that using a UI slider can send multiple commands when used, e.g. 20 then 30 then 40.
Your rules will fire multiple times, but so far as I can see it should all work out as the auto-dimming rule should be managing just one timer (per target Item)

Thanks a lot for your patience and clarification :slight_smile: learnt a lot this weekend and it is working now.

This is the final rule and it works like I attended!

rule "Test fade"

when

Item testD received command

then

universaldimmer.sendCommand("Dimmer,"+testD.state.toString+",3000,1,Light_DimmerTafellamp")

end

I first tried "+ numeric.toString +" but then I saw in the command it just says the items it’s name in the command, when I edited it to "+ numeric.state.toString +"it sends the fake dimmer it’s value as a targetValue.

You’ll have to wait is is done with his dimming before you can change it value again but I have set it to 3 seconds so you won’t have t wait that long :slight_smile:

'numeric" was just an example. I’m usually more explicit with yourNumericValue or something.

Caution;

These are two different things. Commands are NOT states.
Commanding an Item may eventually result in a state change.
Examining the state immediately after a command may give you the before or after state, it’s pot luck with timing.

Who cares - don’t look at the state! Just use the command.directly
... "Dimmer,"+ receivedCommand.toString+",3000 ...

  • and in this case receivedCommand is the name of a real variable that is automatically available in rules triggered by commands.

I know, it was also a example from me, I replaced numeric with the testD in my rule :wink:

Hehe nice, didn’t know that this was possible but that’s indeed even more clever then looking at the state of the item, just edited the rule again and it seems to work quite well, it looks really nice :smiley: much better than the normal light switching! Thanks!

I’ve been using @tbnobody’s version but have been having problems with it going into infinite loops. Maybe it was because I set the timer to repeat too agressively but I couldn’t work out why so decided to iterate on the approach.

Rather than bump the value up or down a stepSize on every timer tick, mine calculates the required brightness at the time of the tick using linear regression (Y=Ax + B) and will always exit within the required fade time.

Usage

universaldimmer.sendCommand("Dimmer|Color,target-value,fade-time-ms,item_name")

YMMV. Happy to receive feedback/PRs. I’m a solid coder but little experience in Java.

1 Like

Hello,

nice idea.

I have made a first Version as Python Script. It have the same Syntax, so you can change to NGR.
Only DIMMER is active, but the Function have one more switch, to regulate the Steps in Milliseconds.

I hope for Testinpeople.
The Script are availabe here:

All thanky by PeterK, it is his idea, i´m only convert it.

Best Thanks

Thanks for the code - works very well!

One minor suggestion for improvement:
Instead of having “type” as a parameter you can retrieve it from the item itself

// item Type: HSBType->Color or PercentType->Dimmer

val item_type = item.getAcceptedDataTypes().get(0).toString.split(" ").get(1).toString.split("\\.").get(6).toString

var int startingBrightness = 0

if (item_type == "HSBType") {     // type == "Color"
        startingBrightness = (item.state as HSBType).getBrightness().intValue
} else if (item_type == "PercentType") { // type == "Dimmer" 
        startingBrightness = (item.state as DecimalType).intValue
}

Example:
item.AccecptedDataTypes: [class org.eclipse.smarthome.core.library.types.PercentType, class org.eclipse.smarthome.core.library.types.OnOffType, class org.eclipse.smarthome.core.types.UnDefType]
thereof get(0): “class org.eclipse.smarthome.core.library.types.PercentType”
thereof split(" “).get(1): “org.eclipse.smarthome.core.library.types.PercentType”
thereof split(”\.").get(6): “PercentType”

This is e.g. useful for dimming a group of lights of different types (Color or Dimmer)

gLight.members.forEach[ i | universaldimmer.sendCommand("100,10000," + i.name) ]

Kind regards, Joachim

One minor correction:

if (item.state == null) 
    { ...

should read

if (item.state == NULL) 

    { ...

(see also [SOLVED] NULL | UNDEF | null | uninitialized)