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

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("item_name,target-value,fade-time-ms")

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

** EDIT: Updated to incorporate Joachim’s suggestions

2 Likes

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

1 Like

One minor correction:

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

should read

if (item.state == NULL) 

    { ...

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

1 Like

Thank’s Joachim - have incorporated both tips and updated the gist.

:warning: item_name is now the first argument - not last.

Hi.
I like the idea behind the script and this matches pretty much, what i need for my automation.
So i created the item using “add item (textual)”-function in OH3.
Then i created an “universaldimmer.rules” file in the “rules” folder of OH andjust copy/paste the whole code block from the second post ino the file.
Now i can see the rule “Universaldimmer” in my rules-list.

To make it work now, i set up a new rule:

triggers:
  - id: "1"
configuration:
  itemName: Bewegungsmelder
  state: ON
type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
id: "3"
configuration:
  itemName: universaldimmer
  command: Dimmer,20,2000,10,Lampe_Helligkeit
type: core.ItemCommandAction

“Bewegungsmelder” is a presence sensor and this seems to work, but my light “Lampe_Helligkeit” keeps off. The log file tells me:

*[ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'universaldimmer-1' failed: The name 'ScriptServiceUtil' cannot be resolved to an item or type; line 48, column 17, length 17 in universaldimmer*

So i don’t reallyknow, whats wrong there, but it seems, the scrips can’t find my light, or the
“ScriptActionHandler” does not work at all …
I’m stuck at this point, could you help me plz?

Well, i did it exactly, like the original creator of the script:

import org.eclipse.smarthome.model.script.ScriptServiceUtil

right at the first line in the rules-file
Is there a way to check whether the import is really successful?
Or is there a dependency which has to be met before? (Java is installed ofcourse)


Also i found another post ([Rules DSL] Get item from string name! which tells, you need to change the import to

import org.openhab.core.model.script.ScriptServiceUtil

And the last part did it! :smiley:
but you need to restart openhab, even the rule is reloaded, as sonn as you save the changes (te log file also shows, that the module is realoaded)

But you’ve shown us a UI entered rule, not a rule from a xxx.rules file as used in 2018? The UI entered rule shares nothing with your import, which is local to the xxx.rules file it is in only.

I don’t think you can do imports like this in UI rules.

But you’ve shown us a UI entered rule, not a rule from a xxx.rules file as used in 2018?

Right.
As described, this is a new rule i created, which calls the text-coded rule, as soon as movement is recognized by the presence sensor :wink:
It’s a bit tricky, but the error was caused by changed import in version 3 in combination with OH not loading the import, when it is not restarted.


Args … the script seems to be not ok anymore (due to changed environment i guess?)
The script runs now, but it’s buggy.

[ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'universaldimmer-1' failed: 'plusMillis' is not a member of 'java.time.ZonedDateTime'; line 69, column 27, length 22 in universaldimmer

The light is dimmed by one step (which is set as the 3rd parameter) and stops then. It’s not repeated until the set destination value (the 1st variable).
So to be precise: when i turn my light on by hand to 40% and start the script with as follows:

Dimmer,0,2000,10,Lampe_Helligkeit

The dimmer is set from 40% to 30%, then 30% to 38%, and again 38% to 30% and the script stops …


Also found a solution for this.
You need to change to lines to make it work nice and smooth :slight_smile:

now.plusMillis(mytime)

must be changed to

now.toInstant().plusMillis(mytime).atZone(now.zone)

You should be able to. If it doesn’t work it’s a bug.