[Solved] Increese Philips Hue light level by 10% by LUUP code

Hi.
I have bought myself a wallmote with 4 buttons and right now I can control two Philips Hue bulbs. One button to turn the first bulb on, second button to turn it off etc. etc.

Instead I want to be able to due one of the following steps.

  1. When I click on a button, if the controlled bulb is off then turn it on, if its on, turn if off. Then I am able to control 4 lights instead of two.

or

  1. When I click one button the light will turn on at 10% if its off or increase the light by 10%.

I know that the wallmote have swipe, but it is only working with association groups and that does not work with Philips Hue, so the only way this can be achieved is by LUUP, I think.

Can anyone tell be how I can control a Philips Hue bulb by LUUP or have an example of something that does something simular.

Thanks in advance.

Best regards,
Jens

Here is on/off code I have running a hue light on a minimote.

local device=36 --replace
local switchOnOff = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, device)

if (switchOnOff == “1”) then
– Switch is on
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “0”}, device)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, device)

end 

this was a ramp up/down light I had set up for my daughter. We replaced that lamp so this is a copy of the code I had. It was ui5 so it may need some tweak for ui7.

It is set up so that if on it turns off and if off it turns on at 10% and slowly brightens to full. It should give you the code base to make what you want. You won’t need the while loop with the delay that is used in the ramp. You would take the lightLevel variable and add 10 for the SetTarget command.

local device=40
local delay=10
local mindim=0
local maxdim=100
local counter=0
local counter_max=11

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
– Switch is on
while (lightLevel >=0) do
luup.call_delay( luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevel}, device) ,delay)
lightLevel =lightLevel-1
end

  else 
 lightLevel=10

        while   (lightLevel <100)  do
             luup.call_delay( luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = lightLevel}, device)     ,delay)                      
             lightLevel =lightLevel+1
        end


     
end

ReplyQuoteNotify

Hi kigmatzomat.

Thanks for you help and inspiration.

The first script worked like a charm, the second one have a bug in it that took me a while to figure out :slight_smile: (at least in the current UI7)

Let me explain.
You are getting the current light level in this line
local lightLevel =luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, device)
Now you lightLevel var contain a string with the value of, lets say “10”
Later on you are making a comparison like this while (lightLevel <100) do
This will not work because you are comparing a string with a number.
The reason it works anyway is because you have lightLevel =lightLevel+1 and that is a number (1 the first time)

The way to fix that is to convert it to a number like this

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

And now for my contribution. This script will increase the light level to 20%,40%,60%,80%, 100% and then turn the light off for each time you run the scene.

If the light level is 10% it will increase it to 20%, if its 21% it will increase it to 40% etc.

It works in conjunction with the Wallmote

local device=15 --to be replaced by the id of your bulb

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

if (lightLevel<20) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “20”}, device)
elseif ((lightLevel>=20) and (lightLevel<40)) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “40”}, device)
elseif ((lightLevel>=40) and (lightLevel<60)) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “60”}, device)
elseif ((lightLevel>=60) and (lightLevel<80)) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “80”}, device)
elseif ((lightLevel>=80) and (lightLevel<100)) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, device)
elseif (lightLevel==100) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “0”}, device)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)
end

There might be a better way to do it, but it works :slight_smile:

Best regards,
Jens

Good catch on the error. Not sure if it got saved wrong or if ui5 had autoconversions.

You only need 3 conditions. For <20 jump to 20, everything up to 100 add 10, at 100 set zero.

I think this is the code to do the string/number/string conversions.


local switchOnOff = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, device)
local lightLevel =luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, device)
lightLevel = tonumber(lightLevel)
lightLevelplus = tostring ( tonumber(lightLevel)+10)

If (lightLevel<20 or switchOnOff ==“0” ) then
call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “1”}, device)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “20”}, device)
ElseIf lightLevel<100 then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevelplus}, device)
Else
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “0”}, device)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)
end

Thanks kigmatzomat.

As I wrote, it might not be the best way to do it but it works :slight_smile:

At least I used if, elseif elseif, end
instead of the first version I made where it was if end, if end, if end :slight_smile:

Best regards,
Jens