Author Topic: Scene with dimming increment  (Read 448 times)

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Scene with dimming increment
« on: September 12, 2017, 02:12:09 pm »
Hi!

I would like to create a manually triggered scene that increase dimming level by 10 % from current level. Found a piece of LUA and tried to puzzle something together. Does not work though.... any thoughts?

Would be very grateful for your input, code looks as per below:

local function Dimma()

local dID = 186

local mindim = 0
local maxdim = 100

local switchOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device)
local lightLevel =luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device)

if (switchOnOff == "1") then
              lightLevel =lightLevel-10
              luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = lightLevel}, device)                 
      else
              lightLevel =100 
              luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = lightLevel}, device) 
end           

return Dimma()

Offline Majimus

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +3/-0
Re: Scene with dimming increment
« Reply #1 on: September 12, 2017, 02:50:03 pm »
When you read lightlevel you need to convert to a number before the arith (at a quick glance)?

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Re: Scene with dimming increment
« Reply #2 on: September 12, 2017, 03:13:31 pm »
Hi, thanks for the fast response!

I have no idea what I am doing, are totally illiterate when it comes to LUA.

What I have tried to do is to puzzle something together. All I really want is to be able to create 2 scenes (manually triggered):

1. To increase the dimming of a bulb, from current level incrementally by +10% (of course, each time the scene is run)
2. To decrease the dimming of a bulb, from current level incrementally by -10% (of course, each time the scene is run)

Now I am hoping for forum support and more skilled people to help me out :-)

Offline Matsohl

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +4/-1
Re: Scene with dimming increment
« Reply #3 on: September 12, 2017, 03:35:38 pm »
Code: [Select]
local lightLevel =tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device))
Try This  :)

What are you using "device" and dID for, perhaps you'r mixing them up? As they are stated the device seems to be a global variable and the dID is local. Which one is the light and the switch?

This could work

Code: [Select]
-- I have not tested it but it seems ok. When switchOnoff is on the light goes down with 10% each time the scene runs
-- When switchOnoff is off the light goes up with 10% each time the scene runs
-- The code could be simplified with else statements but are stated as is for understanding

local dID = 186 -- The Bulb Device
local dID2 = 123 -- The Switch

local switchOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", dID2)
local lightLevel =tonumber((luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", dID)))

if (switchOnOff == "1") and (lightlevel > 0) then
              lightLevel =lightLevel-10
              luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = lightLevel}, dID)                 
end           
if (switchOnOff == "0") and (lightlevel < 100) then
              lightLevel =lightLevel+10
              luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = lightLevel}, dID)                 
end   
« Last Edit: September 12, 2017, 04:38:01 pm by Matsohl »

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Re: Scene with dimming increment
« Reply #4 on: September 12, 2017, 03:44:34 pm »
Hi!

Dont quite understand, in the code you posted, where do I put the deviceID and where do I put the increment +10% and -10%?

I want scene 1 to increase the current dim level by 10% and decrease by 10% respectively in scene 2.

:-)

Offline Matsohl

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +4/-1
Re: Scene with dimming increment
« Reply #5 on: September 12, 2017, 04:08:39 pm »
I changed my post above :)

Online jswim788

  • Sr. Member
  • ****
  • Posts: 476
  • Karma: +22/-2
Re: Scene with dimming increment
« Reply #6 on: September 12, 2017, 04:19:54 pm »
This code has an error:

  local lightLevel =tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device))

The problem is that luup.variable_get has 2 return values, the value and the time stamp of when the variable changed.  See http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get.  to_number() takes the input and the base.  So the result of the above code is almost certainly not what you want.  But if you add a second set of parentheses you'll get the single return value and tonumber() will give what you want:
Code: [Select]
local lightLevel =tonumber((luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device)))

Offline Matsohl

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +4/-1
Re: Scene with dimming increment
« Reply #7 on: September 12, 2017, 04:27:31 pm »
You are right  :) Thanks, modified above
« Last Edit: September 12, 2017, 04:33:37 pm by Matsohl »

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Re: Scene with dimming increment
« Reply #8 on: September 12, 2017, 05:59:54 pm »
Yet again I am stunned by this amazing forum :-)

Re-thought: What if I create two separate and manually triggered scene, one for a) ->increasing by 10 %  and b) ->decreasing by 10%. Forget also the part where current setting is >0 and <100.

So I end up with One scene for increasing 10% from current level, and One scene for decreasing 10% from current level. In other words, all I would have to do to increase the light dimming level, would be to run the scene 1 time (as an example)

In other words, if we exclude the Switch, what should the code look like?

BONUS question: if I would like to include more than one dimmer/bulb, what would that look like?

Thanks A LOT!!!! ;D


Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9563
  • Karma: +728/-135
    • RTS Services Plugins
Re: Scene with dimming increment
« Reply #9 on: September 12, 2017, 07:43:22 pm »
Have you tried to do this without LUA code at all.

Dimmers have actions:
    SetStepDelta       with an arg of Some Number like 10 for 10%
    StepUp
    StepDown

These are available via the advanced tab in the Scene Editor or PLEG actions.


Offline zedrally

  • Hero Member
  • *****
  • Posts: 768
  • Karma: +1/-0
  • Black Cat Controllers & Control Systems
    • Black Cat Control Systems
Re: Scene with dimming increment
« Reply #10 on: September 12, 2017, 11:18:44 pm »
Have you tried to do this without LUA code at all.

Dimmers have actions:
    SetStepDelta       with an arg of Some Number like 10 for 10%
    StepUp
    StepDown

These are available via the advanced tab in the Scene Editor or PLEG actions.


Can you elaborate on this, I'm curious as to how to change the Ramp Rate if there is no Device Parameter available. Ideally, I would like to be able to set a device to "ramp up" from 0% to x% over n period of time (secs) and also the opposite "Ramp Down". Is this possible?
Living in the Land of Oz, give me a vegemite sandwich. Home Seer, Vera Lite & Edge, Popp, Black Cat Smart Hub & Vera G, Black Cat Lite 1 & 2's a Black Cat Dimmer or 2, then some  Black Cat Cat's Eye PIR's & Door-Window Sensors plus a Z-UNO or 2.

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Re: Scene with dimming increment
« Reply #11 on: September 13, 2017, 05:47:01 am »
Hi Mr Schaefer!

I thought your idea was brilliant!

I created a manually triggered scene, selected the dimmer, changed under Advanced Tab as per your instructions.
However, I cant get it to work.

Please see attached pictures.

Would be WONDERFUL if you could identify what I have done wrong.

Best regards,
P-a

Offline Priest

  • Full Member
  • ***
  • Posts: 118
  • Karma: +5/-0
Re: Scene with dimming increment
« Reply #12 on: September 13, 2017, 07:46:27 am »
I have a dimmer module in my living room for a standing lamp. The LED bulb in it actually responds well all the way down to 2% and is dimmer than a night-light at that level, unlike a lot of other LED bulbs out there that seem to put out a lot of light at 1%. With that in mind.... here are the codes I have in two different scenes, one for dim up and one for dim down.  I step it where it goes first to 5% which is a usable light level to walk around the living room in really dim light. then it jumps bigger from there.  the same thing on the dim down. I did it the way I did so that if someone had manually adjusted the dimmer through the app to some odd number it would still hit the 5% and not drop to something unusable.  I also had a problem where things would get tweaky when Vera tried to subtract 20 from 15 and she would hang up for about 5 minutes....doing it this way avoided that.  I never have had a problem with addition going over 100% but i did with subtraction going below 0.

Just replace the dID number with your device ID and this thing will work. If you need to change the values for things just make sure that the integrity of the if and elseif statements still works.....you dont want any weird overlap.

Step Up Code
Code: [Select]
local dID =23
local level = tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",dID),10)
if level < 5 then level = 5
elseif level >= 80 then level =100
else level = level + 20 end
     local args = {}
     args["newLoadlevelTarget"] = level
     luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", args, dID)



Step Down Code
Code: [Select]
local dID =23
local level = tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",dID),10)
if level >= 25 then level = level -20
elseif level <= 5 then level = 0
else level = 5 end
     local args = {}
     args["newLoadlevelTarget"] = level
     luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", args, dID)

Offline P-a

  • Sr. Newbie
  • *
  • Posts: 37
  • Karma: +2/-0
Re: Scene with dimming increment
« Reply #13 on: September 13, 2017, 01:21:59 pm »
Mr Priest,

You MADE my DAY, massive amount of credit heading your way ;-)

I LOVE this forum, have a wonderful evening.

Best regards,
P-a

Offline Priest

  • Full Member
  • ***
  • Posts: 118
  • Karma: +5/-0
Re: Scene with dimming increment
« Reply #14 on: September 13, 2017, 01:52:38 pm »
Mr Priest,

You MADE my DAY, massive amount of credit heading your way ;-)

I LOVE this forum, have a wonderful evening.

Best regards,
P-a

Not a problem.  I'm still kind of a code idiot, but I have figured out how to grab bits and pieces from other peoples code they have posted and slap them together till they work.  I'd never try to write you a piece of code from scratch to solve a problem since I'd get it wrong for a while I'm sure....but if I have the code you want already tested and in my system I have no problem sharing.

I have a document with all sorts of bits of code in it I've used or copied from elsewhere.  If I know for certain it works the text is green, if it is copied from the forum for reference but I've not tested it myself it is yellow, if it is a work in progress or broken it is in red.