Simple LMS LUA volume dimmer

Moderator: leecollings

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 had a try with grabbing the "Level" instead of using svalue and got completely confused. Started by using your JSON for the LMS player in the time script and tried adding it to the device script and grabbing the "Level" value and comparing that rather than the svalue but too many errors etc.. So close.. lol :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 »

Am I close? I suspect my file = assert isn't quite right and my the device info.result might need more than just Level..

It runs but get a Nil value as I don't know how to read the Level from the JSON, not sure what the syntax should be lol

Code: Select all

commandArray = {}
json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl \'http://http://192.168.1.26:8080/json.htm?type=devices&rid=265'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result["Level"] 


if devicechanged['Lounge Touch V'] then

   targetvolume = vlevel
   
   if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
   
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:You have the patience of a saint. This is working except for the On command. If you say set to 20% for example it will send an On command which can't be helped (HomeKit thing). But when the scripts tries to send 'On' the volume goes to zero. Is there a way to ignore the On and after the minute the script will get the current volume state from the player and everything is fine. Its hit and miss when you use Siri to set the volume sometimes the on command is sent first and sometimes second - its when its sent second it becomes a problem as it leaves the switch with 'On' then the script tries to send 'On' and the dimmer is set to 0. Maybe its more of a domoticz thing..

Thanks for all your help and patience! :D
Ok, now this maybe starts to make more sense, maybe :D So all the time you have been testing this with homebridge / siri? And the current implementation is sending some extra 'On' command always? I'd recommend leaving siri completely out of the equation and just try to get everything working by using the dimmer switch from UI and directly from LMS. If you get it working but then the when trying through siri the behavior is different, the reason must be the homebridge / eDomoticz implementation that needs fixing. What comes to the script, there is only one situation it reacts to On command when player is already on and that is:

Code: Select all

   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
And this we need to react to dimmer level change. So I'm not quite sure what you mean with this:
Is there a way to ignore the On and after the minute the script will get the current volume state from the player and everything is fine. Its hit and miss when you use Siri to set the volume sometimes the on command is sent first and sometimes second - its when its sent second it becomes a problem as it leaves the switch with 'On' then the script tries to send 'On' and the dimmer is set to 0. Maybe its more of a domoticz thing..
As mentioned, the script does what you are asking it to do, no more, no less :) It is possible to ignore e.g. device change event for 'On' in only certain case if you have some way to identify it for the system (maybe with a help of user variable or another dummy switch).
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:Am I close? I suspect my file = assert isn't quite right and my the device info.result might need more than just Level..

It runs but get a Nil value as I don't know how to read the Level from the JSON, not sure what the syntax should be lol
Well, you have double http there. It should be enough just to:

Code: Select all

'curl http://192.168.1.26:8080/json.htm?type=devices&rid=265'
or

Code: Select all

'curl "http://192.168.1.26:8080/json.htm?type=devices&rid=265"'
to grab the level, what you have should work. Also vlevel = deviceinfo.result.Level should work as there are no spaces in the field name.
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 »

Yes a guy over at the edomoticz is looking at the on command being sent. However still when not using Siri the volume will got to an On command for some strange reason. If I grab the Level from JSON it won't matter anymore.. Image

How the hell did I miss that double http:// Been going over this all day scratching my head and searching google lol. I have just tried this and it crashes Domoticz lol Thats an achievement, never done that before lol

Code: Select all

commandArray = {}
json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl http://192.168.1.26:8080/json.htm?type=devices&rid=265'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result["Level"] 


if devicechanged['Lounge Touch V'] then

   targetvolume = vlevel
   
   if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
   
end

return commandArray
Also tried this and still crashes Domoticz lol

Code: Select all

commandArray = {}
json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl "http://192.168.1.26:8080/json.htm?type=devices&rid=265"'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result.Level 


if devicechanged['Lounge Touch V'] then

   targetvolume = vlevel
   
   if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
   
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:Yes a guy over at the edomoticz is looking at the on command being sent. However still when not using Siri the volume will got to an On command for some strange reason. If I grab the Level from JSON it won't matter anymore.. Image

How the hell did I miss that double http:// Been going over this all day scratching my head and searching google lol. I have just tried this and it crashes Domoticz lol Thats an achievement, never done that before lol

Code: Select all

commandArray = {}
json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl http://192.168.1.26:8080/json.htm?type=devices&rid=265'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result["Level"] 


if devicechanged['Lounge Touch V'] then

   targetvolume = vlevel
   
   if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
   
end

return commandArray
Also tried this and still crashes Domoticz lol

Code: Select all

commandArray = {}
json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl "http://192.168.1.26:8080/json.htm?type=devices&rid=265"'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result.Level 


