Simple LMS LUA volume dimmer

Moderator: leecollings

Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

G3rard wrote:Very nice this topic :mrgreen:
Going to use this for my Sonos volume so that I can also control the volume via Siri.

I found that it is possible to update the dimmer switch in Domoticz without triggering the script (to avoid the volume loop) with

Code: Select all

commandArray['UpdateDevice'] = '409|2|60'
where 409 is the idx, 2 is status on (with showing the % in stead of On) and 60 is the %. The % value of the dimmer is updated, but the only thing is that the slider isn't.

Will post my script here once it is finished.
Unfortunately can't get this working with UpdateDevice and nValue = 2. For some reason, if I use 2 as nValue the volume dimmer get's always a "Group Off" status and turns off. It does not execute the "off"-action and it does not execute the Lua script so in that sense it is ok. nValue = 1 works pretty much as you describe above. It does not update the slider but also does not trigger the Lua. I just don't understand why I was not able to make it (nValue = 0 or 1) work with my earlier experiments with another script. It only skipped the on/off actions, but still triggered the Lua for device change event. Well, maybe the conditions in Lua were not properly set...

But does it work for you with nVakue = 2? Do you have 100 step dimmer?
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Haha. Don't know how whether too wait for you to debug hh. Lazy I know lol ;)
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Why oh why! LOL If I run the script above I'm getting a Nil value from svalues Lounge Touch V.. hmmm
pi@raspberrypi:~$ lua /home/pi/domoticz/scripts/lua/script_time_sqtouch_volume.lua
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 881 100 796 100 85 47276 5048 --:--:-- --:--:-- --:--:-- 49750
lua: .../domoticz/scripts/lua/script_time_sqtouch_volume.lua:14: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
.../domoticz/scripts/lua/script_time_sqtouch_volume.lua:14: in main chunk
[C]: ?
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Appears the dimmer issue sending an On Command via Siri and the Home App sliders (HomeKit) has been fixed by The Ramon so I might be able to get away with one of the previous scripts using the 100 dimmer... I think that was the only reason I stopped using it and tried to use the "Level", just gotta look back and remember which scripts it was that worked!

Tested and all works other than when you turn the player on and the dimmer (Lounge Touch V) displays on then it sets the player to 0% Once you move slide or say siri set radio to 50% then it will and it works. Not sure if this is a limitation of the 100 dimmer or not.. Oh and its 1% out lol. (0-99 - maybe a +1 can be added as the offset) This is the old script:-

Code: Select all

commandArray = {}

    json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl \'http://192.168.1.26:9000/jsonrpc.js\' --data-binary \'{"id":1,"method":"slim.request","params":["00:04:20:28:54:32",["status","-","1",""]]}\''))   
    
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    volume = deviceinfo.result["mixer volume"]
   powerstatus = deviceinfo.result.power
   
   switchVolume = otherdevices_svalues['Bedroom Radio Volume']
   if tonumber(volume) ~= tonumber(switchVolume) and tonumber(powerstatus) == 1 then
      commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=272&switchcmd=Set%20Level&level='..volume
      commandArray['Bedroom Radio Volume'] = 'On'
   end   

   if tonumber(powerstatus) == 0 then
       commandArray['Bedroom Radio Volume'] = 'Off'
   end
   
return commandArray
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

simon_rb wrote:Why oh why! LOL If I run the script above I'm getting a Nil value from svalues Lounge Touch V.. hmmm
pi@raspberrypi:~$ lua /home/pi/domoticz/scripts/lua/script_time_sqtouch_volume.lua
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 881 100 796 100 85 47276 5048 --:--:-- --:--:-- --:--:-- 49750
lua: .../domoticz/scripts/lua/script_time_sqtouch_volume.lua:14: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
.../domoticz/scripts/lua/script_time_sqtouch_volume.lua:14: in main chunk
[C]: ?
Hmm, this doesn't make sense to me. Maybe happens when you run it form command line (might be different user than running Domoticz, not able to access the otherdevices_svalues table) only? I always let Domoticz run the scripts to be sure if they work or not. In any case, the dimmer switch, once you use it once at least, should always have some sValue.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

simon_rb wrote:Appears the dimmer issue sending an On Command via Siri and the Home App sliders (HomeKit) has been fixed by The Ramon so I might be able to get away with one of the previous scripts using the 100 dimmer... I think that was the only reason I stopped using it and tried to use the "Level", just gotta look back and remember which scripts it was that worked!

