Simple LMS LUA volume dimmer

Zycker
Posts: 31
Joined: Monday 29 December 2014 9:04
Target OS: Linux
Domoticz version:
Contact:

Simple LMS LUA volume dimmer

Post by Zycker » Wednesday 30 December 2015 15:52

Hi,
I'm starting LUA scripting, before that I just used bash but with no big complexity.
Today I've wanted to make a dimmer that could change the volume and switch off my squeezelite player (installed on the same machine as domoticz).

Here is my code (with bits that I did find on this board) :

Code: Select all

commandArray = {}
DomDevice = 'Platine';


if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
     commandArray['Musique_salon']='Off';
   else
     commandArray['Musique_salon']='On';
	 DomValue = otherdevices_svalues[DomDevice];
     CalcValue = (DomValue*100) / 31;
     commandArray['Musique_salon']='Set Volume '..CalcValue;
   end

end
return commandArray
"Platine" is the name of my dimmer, "Musique_salon" is the name of my LMS device which works fine.
The Off part and volume part works fine but the commandArray['Musique_salon']='On' doesn't seems to be send as my player doesn't turn on.
I've tried to invert the On part and the Volume part and it seems that again, only the last command is send (so the player is turned on but Volume doesn't change).
This proves that the On command works but not when I use two commandarray in the else part.

Any idea ?

User avatar
jvdz
Posts: 951
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Simple LMS LUA volume dimmer

Post by jvdz » Wednesday 30 December 2015 16:12

Your second entry for ['Musique_salon'] setting the volume overrides the first array entry for switching it on.
Try this:

Code: Select all

commandArray = {}
DomDevice = 'Platine';


if devicechanged[DomDevice] then
	if(devicechanged[DomDevice]=='Off') then
		commandArray['Musique_salon']='Off';
	else
		DomValue = otherdevices_svalues[DomDevice];
		CalcValue = (DomValue*100) / 31;
		commandArray[1]={['Musique_salon']='On'}
		commandArray[2]={['Musique_salon']='Set Volume '..CalcValue }
	end
end
return commandArray
Jos

Zycker
Posts: 31
Joined: Monday 29 December 2014 9:04
Target OS: Linux
Domoticz version:
Contact:

Re: Simple LMS LUA volume dimmer

Post by Zycker » Wednesday 30 December 2015 16:45

Hi Jos,
I still have the same issue, only the content of the first commandArray is execute, I've tried On then Volume and Volume then On and only the last command is done.

User avatar
jvdz
Posts: 951
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Simple LMS LUA volume dimmer

Post by jvdz » Wednesday 30 December 2015 16:52

Pretty sure both will be ran by the event system in this way, but unsure how they are handled after that as I don't know how this system is integrated.
Does it require some time between the commands for the unit to switch on first?

Jos

Zycker
Posts: 31
Joined: Monday 29 December 2014 9:04
Target OS: Linux
Domoticz version:
Contact:

Re: Simple LMS LUA volume dimmer

Post by Zycker » Wednesday 30 December 2015 16:59

I can't tell, it's just software but I might have a clue.
I was trying to add the control of my amplifier in this script :

Code: Select all

commandArray = {}
DomDevice = 'Platine'

if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
     commandArray['Musique_salon']='Off'
     commandArray['D802']='Off'
   else
     DomValue = otherdevices_svalues[DomDevice]
     CalcValue = (DomValue*100) / 31
     commandArray[1]={['D802']='On'}
     commandArray[2]={['Musique_salon']='Set Volume '..CalcValue }
   end

end
return commandArray
So D802 is my amplifier, I've tried both way to use commandArray and in this case it all works fine si it seems there is a limitation whitin LMS/squeezelite that can't receive two commands in this short time.
But in domoticz I've put this "http://192.168.1.10:9000/status.html?p0 ... 2.168.1.10" in Action On of my dimmer and with that and the lua script my player turns on/off correctly and my amplifier with it, volume still Ok.
It's not that clean but it works.

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Tuesday 01 November 2016 11:02

Top man!

Is this still working? I've been trying to get a script to run to do the exact same thing your trying to do. Then I can use Siri to change the volume by saying "set Kitchen Radio to 50" for example.

Which script should I use? I just want to turn it on and off and be able to get/change the volume via the dimmer?

Cheers mate.

PS. Is it first script followed by "http://192.168.1.10:9000/status.html?p0 ... 2.168.1.10" in Action On

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Tuesday 01 November 2016 11:37

I have tired below with my dimmer called Lounge Touch V and the LMS player is called Lounge Touch, I get this error

2016-11-01 09:35:09.516 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_sqtouch (24:10:2016, 16:52).lua: .../scripts/lua/script_time_sqtouch (24:10:2016, 16:52).lua:5: attempt to index global 'devicechanged' (a nil value)
2016-11-01 09:35:00.857 (Dummy) Light/Switch (Lounge Touch V)

Code: Select all

commandArray = {}
DomDevice = 'Lounge Touch V';


if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
      commandArray['Lounge_Touch']='Off';
   else
      DomValue = otherdevices_svalues[DomDevice];
      CalcValue = (DomValue*100) / 31;
      commandArray[1]={['Lounge_Touch']='On'}
      commandArray[2]={['Lounge_Touch']='Set Volume '..CalcValue }
   end
end
return commandArray

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Tuesday 01 November 2016 13:34

What a Twat! I Changed it from time to device. I have a 100% dimmer so will have to change it a bit lol

Got it working, is there a way to refresh the status of the volume? So the switch knows what the volume is if we change it on the player itself..

Cheers

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

Re: Simple LMS LUA volume dimmer

Post by Nautilus » Tuesday 01 November 2016 14:24

simon_rb wrote:What a Twat! I Changed it from time to device. I have a 100% dimmer so will have to change it a bit lol

Got it working, is there a way to refresh the status of the volume? So the switch knows what the volume is if we change it on the player itself..

Cheers
By the way, which dimmer type is it that nowadays has 100 steps? I read somewhere that the dummy dimmers should have 100 steps but if I create one, I still get only 32 (0-31).

I think you already know the answer to your question. You need to read it from the player json and update from there...:D

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Tuesday 01 November 2016 14:48

You right but I got the number but couldn't get it onto the dimmer. I'm not sure how I got the dimmer with 100 steps but I did lol.

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

Re: Simple LMS LUA volume dimmer

Post by Nautilus » Tuesday 01 November 2016 16:27

simon_rb wrote:You right but I got the number but couldn't get it onto the dimmer. I'm not sure how I got the dimmer with 100 steps but I did lol.
There's at least two ways to do that. If the variable holding the volume is called "volume" then it would be either

Code: Select all

commandArray['UpdateDevice']='idx|0|'..volume
where idx is the actual idx of the dimmer switch. Or with json:

Code: Select all

commandArray['OpenURL'] = 'http://127.0.0.1/json.htm?type=command&param=switchlight&idx=IDX&switchcmd=Set%20Level&level='..volume
where again IDX is the actual idx of the device and url pointing to domoticz.

And maybe the third option (which is essentially the same as second) would be:

Code: Select all

os.execute('curl -s http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=IDX&switchcmd=Set%20Level&level='..volume)
Note: the this option may require some switches before the http part (like curl -s -i -H "Accept: application/json") but for me it works without any on the same machine at least.

Plenty of examples of each method available in wiki and in the forum :D

Have you checked from "Events -> Show Current States" that when you have the dimmer at 100% it shows sValue of 100 (or 99 ?) for the switch so it has really 100 steps? Because at first I though this was the reason you were not able to update the switch with any value over 31. Anyhow, it should work like this...

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Tuesday 01 November 2016 17:55

Excellent. I'm at work, I'll take a lot when I get home as I'm eager to get this working. Cheers mate!

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Wednesday 02 November 2016 2:40

Well... I have managed to get it half working with grabbing the status but its doing it all the time and making my pi run at 50% when its usually only 2%. Got to figure a way of doing it only when the device changes. How do I do that? How to grab feedback when it changes and not all the time. Device Change wouldn't work because it polls it all the time. I'm so confused! The two scripts I have running at the mo are

Grab Status:-

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"]
    print(tostring(volume))
    commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=265&switchcmd=Set%20Level&level='..volume

return commandArray
Send command:-

Code: Select all

commandArray = {}
DomDevice = 'Lounge Touch V';


if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
     commandArray['Lounge Touch']='Off';
   else
     commandArray['Lounge_Touch']='On';
    DomValue = otherdevices_svalues[DomDevice];
     CalcValue = (DomValue);
     commandArray['Lounge Touch']='Set Volume '..CalcValue;
   end

end
return commandArray

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Wednesday 02 November 2016 3:18

Ok, I now have the Grab Status one running every minute which is fine however I only want it to run when the switch is on as every time it updates the switch it turns the switch on. I only want it to update the dimmer value when its actually on..

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"]
    print(tostring(volume))
    commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=265&switchcmd=Set%20Level&level='..volume

return commandArray

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

Re: Simple LMS LUA volume dimmer

Post by Nautilus » Wednesday 02 November 2016 9:01

simon_rb wrote:Ok, I now have the Grab Status one running every minute which is fine however I only want it to run when the switch is on as every time it updates the switch it turns the switch on. I only want it to update the dimmer value when its actually on..
As you have the "player switch" you could control the volume switch so that if the player switch is off, then the volume switch is off. And when the player switch turns on, it turns the volume switch on. If player switch is called "Lounge Touch" this would be with LUA something like:

Code: Select all

commandArray = {}

if devicechanged['Lounge Touch'] ~= 'Off' and devicechanged['Lounge Touch'] ~= 'Disconnected' then --add more conditions if needed (this reacts to 'On', 'Playing' and 'Stopped')
	commandArray['Lounch Touch V'] == 'On'
elseif devicechanged['Lounge Touch'] == 'Off' or devicechanged['Lounge Touch'] == 'Disconnected' then
	commandArray['Lounch Touch V'] == 'Off'
end

return commandArray
But you could maybe also just define the volume switch as slave to player switch.

Then the time script for reading the volume would be (voluem is updated only if the actual volume differs from the volume switch value and the player is on):

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"]
	switchVolume = otherdevices_svalues['Lounge Touch V']
	
	if tonumber(volume) ~= tonumber(switchVolume) and otherdevices['Lounge Touch'] ~= 'Off' and otherdevices['Lounge Touch'] ~= 'Disconnected' then
		commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=265&switchcmd=Set%20Level&level='..volume
	end	

return commandArray
as a second option you could always read the palyer power status from the json:

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']
	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=265&switchcmd=Set%20Level&level='..volume
	end	

return commandArray
And if you want, you could also change the on / off behavior so that you read the volume level when the player turns on and update it directly to correct volume. But the above will sort it as soon as the time script runs so not too much added benefit from this...

edit: I'd like to add that there are actually quite many "devicechanged" events for the Squeezebox players as when the playing track changes, it also triggers the devicechanged event. Because of this, I've created a uservariable for each player that get's value of 1 if the player is on and playing something and value of 0 if it is off, disconnected or stopped. Then I use this variable in other scripts to avoid the scripts triggering on each track change.

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Wednesday 02 November 2016 13:10

Thanks for your help once again! I shall try and implement them. I did toy with having the volume as a dimmer to the main LMS Lounge Touch switch however when turning on the volume the main LMS switch would switch on however the actual physical squeeze player didn't turn on. So I may ignore the main LMS player switch and just use the volume as volume and power.

Thanks for your help!!!! :D

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Wednesday 02 November 2016 13:45

Seems to be working great now! More testing and add my other players then I'll post the final scripts here incase others want to use it :)

Thanks dude!

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Thursday 03 November 2016 2:12

All working other than getting the dimmer/volume switch to mirror the LMS switch within Domoticz. Just having a prob with this script. Error message is 2016-11-03 00:07:46.520 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_sqtouch_on_off.lua: ...pi/domoticz/scripts/lua/script_device_sqtouch_on_off.lua:4: syntax error near '=='

Code: Select all

commandArray = {}

if devicechanged['Lounge Touch'] ~= 'Off' and devicechanged['Lounge Touch'] ~= 'Disconnected' then --add more conditions if needed (this reacts to 'On', 'Playing' and 'Stopped')
   commandArray['Lounge Touch V'] == 'On'
elseif devicechanged['Lounge Touch'] == 'Off' or devicechanged['Lounge Touch'] == 'Disconnected' then
   commandArray['Lounge Touch V'] == 'Off'
end

return commandArray
Cheers

simon_rb
Posts: 674
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Simple LMS LUA volume dimmer

Post by simon_rb » Thursday 03 November 2016 2:22

Change above script to one below and getting an error with line 5: 'then' expected near '=' which I don't understand as its same as above it..

Code: Select all

commandArray = {}

if devicechanged['Lounge Touch'] ~= 'Off' and devicechanged['Lounge Touch'] ~= 'Disconnected' then --add more conditions if needed (this reacts to 'On', 'Playing' and 'Stopped')
   commandArray['Lounge Touch V'] = 'On'
elseif devicechanged['Lounge Touch'] = 'Off' or devicechanged['Lounge Touch'] = 'Disconnected' then
   commandArray['Lounge Touch V'] = 'Off'
end

return commandArray


Cheers

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

Re: Simple LMS LUA volume dimmer

Post by Nautilus » Thursday 03 November 2016 9:32

Yes, sorry there was a mistake in my example. When comparing something in a condition, "equals" condition is set with a double equal sign, i.e. -> ==

But when setting the state to commandArray, it is single. So correct script is:

Code: Select all

commandArray = {}

if devicechanged['Lounge Touch'] ~= 'Off' and devicechanged['Lounge Touch'] ~= 'Disconnected' then --add more conditions if needed (this reacts to 'On', 'Playing' and 'Stopped')
   commandArray['Lounge Touch V'] = 'On'
elseif devicechanged['Lounge Touch'] == 'Off' or devicechanged['Lounge Touch'] == 'Disconnected' then
   commandArray['Lounge Touch V'] = 'Off'
end

return commandArray
Note that < (less than); > (greater than); <= (less than or equal); >= (greater than or equal); and ~= (not equal to) are all valid for setting conditions as well in addition to == (equal).

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests