Dimmer Rule for Fibaro unit to Dim by a percentage till 0 (Audio Visual Scene)

hi team,

Im doing my best to work on a Scene rule which basically turns my AV equipment on and dims my lights, if they are on to 0%.

Essentially if the Dimmer is greater than 0, whatever the value, dim by 5% until the dimmer = 0

Ive got about this far :slight_smile:



rule "Watch a Netflix movie, Dim the Lights and Turn All AV Equipment on"
when
        Item Scene_Watch_Netflix changed to ON
then
       //Switch on TV
       if (TVStatus.state == OFF) {
              sendCommand(TV, ON)
       //Switch On AVR
       }
       else if (Yamaha_Power.state == OFF) {
              sendCommand(Yamaha_Power, ON)
       //Change AVR Scene to Movie
       }
       else if ((Zone_1_Scene.state != "Scene 2" || Zone_1_Scene.state != "Scene 3")) {
              sendCommand(Zone_1_Scene, 'Scene 1')
       //Slowly Dim the Lights if they are above 50%
       }
       else if (LivingRoomSw1.state == ON && LivingRoomDim1.state < 50) {
              sendCommand(LivingRoomDim1, 50)
              Thread::sleep(3000)
              sendCommand(LivingRoomDim1, 40)
              Thread::sleep(3000)
              sendCommand(LivingRoomDim1, 30)
              Thread::sleep(3000)
              sendCommand(LivingRoomDim1, 20)
              Thread::sleep(3000)
              sendCommand(LivingRoomDim1, 10)
              Thread::sleep(3000)
              sendCommand(LivingRoomDim1, 0)
       //Change Input to Netflix
       }
       else if (TVStatus.state == ON) {
              TV.sendCommand(TVNetflix)
       }
end



I believe I see one issue that stares at me. If I am understanding what you are trying to do, I believe your “else if” need to be if.

The reason is this written as language. What you want to say is:

if the tv is off turn it on.

If the receiver is off, turn it on

Etc…

Using else if would do this. I believe you are saying:

If the tv is off turn it on.

Done!

There would be no reason to pass to the else if when the first condition is true. I am hoping this makes sense, and I am giving you the behavior you desire.

Not knowing exactly what yours scenes do, the syntax looks correct, but I can’t comment if you will have the desired output. You may examine the sleeps though they are rather large.

Hope my feedback helps! Great start on the rule.

2 Likes

Thanks @Thedannymullen

Cool, I suspected that may be the case. I havent tested it as yet, but will tonight. Ill have a read of the sleep article. I couldnt think of a way to slowly dim the lights and have a delay. I might use the Timer method :slight_smile:

1 Like

Once you test it, post back here. Lots of smart people will give you feedback. The important part is you are trying and learning like all of us!

I am just getting started developing my rules also. It’s definitely the more difficult part!

1 Like

The rules bit is kinda fun!

I saw some posts with better use of the Timers like you’ve suggested so I will try this:



var int percent = 0
var Timer fade_timer = null

rule "Watch a Netflix movie, Dim the Lights and Turn All AV Equipment on"
when
        Item Scene_Watch_Netflix changed to ON
then
       //Switch on TV
       if (TVStatus.state == OFF) {
              sendCommand('TV, TVOnOff')
              logInfo("TV switched on as a result of Netflix scene")
       //Switch On AVR
       }
       if (Yamaha_Power.state == OFF) {
              sendCommand(Yamaha_Power, ON)
              logInfo("AVR switched on as a result of Netflix scene")
       //Change AVR Scene to Movie
       }
       if ((Zone_1_Scene.state != "Scene 2" || Zone_1_Scene.state != "Scene 3")) {
              sendCommand(Zone_1_Scene, 'Scene 1')
              logInfo("AVR Scene set to Netflix as a result of Netflix scene being pressed")
       //Slowly Dim the Lights if they are above 50%
       }
       if (LivingRoomSw1.state == ON) {
         if (fade_timer !== null) {
          percent = LivingRoomDim1.state
         fade_timer = createTimer(now.plusMillis(500)) [ |
              LivingRoomDim1.sendCommand(percent)
          if (percent > 0) {
              percent = percent - 5
              fade_timer.reschedule(now.plusMillis(500))
          }
        ]
       }
       }
       //Change Input to Netflix
       if (TVStatus.state == ON) {
              TV.sendCommand(TVNetflix)
              logInfo("Input on TV changed to Netflix")
       }
end


1 Like

Tried to test the rule but the log shows this. @vzorglub any thoughts rule guru?

14:17:36.852 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Watch a Netflix movie, Dim the Lights and Turn All AV Equipment on': An error occurred during the script execution: index=1, size=1

Ok, we can tidy up a bit to start with:

var Number percent = 0
var Timer fade_timer = null

rule "Watch a Netflix movie, Dim the Lights and Turn All AV Equipment on"
when
        Item Scene_Watch_Netflix changed to ON
then
    var String scene = "Netflix scene"
    //Switch on TV
    if (TVStatus.state == OFF) {
        TV.sendCommand("TVOnOff")
        logInfo(scene, "TV switched on")
    }
    //Switch On AVR
    if (Yamaha_Power.state == OFF) {
        Yamaha_Power.sendCommand(ON)
        logInfo(scene, "AVR switched on")
    }
    //Change AVR Scene to Movie
    if ((Zone_1_Scene.state != "Scene 2" || Zone_1_Scene.state != "Scene 3")) {
         Zone_1_Scene.sendCommand("Scene 1")
         logInfo(scene, "AVR Scene set to Netflix")
    }
    //Slowly Dim the Lights if they are above 50%
    if (LivingRoomSw1.state == ON) {
        if (fade_timer === null) {
           percent = LivingRoomDim1.state as Number
           fade_timer = createTimer(now.plusMillis(500), [ |
                if (percent > 0) {
                    percent = percent - 5
                    LivingRoomDim1.sendCommand(percent)
                    fade_timer.reschedule(now.plusMillis(500))
                } else {
                    fade_timer = null
                }
            ])
        }
    }
    //Change Input to Netflix
    if (TVStatus.state == ON) {
        TV.sendCommand("TVNetflix")
              logInfo(scene, "Input on TV changed to Netflix")
    }
end

Thx Vincent! I must be blind, I cant see where youve used the Netflix string??

:smiley:

In the logInfo, makes it simpler

1 Like

Nice!!

OK, its working mostly.

Saw this:


18:46:18.020 [WARN ] [arthome.model.script.actions.BusEvent] - Cannot convert '-1' to a command type which item 'LivingRoomDim1' accepts: [PercentType, OnOffType, IncreaseDecreaseType, RefreshType].

The diming works, but doesnt switch off when it gets to 0.

    //Slowly Dim the Lights if they are above 50%
    if (LivingRoomSw1.state == ON) {
        if (fade_timer === null) {
           percent = LivingRoomDim1.state as Number
           fade_timer = createTimer(now.plusMillis(500), [ |
                if (percent > 0) {
                    percent = percent - 5
                    if (percent < 0) percent = 0
                    LivingRoomDim1.sendCommand(percent)
                    fade_timer.reschedule(now.plusMillis(500))
                } else {
                    fade_timer = null
                }
            ])
        }
    }
2 Likes

Nice!!! That has fixed the dimming.

Not switching the TV input… Looking into that now…

I can only assume the TVStatus is not being updated to ON fast enough during execution, which means its not changing the Input to Netflix. Even after I tweaked the Network Binding to be very fast! :confused:

It appears the TVStatus isnt updating fast enough, after it comes online. Soon as you switch it on, its pingable but the TVStatus shows OFF for ages…

Edit the thing, the polling frequency is in the thing settings, not the binding

1 Like

I did… i think

This looks fairly aggressive in terms of timing, no?

Yes, something is wrong.
Why do you have 3 items linked to the same channel?

Comment out the { channel=“xxxx” } from your item definition
Save
Check the linked items on PaperUI, It should be removed
Add it back on

OK fixed that, but its taking AGES to update the item. Few minutes… Ive removed the thing and am restarting OH2.

Same issue, takes minutes.

This is the item:

Switch TVStatus { channel="network:pingdevice:192_168_0_200:online" }

But the documentation suggests something else…

Not sure, never had problems with it.
Have your tried putting the binding on debug?

not sure how…log:set DEBUG org.openhab.binding.networkbinding fails :.

log:set DEBUG org.openhab.binding.network