Tested and all works other than when you turn the player on and the dimmer (Lounge Touch V) displays on then it sets the player to 0% Once you move slide or say siri set radio to 50% then it will and it works. Not sure if this is a limitation of the 100 dimmer or not.. Oh and its 1% out lol. (0-99 - maybe a +1 can be added as the offset) This is the old script:-

Code: Select all

commandArray = {}

    json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl \'http://192.168.1.26:9000/jsonrpc.js\' --data-binary \'{"id":1,"method":"slim.request","params":["00:04:20:28:54:32",["status","-","1",""]]}\''))   
    
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    volume = deviceinfo.result["mixer volume"]
   powerstatus = deviceinfo.result.power
   
   switchVolume = otherdevices_svalues['Bedroom Radio Volume']
   if tonumber(volume) ~= tonumber(switchVolume) and tonumber(powerstatus) == 1 then
      commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=272&switchcmd=Set%20Level&level='..volume
      commandArray['Bedroom Radio Volume'] = 'On'
   end   

   if tonumber(powerstatus) == 0 then
       commandArray['Bedroom Radio Volume'] = 'Off'
   end
   
return commandArray
You just need to ignore completely when sValue is 0, i.e. let it do something only "if tonumber(otherdevices_svalues['Lounge Touch V']) > 0" :)
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

I ran it from command to see whats happening, the script didn't do anything within Domoticz.. Confusing! LOL Hows your script coming along?

Also, When playing about I think it is the device script that would end up making the players go to 0 when they are turned on!
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Simple LMS LUA volume dimmer

Post by G3rard »

Nautilus wrote: Unfortunately can't get this working with UpdateDevice and nValue = 2. For some reason, if I use 2 as nValue the volume dimmer get's always a "Group Off" status and turns off. It does not execute the "off"-action and it does not execute the Lua script so in that sense it is ok. nValue = 1 works pretty much as you describe above. It does not update the slider but also does not trigger the Lua. I just don't understand why I was not able to make it (nValue = 0 or 1) work with my earlier experiments with another script. It only skipped the on/off actions, but still triggered the Lua for device change event. Well, maybe the conditions in Lua were not properly set...

But does it work for you with nVakue = 2? Do you have 100 step dimmer?
I use a 100 step dimmer. For that switch 0 represents Off, 1 is On, 2 is %, 3 is Group Off and 4 is Group On (didn't try it any further).
dimmer switch.jpg
dimmer switch.jpg (15.75 KiB) Viewed 3411 times
Not using Domoticz anymore
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

I am SOOOOO confused now... Don't know where I am or what I am doing now. This is the first LUA stuff I've done and I'm trying to reverse engineer all the scripts and I am getting more confused with sValues nValues, etc.. Didn't think it was going to get this involved. I am completely lost..
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

simon_rb wrote:I ran it from command to see whats happening, the script didn't do anything within Domoticz.. Confusing! LOL Hows your script coming along?
I changed mine to 100 step dimmer and have it working now both ways. The scripts I use for this are basically 1) Device:

Code: Select all

if devicechanged['Terassin äänenvoimakkuus'] ~= 'Off' and devicechanged['Terassin äänenvoimakkuus'] ~= nil then
    if tonumber(otherdevices_svalues['Terassin äänenvoimakkuus']) > 0 then
        volume = otherdevices_svalues['Terassin äänenvoimakkuus']
        commandArray['Musiikki terassi']='Set Volume '..volume
    end
end

if (devicechanged['Musiikki terassi'] == 'Off' or devicechanged['Musiikki terassi'] == 'Stopped' or devicechanged['Musiikki terassi'] == 'Disconnected') then
    if otherdevices['Terassin äänenvoimakkuus'] ~= 'Off' then
        commandArray[#commandArray+1]={['Terassin äänenvoimakkuus'] = 'Off'}
    end
end
and 2) Time:

Code: Select all

if uservariables['SBTerassiStatus'] == 'On' then
    json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl \'http://url/jsonrpc.js\' --data-binary \'{"id":1,"method":"slim.request","params":["mac",["status","-","1",""]]}\''))  
        
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
    
    powerstatus = deviceinfo.result.power
    playervolume = deviceinfo.result["mixer volume"] + 1
    switchvolume = otherdevices_svalues['Terassin äänenvoimakkuus']
    difference = math.abs(switchvolume - playervolume)
       
    if difference > 1 and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://192.168.11.92:30/json.htm?type=command&param=switchlight&idx=314&switchcmd=Set%20Level&level='..playervolume..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Terassin äänenvoimakkuus'] == 'Off' then
        os.execute('curl -s "http://192.168.11.92:30/json.htm?type=command&param=switchlight&idx=314&switchcmd=Set%20Level&level='..playervolume..'" &')
    end
    
end
Player on/off is still via the On/Off action of the volume switch.
Also, When playing about I think it is the device script that would end up making the players go to 0 when they are turned on!
That's correct. As explained in few previous posts (;)), the Off command basically equal sValue = 0 and the On does not change it to anything else. So if the volume dimmer just turn on, sValue is always 0 and if you let that to be sent to the player it is muted. Therefore you need to ignore sValue = 0 completely. The uservariable method I mentioned about will allow you to send the same sValue as it was before last "Off" but it should not be necessary to use this as the player should be on the same volume as it previously was...

edit: if there are some confusing bits there, it is probably because I had already scripts for "Musiikki terassi" ( = the player) and I just added the parts I needed for "Terassin äänenvoimakkuus" ( = the volume dimmer). But I copied here just the relevant parts for this purpose.
Last edited by Nautilus on Tuesday 08 November 2016 23:50, edited 1 time in total.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Just understood what you mean. Been try to reverse engineer the last device and time scripts to work without the volume calculations, think I have the user variables sorted lol Although it doesn't match up yet (set volume to 70 and player goes to 28 lol. Think its still doing the maths somewhere)
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Trying to copy your scripts over and understand them but I don't know what Terassin äänenvoimakkuus (Lounge Touch V) & Musiikki terassi (Lounge Touch)

Is that right?
Last edited by simon_rb on Tuesday 08 November 2016 23:55, edited 1 time in total.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

G3rard wrote:
I use a 100 step dimmer. For that switch 0 represents Off, 1 is On, 2 is %, 3 is Group Off and 4 is Group On (didn't try it any further).

dimmer switch.jpg
Ok, I guess it would explain it as I had a bit different type of dimmer. Anyhow, I'm quite happy with the way the current script operates including updating the slider. Admittedly it "resends" the volume once to player (could prevent it with a variable but no harm in it as it is the same volume as already on the player) :D
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

simon_rb wrote:Trying to copy your scripts over and understand them but I don't know what Terassin äänenvoimakkuus & Musiikki terassi are in relation to Lounge Touch V..which one is your virtual dimmer switch?
The "edit" part from my previous post:

If there are some confusing bits there, it is probably because I had already scripts for "Musiikki terassi" ( = the player) and I just added the parts I needed for "Terassin äänenvoimakkuus" ( = the volume dimmer). But I copied here just the relevant parts for this purpose.
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Simple LMS LUA volume dimmer

Post by G3rard »

My volume slider is now working with Siri :mrgreen:
Not using Domoticz anymore
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

G3rard wrote:My volume slider is now working with Siri :mrgreen:
Happy Days!!!!!!! :D
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Simple LMS LUA volume dimmer

Post by Nautilus »

simon_rb wrote:
G3rard wrote:My volume slider is now working with Siri :mrgreen:
Happy Days!!!!!!! :D
Yes, can also confirm it working via Siri, just seems that Siri loses one percentage point on the way. If I tell her to set it to 20%, the actual dimmer goes to 19% (and also the player volume of course). But this I can live with :)
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Nautilus wrote:Yes, can also confirm it working via Siri, just seems that Siri loses one percentage point on the way. If I tell her to set it to 20%, the actual dimmer goes to 19% (and also the player volume of course). But this I can live with :)
Show off!! :lol:
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Simple LMS LUA volume dimmer

Post by G3rard »

Nautilus wrote:
Yes, can also confirm it working via Siri, just seems that Siri loses one percentage point on the way. If I tell her to set it to 20%, the actual dimmer goes to 19% (and also the player volume of course). But this I can live with :)
Same here for 20 which sets volume to 19.
And when I ask for 15 it sends a message for 16, which actually sets the volume to 15 :?

Code: Select all

MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":409,"level":16,"switchcmd":"Set Level"}
But hey it's working very nice :D
Not using Domoticz anymore
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb »

Working working working

THANK YOU!

Same issue here with the 1% out with Siri, and the slider within the native home app will always jump back down 1% when you release. I have noticed a couple of times when I have turn my 'Lounge Touch V' it has gone to 99% That could be a problem. I noticed you haven't used the uservariables in your scripts. Have you noticed this behaviour?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest