Page 4 of 6

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 0:18
by simon_rb
Spot on mate. If I slide it then it crashes. If I just click to the next level then the volume will change but there is a 15-20 sec delay... No as smooth as the original way before we used the JSON.. Not even sure the delay would help! Back to square one with it! :( Any ideas? Cheers :D

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 7:54
by Nautilus
I have a couple of dummy dimmers (well, they are based on a hw profile and thus work a bit differently as I described earlier) and I have scripts that react on their status (on/off) and sValue changes. It does not involve any json reading (though I have plenty of other scripts that do, and they take a bit longer to run sometimes) but work very smoothly, no matter if you click it or slide it. So maybe the sValue would be the way to go after all, at least once the switch is already on (you could totally ignore sValue = 0 and in case it is, then read the level with json). Even with reading the level with json I don't think it should take quite that long, it could be there are some issues with the 100 step dummy dimmer implementation as there's the sValue offset etc. Just speculating though...:)

And maybe the best option would be to test the other type of dummy dimmer (for which the examples in the first posts of this thread refers to). To do this you need to go to the device tab and add manual device (left top). For hardware select the dummy hardware, for switch type select dimmer and for subtype select e.g. LightwaveRF (rest can be default). Then you should have a dimmer that has sValue steps from 0 to 31 and which does not change the sValue to 0 when turned off/on. So you can keep setting the volume with this script:

Code: Select all

commandArray = {}
if devicechanged['Lounge Touch V'] ~= 'Off' then
     volume = otherdevices_svalues['Lounge Touch V']
     calcvolume = (volume*100) / 31
     commandArray['Lounge Touch']='Set Volume '..calcvolume
end
return commandArray
As suggested by the thread starter, to control the player on/off status you can put to on action of the Lounge Touch V something like this:
"http://192.168.1.10:9000/status.html?p0 ... 2.168.1.10" (change the first address to LMS ip and port and second to player ip). For off action change power_on to power_off. Then the device script can just control the volume.

For time script you should use something like (rounding = math.floor not tested):

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:23:88:36",["status","-","1",""]]}\''))   
    
   raw = file:read('*all')
   file:close()   
   deviceinfo = json:decode(raw)
   
   volume = deviceinfo.result["mixer volume"]
   powerstatus = deviceinfo.result.power
   
   switchVolume = otherdevices_svalues['Lounge Touch V']
   calcVolume = math.floor((switchVolume*100 / 31)+0.5)
   if tonumber(volume) ~= tonumber(calcVolume) and tonumber(powerstatus) == 1 then
      commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=265&switchcmd=Set%20Level&level='..volume
   end
   
   if tonumber(powerstatus) == 1 and otherdevices['Lounge Touch V'] == 'Off' then
            commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=265&switchcmd=Set%20Level&level='..volume
    end

   if tonumber(powerstatus) == 0 and otherdevices['Lounge Touch V'] ~= 'Off' then
       commandArray['Lounge Touch V'] = 'Off'
   end
   
return commandArray

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 10:56
by simon_rb
I'll try them and see what happens. I had the power on setup already. I'll let you.. :D

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 14:19
by Nautilus
simon_rb wrote:I'll try them and see what happens. I had the power on setup already. I'll let you.. :D
I have one player for outdoor speakers where perhaps this kind of volume switch would be nice as well. So I went ahead and tested and there's some things I did not consider. But this seems to work for setting the volume. You need to create one user variable as well, type integer to store the volume level before "Off" as for some reason this new dummy dimmer seems to set it to 0 when Off. So when turning it back to On, the value will be taken from the uservariable.

Code: Select all

if devicechanged['Lounge Touch V'] ~= 'Off' and devicechanged['Lounge Touch V'] ~= nil then
    if tonumber(otherdevices_svalues['Lounge Touch V']) > 0 then
        volume = otherdevices_svalues['Lounge Touch V']
        calcvolume = math.floor((volume*100/32)+0.5)
        print('Setting Lounge Touch volume to '..calcvolume)
        commandArray['Lounge Touch']='Set Volume '..calcvolume
        commandArray['Variable:LoungeTouchVolume'] = otherdevices_svalues['Lounge Touch V']
    else
        volume = tonumber(uservariables['LoungeTouchVolume'])
        calcvolume = math.floor((volume*100/32)+0.5)
        commandArray['Lounge Touch']='Set Volume '..calcvolume
    end
end
With the above in mind, I guess it is pretty much the same if you use dimmer with 100 steps or not (note that even with this model, the original script was dividing with 31 which results to wrong value, it needs to be devided with 32). Only thing is that if the "Offset" becomes a problem...

Also note that this cannot be used to mute the player as the switch turns always off when sValue goes to 0 and thus turn the player off.

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 14:51
by Nautilus
The timescript which works for me (does not result to 1:1 volume because of 32 steps available only, the first check of uservariable is the same as checking if the player is "On", I turn the volume switch of elsewhere):

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)
       
    volume = deviceinfo.result["mixer volume"]
    powerstatus = deviceinfo.result.power
       
    switchVolume = otherdevices_svalues['Lounge Touch V']
    print(switchVolume)
    calcVolume = math.floor((volume/100*31)+0.5)
    print(calcVolume)
       
    if tonumber(volume) ~= tonumber(calcVolume) and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://url/json.htm?type=command&param=switchlight&idx=IDX&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Lounge Touch V'] == 'Off' then
        os.execute('curl -s "url/json.htm?type=command&param=switchlight&idx=IDX&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 0 and otherdevices['Lounge Touch V'] ~= 'Off' then
        commandArray['Lounge Touch V'] = 'Off'
    end
end

return commandArray

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 15:48
by simon_rb
Cheers dude! Should I use my exiting 100 step dimmer or create a new one with 32 steps?

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 16:25
by simon_rb
I've added a new 32 step dimmer.. Got the device script to work but not the Time script.. What uservariable should I use? Should it be Lounge Touch V or Lounge Touch for me? I haven't set a user variable so should I just remove it? Here is mine..

The volume changes beautifully from the new 32 step Lounge Touch V switch (Renamed and will delete the original one Lounge Touch V). However if I turn off the dimmer the player doesn't turn off. It doesn't turn on either (basically doesn't reflect the player power status or have control of it) Good news is the On doesn't set the player to 0% :) Just tried setting the volume via Siri via eDomoticz/Homebrige and even though it says "On" the volume changes just fine! Super, well done mate thats that bit fixed, can throw away the JSON Level stuff haha. Just need to get the switch to turn on and off and to have it grab the volume from the LMS player :D

Code: Select all

if uservariables['Lounge Touch'] == 'On' then
    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:23:88:36",["status","-","1",""]]}\''))  
        
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
       
    volume = deviceinfo.result["mixer volume"]
    powerstatus = deviceinfo.result.power
       
    switchVolume = otherdevices_svalues['Lounge Touch V']
    print(switchVolume)
    calcVolume = math.floor((volume/100*31)+0.5)
    print(calcVolume)
       
    if tonumber(volume) ~= tonumber(calcVolume) and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Lounge Touch V'] == 'Off' then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 0 and otherdevices['Lounge Touch V'] ~= 'Off' then
        commandArray['Lounge Touch V'] = 'Off'
    end
end

return commandArray

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 18:48
by simon_rb
I amended the script below to get it running. However every minute it drops the volume by 4-5% by itself and eventually turns off lol!

The Lounge Touch V switch will turn on and off to reflect the players power status however turning off the Lounge Touch V switch will just turn off the switch and the player remains on, then a minute later the Lounge Touch V turns back on lol. I have the turn on action to turn the player on, do I need to set the turn off action to turn the player off. Last time the scripts turned off the player when the Lounge Touch V was turned off.

Cheers

PS. We are SSOOOOOOOO close :D

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:23:88:36",["status","-","1",""]]}\''))  
        
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
       
    volume = deviceinfo.result["mixer volume"]
    powerstatus = deviceinfo.result.power
       
    switchVolume = otherdevices_svalues['Lounge Touch V']
    print(switchVolume)
    calcVolume = math.floor((volume/100*31)+0.5)
    print(calcVolume)
       
    if tonumber(volume) ~= tonumber(calcVolume) and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Lounge Touch V'] == 'Off' then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..calcVolume..'" &')
    end

    if tonumber(powerstatus) == 0 and otherdevices['Lounge Touch V'] ~= 'Off' then
        commandArray['Lounge Touch V'] = 'Off'
    end


return commandArray

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 22:53
by Nautilus
simon_rb wrote:The Lounge Touch V switch will turn on and off to reflect the players power status however turning off the Lounge Touch V switch will just turn off the switch and the player remains on, then a minute later the Lounge Touch V turns back on lol. I have the turn on action to turn the player on, do I need to set the turn off action to turn the player off. Last time the scripts turned off the player when the Lounge Touch V was turned off.
Yes, as mentioned a few posts earlier :)
As suggested by the thread starter, to control the player on/off status you can put to on action of the Lounge Touch V something like this:
"http://192.168.1.10:9000/status.html?p0 ... 2.168.1.10" (change the first address to LMS ip and port and second to player ip). For off action change power_on to power_off. Then the device script can just control the volume.
(you need to start really paying attention to what there is defined in the script, especially if you see something you are not expecting. Only this way you can learn and amend stuff to work to your liking ;))

Also, you naturally cannot use any uservariable which you haven't defined. For the time script it is not needed necessarily but for the device script, if you want that turning the volume switch "On" (without touching the slider) will send a valid volume command, you need it. Though I guess you do not need it to send it as it should always be on the same volume as when turned off so plain "On" doesn't necessarily have to do anything. So only this part is really needed:

Code: Select all

if devicechanged['Lounge Touch V'] ~= 'Off' and devicechanged['Lounge Touch V'] ~= nil and tonumber(otherdevices_svalues['Lounge Touch V']) > 0 then
    volume = otherdevices_svalues['Lounge Touch V']
    calcvolume = math.floor((volume*100/32)+0.5)
    print('Setting Lounge Touch volume to '..calcvolume)
    commandArray['Lounge Touch']='Set Volume '..calcvolume
end
The biggest issue I guess it the volume changing always a few percent and going eventually to zero (=Off)? Couple of options come to mind. Either using the 100 step dimmer and getting it into 1:1 sync (player volume = switch sValue), setting the condition so that it updates the volume on the switch only if it differs more than let's say 2 sValue steps (volume control is available in even bigger steps only) or adding some condition (with help of a uservariable maybe) to prevent the device script from sending the volume to player when volume switch change is coming from the time script (this would avoid the loop: switch updates player volume -> player volume updates switch sValue -> switch updates player volume).

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 23:07
by Nautilus
Hey, I got it working at least based on quick test by using

Code: Select all

calcVolume = math.floor((volume/100*32)+0.5)
In the time script and

Code: Select all

calcvolume = math.floor((volume*100/31)+0.5)
on the device script. Now it send always the same volume to player although sValue changes by +/- 1. Need to think something more robust just in case though...

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 23:19
by simon_rb
My apologise for the miss understanding with the On and Off, I didn't add the Off as wasn't sure it would create some sort of loop. So the scripts no longer turn off Lounge Touch when Lounge Touch V is off - understood. I have the On already so I can copy that and change it to off. The user variable is understood. Could have it if Lounge Touch V is anything other than Off run the script.
Hey, I got it working at least based on quick test by using
CODE: SELECT ALL
calcVolume = math.floor((volume/100*32)+0.5)
In the time script and
CODE: SELECT ALL
calcvolume = math.floor((volume*100/31)+0.5)
on the device script. Now it send always the same volume to player although sValue changes by +/- 1. Need to think something more robust just in case though...
You have just changed the 32 & 31s around? Unfortunately it doesn't work my end.. its still going down lol

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 23:33
by Nautilus
simon_rb wrote:My apologise for the miss understanding with the On and Off, I didn't add the Off as wasn't sure it would create some sort of loop. So the scripts no longer turn off Lounge Touch when Lounge Touch V is off - understood. I have the On already so I can copy that and change it to off. The user variable is understood. Could have it if Lounge Touch V is anything other than Off run the script.
Hey, I got it working at least based on quick test by using
CODE: SELECT ALL
calcVolume = math.floor((volume/100*32)+0.5)
In the time script and
CODE: SELECT ALL
calcvolume = math.floor((volume*100/31)+0.5)
on the device script. Now it send always the same volume to player although sValue changes by +/- 1. Need to think something more robust just in case though...
You have just changed the 32 & 31s around? Unfortunately it doesn't work my end.. its still going down lol
Yes, noticed it only worked with big enough levels. With small sValue levels it started going down. Tried it the other way around and this seems to work with both big and small levels :D

Code: Select all

    
    playervolume = deviceinfo.result["mixer volume"]
    playercalc = math.floor((playervolume/100*31)+0.5)

    switchvolume = otherdevices_svalues['Terassin äänenvoimakkuus']
    switchcalc = math.floor((switchvolume*100/31)+0.5)
    
    difference = math.abs(switchcalc - playervolume)
       
    if difference > 2 and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://url/json.htm?type=command&param=switchlight&idx=314&switchcmd=Set%20Level&level='..playercalc..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Terassin äänenvoimakkuus'] == 'Off' then
        os.execute('curl -s "http://url/json.htm?type=command&param=switchlight&idx=314&switchcmd=Set%20Level&level='..playercalc..'" &')
    end

    if tonumber(powerstatus) == 0 and otherdevices['Terassin äänenvoimakkuus'] ~= 'Off' then
        commandArray['Terassin äänenvoimakkuus'] = 'Off'
    end
It almost looks as if this could be made into if tonumber(switchcalc) ~= tonumber(playervolume) but maybe that is pushing it, min volume change of 3 is ok as each sValue change represents about three in volume change with this 32 step dimmer...:)

Re: Simple LMS LUA volume dimmer

Posted: Monday 07 November 2016 23:37
by simon_rb
Excellent - I shall give it ago.. It looks like you have changed quite a lot! lol Terassin äänenvoimakkuus = Lounge Touch V, that right?

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 0:08
by G3rard
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.

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 0:24
by simon_rb
Off to bed as had 3 hours sleep and up early tomorrow, will try again then. I currently have this script running but its not updating my Lounge Touch V at all even though I have the player set to 80 and Lounge Touch V to 21.

Can't quite see what's wrong lol

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:23:88:36",["status","-","1",""]]}\''))  
        
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
       
    
    playervolume = deviceinfo.result["mixer volume"]
    playercalc = math.floor((playervolume/100*31)+0.5)

    switchvolume = otherdevices_svalues['Lounge Touch V']
    switchcalc = math.floor((switchvolume*100/31)+0.5)
    
    difference = math.abs(switchcalc - playervolume)
       
    if difference > 2 and tonumber(powerstatus) == 1 then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..playercalc..'" &')
    end

    if tonumber(powerstatus) == 1 and otherdevices['Lounge Touch V'] == 'Off' then
        os.execute('curl -s "http://192.168.1.26:8080/json.htm?type=command&param=switchlight&idx=281&switchcmd=Set%20Level&level='..playercalc..'" &')
    end

    if tonumber(powerstatus) == 0 and otherdevices['Lounge Touch V'] ~= 'Off' then
        commandArray['Lounge Touch V'] = 'Off'
    end
return commandArray

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 0:44
by simon_rb
G3rard wrote:Will post my script here once it is finished.
Cheers G3rard. It's pretty much all Nautilus :D

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 0:45
by G3rard
simon_rb wrote: Cheers G3rard. It's pretty much all Nautilus :D
I know, but my script is a bit different as I have a Sonos ;)

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 7:57
by Nautilus
simon_rb wrote:Excellent - I shall give it ago.. It looks like you have changed quite a lot! lol Terassin äänenvoimakkuus = Lounge Touch V, that right?
Correct - didn't have time to change them now :D But otherwise only a small change in one of the comparisons and a bit different variables / variable naming.
G3rard wrote: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'
Yes, this is something I was thinking earlier (viewtopic.php?f=23&t=9638#p101592) as well, just didn't know about the option"2" for nValue for not triggering the script. I had earlier played with the same (using json / udevice though, i.e. /json.htm?type=command&param=udevice&idx=IDX&nvalue=NVALUE&svalue=SVALUE) and managed to change status without triggering the On/Off scripts but LUA would still be executed on devicechange. But good to know this.

It was only yesterday when I actually created the volume switch for me as well and I think the latest approach where player volume is compared to converted sValue volume works quite well, at least doesn't create a loop. Although, when the update comes from the player via the time script it send the volume back to the player once which slightly changes it and would not be necessary of course. So maybe I'll change it to this nValue = 2 option in any case (and probably also a 100 step dimmer now that I know how to create one :))

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 10:18
by simon_rb
Is it worth going back to the 100 step dimmer then? Is yours working yet? :-D

Re: Simple LMS LUA volume dimmer

Posted: Tuesday 08 November 2016 11:20
by Nautilus
simon_rb wrote:Is it worth going back to the 100 step dimmer then?
Yes, I guess it is. Though I first want to make sure the current script works properly and once I feel confident it does, then maybe change the dimmer type...:)