if devicechanged['Lounge Touch V'] then

   targetvolume = vlevel
   
   if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
   
end

return commandArray
I think the problem is that the json decoding is triggered now on whatever change happens in your domoticz. You should move this whole bit:

Code: Select all

json = (loadfile '/home/pi/domoticz/scripts/lua/json.lua')()
    file = assert(io.popen('curl "http://192.168.1.26:8080/json.htm?type=devices&rid=265"'))
 
raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
   
    vlevel = deviceinfo.result.Level 
after "if devicechanged['Lounge Touch V'] then". Does it work then?
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 »

Its not crashing! Thanks! So I guess Domoticz was overloading - get it. Prob is I get this now when the player is turned on which actually domoticz comes up with "problem sending command" however it does because the player turns on.

2016-11-06 19:49:11.044 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua: ...pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua:23: attempt to concatenate global 'targetvolume' (a nil value)
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:Its not crashing! Thanks! So I guess Domoticz was overloading - get it. Prob is I get this now when the player is turned on which actually domoticz comes up with "problem sending command" however it does because the player turns on.

2016-11-06 19:49:11.044 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua: ...pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua:23: attempt to concatenate global 'targetvolume' (a nil value)
By the way, now's the first time I actually looked the new script. What is it exactly that you are trying to achieve? :D Aren't you now reading the current dimmer level and then setting the same level to the same dimmer, does not make any sense to me...:D

Error itself should be avoided by adding to the conditions that result into using targetvolume: "and targetvolume ~= nil"
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, no worries. It only half makes sense to me.

I'm trying to bypass the dimmer trying to send an "On" value to the player and making the volume go to 0%. Instead of comparing it to the svalue which is the same as "Data" or "Status" which can say "On". The "Level" within the JSON will always be a the number the volume is set at. This way it covers all bases. Does that make sense? :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:Haha, no worries. It only half makes sense to me.

I'm trying to bypass the dimmer trying to send an "On" value to the player and making the volume go to 0%. Instead of comparing it to the svalue which is the same as "Data" or "Status" which can say "On". The "Level" within the JSON will always be a the number the volume is set at. This way it covers all bases. Does that make sense? :D
But the device script we are talking about does not do anything to "Lounge Touch V" (= the dimmer?). It reacts to any change on "Lounge Touch V" and then either turns the "Lounge Touch" (the player?) Off or On (and sets the volume). Are you sure there isn't any other script that causes the odd behaviour with "Lounge Touch V"? As it shouldn't be this script! Or did you set it as a slave for "Lounge Touch" and forgot about it?
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 »

But the device script we are talking about does not do anything to "Lounge Touch V" (= the dimmer?). It reacts to any change on "Lounge Touch V" and then either turns the "Lounge Touch" (the player?) Off or On (and sets the volume)
I'm sure the device script sends the volume when it changes. So if the Lounge Touch V changes then the script compares the number. So if I increase the dimmer on Lounge Touch V switch then the Lounge Touch V will see its different to the LMS Lounge Touch physical player and change its volume? Is that correct? If it is then for some reason if the switch displays "On" instead of "50%" for example then the script somehow sets the physical LMS Player to 0%.

So instead of comparing the vstatus of Lounge Touch V wee compare it to the JSON file and use the Level which is always the dimmer number.. So even if for some reason it reverts to On then it doesn't matter.
Are you sure there isn't any other script that causes the odd behaviour with "Lounge Touch V"? As it shouldn't be this script! Or did you set it as a slave for "Lounge Touch" and forgot about it?
No I don't have anything else interfering with the switch, No slaves or anything.

Cheers :D
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 think the penny has just dropped.. The script just sends the svalue and thats it.. and if its "On" then it sends that which sets the radio.. I was hoping to send the Level rather than svalue.. thats what it comes down to :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: I'm sure the device script sends the volume when it changes. So if the Lounge Touch V changes then the script compares the number. So if I increase the dimmer on Lounge Touch V switch then the Lounge Touch V will see its different to the LMS Lounge Touch physical player and change its volume? Is that correct? If it is then for some reason if the switch displays "On" instead of "50%" for example then the script somehow sets the physical LMS Player to 0%.
Trust me, if the script is exactly as you have copied it here, then it only checks these conditions

Code: Select all

 if devicechanged['Lounge Touch V'] == 'Off' and otherdevices['Lounge Touch'] ~= 'Off' then
      commandArray['Lounge Touch'] = 'Off'
   elseif devicechanged['Lounge Touch V'] == 'On' and otherdevices['Lounge Touch'] == 'Off' then
      commandArray[#commandArray+1]={['Lounge Touch'] = 'On'}
      commandArray[#commandArray+1]={['Lounge Touch'] = 'Set Volume '..targetvolume}
   else
      commandArray['Lounge Touch'] = 'Set Volume '..targetvolume
   end
and acts accordingly. Which is a) turns player off (if it isn't already) b) turns the player on (if it isn't already) and sets it to the volume defined in variable "targetvolume" or c) sets it to the "targetvolume". So the script is activated when "Lounge Touch V" changes, but it does not do anything that would again change it, it would actually be an endless loop :D So yes you are correct it sends the volume to the player when it changes but this script does not compare anything, it just send the value defined in variable "targetvolume" to the player.
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 »

Ah I see. I thought it compared - my mistake :-) Can it not send the Level from the JSON as the targetvolume?


Sent from my iPhone using Tapatalk
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 think the penny has just dropped.. The script just sends the svalue and thats it.. and if its "On" then it sends that which sets the radio.. I was hoping to send the Level rather than svalue.. thats what it comes down to :D
Yes! :D But I noticed now something, you can created two types of virtual dimmers and they act very differently when set on/off. The one you have will change sValue to 0, the one's I'm using (which have only 32 steps though) keeps the sValue unchanged when set to on/off. And when Off sets the sValue to 0, then putting it back "On" will also report sValue = 0. The sValue changes to something meaningful (at least in my test) only when you move the slider. So in essence this means you should never set it to On or Off just set the slider to certain level.

Althoug, I also observed something else very strange. When using the json to set the level, the scale is offset by one. Setting it it to 0 or 1 turns the switch off (= svalue 0), setting the level to 2 turns it to 1%, setting the level to 100 turns it to 99% and setting the level to 101 turns it to 100%. Not sure how this should be factored into the code.
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:Ah I see. I thought it compared - my mistake :-) Can it not send the Level from the JSON as the targetvolume?


Sent from my iPhone using Tapatalk
Hmm, well yes I guess it should work. You change the dim level, the script is triggered and it check the level and then sets that level to the player. So yes it should work also like this. Maybe you can avoid the problems with sValue "offset" (as mentioned in the previous post) also with this approach...
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 »

Ah, I noticed this too that the volume was off by 1% but I wasn't too worried about that. I never have it at 100% volume anyways..

Ah, so which dimmer would be best? I obviously turn on the Lounge Touch player using the Lounge Touch V dimmer. Most of the time it reverts back to the previous dimmer or it can display "On". So should I change the dimmer to the 32? Would that stop the "On" problem?

Or would it best to just send the "Level" from the JSON reply. All I need to do is set it from the script above but it's coming up with a Nil, obviously my "vlevel = deviceinfo.result.Level" is wrong.. once it grabs that value then it wouldn't matter if the dimmer displays on for whatever reason as it will send the Level anyways..


Sent from my iPhone using Tapatalk
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:Its not crashing! Thanks! So I guess Domoticz was overloading - get it. Prob is I get this now when the player is turned on which actually domoticz comes up with "problem sending command" however it does because the player turns on.

2016-11-06 19:49:11.044 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua: ...pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua:23: attempt to concatenate global 'targetvolume' (a nil value)
commandArray['OpenURL'] always gives and error to me as well. If you want to avoid it, you can use os.execute('curl -s "http://url"'). The lua error is result from "targetvolume" not having any value. Add

Code: Select all

print(vlevel)
after the vlevel line to see in the log if it can read the "Level" correctly.

Edit: No it does not read it correctly :) You need to use:

Code: Select all

vlevel = deviceinfo.result[1].Level
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 »

Cheers, There is a massive delay between moving the slider and it updating the volume.. Sometimes it doesn't update at all. Noticed this in the log, it also crashes Domoticz lol. :( I suspect when you change the slider it starts a loop with the JSON and it crashes the Domoticz..

2016-11-06 22:54:20.388 Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua has been running for more than 10 seconds
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:Cheers, There is a massive delay between moving the slider and it updating the volume.. Sometimes it doesn't update at all. Noticed this in the log, it also crashes Domoticz lol. :( I suspect when you change the slider it starts a loop with the JSON and it crashes the Domoticz..

2016-11-06 22:54:20.388 Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_device_sqtouch_volume.lua has been running for more than 10 seconds
What are you using to change the dim level? Do you "click" it to another position or move the slider? Maybe moving the slider causes it to go through each step ( = multiple json calls) and thus hangs the system, not sure though. What happens if you just click to certain point in the dim slider and wait Domoticz to finish? How long does it take then and does that create some sort of loop?

You could add a check when the device was last changed and do nothing unless it has been e.g. three seconds since the last change. Maybe it would help.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest