LUA error send command TV

Moderator: leecollings

Post Reply
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

LUA error send command TV

Post by Brutus »

Hi guys,

My wish is to make a litte LUA script for sending a JSON command to my TV (standby). This is possible because I have a 2011 or later Philips TV.
I already have a dummy switch that gets activated through a LUA ping script. With the switch off action I would like to trigger this script.

I am trying to understand the logic off lua but I do something wrong and cannot find the solution on the different topics.
This is the script I made (copied/composed) so far.

Script_device_TV.lua:


commandArray = {}
if (devicechanged['TV Slaapkamer'] == 'Off') then
os.execute('curl -X POST -H "Content-Type: application/json" -d '{ "key": "Standby"}' http://192.168.1.51:1925/1/input/key')
end


When I put this in my LUA script folder I get the following error message spammed in the logfile:
2015-01-23 23:53:00 Error: ...am Files (x86)\Domoticz\scripts\lua\Script_device_TV.lua:1: ')' expected near '{'

Thanks for the help so far.
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: LUA error send command TV

Post by D'rMorris »

I notice that you do not close your commandarray:

Code: Select all

commandArray = {}
if (devicechanged['TV Slaapkamer'] == 'Off') 
then
os.execute('curl -X POST -H "Content-Type: application/json" -d '{ "key": "Standby"}' http://192.168.1.51:1925/1/input/key')
end
return commandArray
All scripts should have a return commandArray at the end, maybe that's the issue?
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: LUA error send command TV

Post by Brutus »

D'rMorris wrote:I notice that you do not close your commandarray:

Code: Select all

commandArray = {}
if (devicechanged['TV Slaapkamer'] == 'Off') 
then
os.execute('curl -X POST -H "Content-Type: application/json" -d '{ "key": "Standby"}' http://192.168.1.51:1925/1/input/key')
end
return commandArray
All scripts should have a return commandArray at the end, maybe that's the issue?
Thnx D'rMorris for mentioning this slightly stupid error of mine :oops:

I have tried to run it again after updating the code but it does'nt solve the problem :cry:
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
mbulles
Posts: 11
Joined: Wednesday 21 January 2015 19:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Lemiers (NL)
Contact:

Re: LUA error send command TV

Post by mbulles »

Hi,

I'm using this switch on the On Action Tab for example turning on the Enigma 2 STB

http://root:passw@ipaddress/web/powerstate?newstate=4

and

This action to turn the TV of:

http://root:passw@ipaddress/web/powerstate?newstate=0

This works just fine, but if you intend to not only switch the TV using domoticz but also via your RC than you should use LUA indeed so you can actually check the state of the device to have the switch corresponds the correct active state.
Domoticz
Platform: Raspberry Pi B
Protocols: X-10 / RFY / HomeMade P1 Smartmeter USB TTL-RS232-WE on Kampstrup
Hardwarre: CM15Pro / RFXTRX433E
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: LUA error send command TV

Post by Brutus »

mbulles wrote:Hi,

I'm using this switch on the On Action Tab for example turning on the Enigma 2 STB

http://root:passw@ipaddress/web/powerstate?newstate=4

and

This action to turn the TV of:

http://root:passw@ipaddress/web/powerstate?newstate=0

This works just fine, but if you intend to not only switch the TV using domoticz but also via your RC than you should use LUA indeed so you can actually check the state of the device to have the switch corresponds the correct active state.
Thnx for your reply.

I don't think my Philips TV will accept those commands. My TV uses the Jointspace protocol that needs to be send with JSON. Or do you have A Philips TV to?
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
mbulles
Posts: 11
Joined: Wednesday 21 January 2015 19:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Lemiers (NL)
Contact:

Re: LUA error send command TV

Post by mbulles »

Hi,

Yes I have a philips Tv.

But I use my GigaBlue DVB-STB Enigma2 receiver to trigger the TV via the HDMI CIC protocol.

So actually I turn on (wake from standby) my DVB-STB which also turns on the TV automatically via the HDMI CIC setting.
Domoticz
Platform: Raspberry Pi B
Protocols: X-10 / RFY / HomeMade P1 Smartmeter USB TTL-RS232-WE on Kampstrup
Hardwarre: CM15Pro / RFXTRX433E
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: LUA error send command TV

Post by Brutus »

mbulles wrote:Hi,

Yes I have a philips Tv.

But I use my GigaBlue DVB-STB Enigma2 receiver to trigger the TV via the HDMI CIC protocol.

So actually I turn on (wake from standby) my DVB-STB which also turns on the TV automatically via the HDMI CIC setting.
Unfortunately I don't have that device so this option will not work with my setup.

But thnx for thinking along.

Greetings
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
Dingetje2004
Posts: 20
Joined: Saturday 04 June 2016 8:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by Dingetje2004 »

Hello, just became a domoticz user and saw your post from last year. Did you ever found a solution, since I am facing the same difficulties now? In Domoticz configuration there is only one HTTP Post possible, but I would like to have more and therefore would like to create a LUA Script what will send commands to my Philips TV.
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: LUA error send command TV

Post by Brutus »

Hi Dingetje2004,

I haven't tried anymore. The TV is question is also sold.

Greetings Brutus.
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: LUA error send command TV

Post by Westcott »

Hi Dingetje2004,

If you use Lua, you can use a Socket, or perhaps a call to curl -

local url = 'curl -m 5 "http://your-url"'
local data = assert(io.popen(url))
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
Dingetje2004
Posts: 20
Joined: Saturday 04 June 2016 8:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by Dingetje2004 »

Thanks, will try 2morrow!
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: LUA error send command TV

Post by felix63 »

This might help in controlling a Philips TV... Some complicated (I think) logic to control your TV with a selector switch. Only prerequisite is a way of keep track of the energy consumption of your TV. You can use similar construction to switch sound level. You can use http://ip-address:1925/1/doc/API.html to find all possible commands you can send your TV.

1- obtain IP address of your TV
2- use

Code: Select all

http://ip-address:1925/1/sources
3- create a selector switch with values 'Off' and the names from the output of step 2
4- create a local variable ('televisie-status') to keep track of the TV
5- now we need to create a scripts to switch the TV

Code: Select all

--
-- Dit script stuurt afhankelijk van de huidige status van de televisie het benodigde commanda om de TV te schakelen.
-- Replace IP-address with the IP-address of your TV
-- Replace NAME-X and ID-X values in the script with the values from step 2, replicate as many elseif's as you need
--

--Change the values below to reflect to your own setup
	local status_uservar   = 'televisie_status'	-- user variable used to track the state of the TV, values 'On', 'Off' or 'Standby'
	local selector		= 'TV Bron'
	local switch_command = "curl -X POST -H \"Content-Type: application/json\" -d '{ \"key\": \"Standby\"}' http://192.168.178.36:1925/1/input/key &"

    commandArray = {}
     if devicechanged[selector'] == 'Off' then
        print('TV switch gaat uit')
        if uservariables[status_uservar] == "On" then
            print('status van de TV is aan, dus we gaan uitschakelen')
    		os.execute(switch_command)
    		commandArray['Variable:' .. status_uservar]='Standby'
        end
    elseif devicechanged[selector] == 'NAME-1' then 
        if uservariables[status_uservar] == "Standby" then
            print('De TV staat kennelijk uit, dus we gaan de TV inschakelen')
            os.execute(switch_command)
    		commandArray['Variable:' .. status_uservar]='On'
        end
        if uservariables[status_uservar] == "On" then
            os.execute("curl -X POST -H \"Content-Type: application/json\" -d '{ \"id\": \"ID-1\"}' http://192.168.178.36:1925/1/sources/current")
        end
    elseif devicechanged[selector] == 'NAME-2' then 
        if uservariables[status_uservar]  == "Standby" then
            print('De TV staat kennelijk uit, dus we gaan de TV inschakelen')
            os.execute(switch_command)
    		commandArray['Variable:' .. status_uservar]='On'
        end
        if uservariables[status_uservar] == "On" then
            os.execute("curl -X POST -H \"Content-Type: application/json\" -d '{ \"id\": \"ID-2\"}' http://192.168.178.36:1925/1/sources/current")
        end
    end
return commandArray
6. The second script is for checking the status of your TV:

Code: Select all

--
-- Dit script checkt het stroomgebruik van de televisie om daaruit de status van de tv af te leiden (On/Standby/Off) en dat vast te leggen in een lokale variabele  
--
local ip_address              	= '192.168.178.36'  	 	--IP address of the Philips TV
local port                    	= '1925'                          	--Port for commands
local tv_status_uservar      = 'televisie_status'       	--Status of the TV (On/Off/Standby)
local tv_source               	= 'TV Bron'                       	--Selector Switch with possible Sources (now static, could be made dynamic)
local energy_consumption	 = 'televisie_gebruik'         	--Name of Z-Wave plug that contains actual consumption of television (in Watts)
local idle_minutes            	= 1                               	--The amount of minutes the consumption has to stay below the 'consumption_lower' value
local consumption_upper 	= 34                              	--If usage is higher than this value (Watts), the television is on
local consumption_lower	= 20                              	--If usage is lower than this value (Watts), the television is off
local light_sensor              = "Woonkamer Lux"           		--If this switch is on it is dark 

local television_usage = tonumber(otherdevices_svalues[energy_consumption])

commandArray = {}

--Virtual switch is off, but consumption is higher than configured level, so tv is on has started
if (television_usage > consumption_upper) and uservariables[tv_status_uservar] ~= "On" then
    commandArray['Variable:' .. tv_status_uservar]='On'
    print('Current power usage (' ..television_usage.. 'W) is above upper boundary (' ..consumption_upper.. 'W), so television is on!')
--television is not using a lot of energy, check the counter
elseif (television_usage < consumption_lower) and uservariables[tv_status_uservar] ~= "Off" then 
    commandArray['Variable:' .. tv_status_uservar]='Off'
    commandArray[tv_source]='Set Level: 0'
    print('Current power usage (' ..television_usage.. 'W) is below lower boundary (' ..consumption_upper.. 'W), so television is off!')
--television is using some energy
elseif (television_usage > consumption_lower) and (television_usage < consumption_upper) and uservariables[tv_status_uservar] ~= "Standby"  then
    commandArray['Variable:' .. tv_status_uservar]='Standby'
    commandArray[tv_source]='Set Level: 0'
    print('Current power usage (' ..television_usage.. 'W) is between lower and upper boundary (' ..consumption_upper.. 'W), so television is on standby!')
end

--Check to see if we need to turn some lights on to prevent watching TV in the dark
--if (uservariables[tv_status_uservar] == 'On') and otherdevices['TV'] == 'On' then
if (uservariables[tv_status_uservar] == 'On') then
    local http = require('socket.http')
    local ltn12 = require('ltn12')
    local json = require('dkjson')
    
    if (tonumber(otherdevices_svalues[light_sensor]) <= 2 and otherdevices["Staande lamp"] == 'Off') then
        print("Het is donker:"..otherdevices_svalues[light_sensor].." dus we dan wat licht aan")
        commandArray['Scene:TV verlichting'] = "On"
    end 

--Check actual TV Source and reflect setting on virtual selector switch
    t = {}
    local url = string.format("http://%s:%s/1/sources/current", ip_address, port)
    b, c, h = http.request{url=url, sink = ltn12.sink.table(t), method='GET'} 
    url = tostring(table.concat(t))
    local obj, pos, err = json.decode (url, 1, nil)
    if err then
        print ("Error:", err)
    else
        if obj.id == "ID-1" and otherdevices[tv_source] ~= 'NAME-1'  then commandArray[tv_source]='Set Level: 10'
            print("Selector aanpassen naar NAME-1")
        elseif obj.id == "ID-2" and otherdevices[tv_source] ~= 'NAME-2'    then commandArray[tv_source]='Set Level: 20'
            print("Selector aanpassen naar NAME-2")
        elseif obj.id == "ID-3" and otherdevices[tv_source] ~= 'NAME-3'    then commandArray[tv_source]='Set Level: 30'
            print("Selector aanpassen naar NAME-3")
        end
    end
end

return commandArray
7. That's it. Please not that if your TV has been off - not standby but really of power - it has no IP-address. In that case the switch commands will throw an error and nothing will happen. That means you have to use your remote once to turn the TV on. Thereafter the script and switch will work.
Trunald
Posts: 10
Joined: Saturday 04 March 2017 11:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by Trunald »

Thank you very much felix63, this is very helpful. Unfortunately I am a completely noob in both Domoticz, and in scripting. I have managed to create the variable, the selector switches, paste the modified script as a saved LUA evenet script, but for the life of me, I cannot figure out how to "call" the script. Not exactly sure whether my selector switches are configured correctly for the purpose, either.

But I am sure I've got the fixed IP of the TV correctly, and Domoticz is communicating with it; I have managed to input text to the TV screen with another script on various events.

I would appreciate your assistance a lot.
Attachments
domoticz.jpg
domoticz.jpg (214.9 KiB) Viewed 6459 times
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Re: LUA error send command TV

Post by rizzah »

Does anyone know how to get the "On" command working on Philips TVs?
I got the Standby working just fine, but it does not respond to "On".

i found this url: http://www.supportforum.philips.com/en/ ... ilips-tv-s
But the POST at the bottom does not work either, simply because the IP is unreachable.

Maybe the wake-on-wlan needs a different syntax?
cipriani
Posts: 3
Joined: Friday 01 December 2017 19:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by cipriani »

Hi rizzah,

I'm using "cec-client" to switch it ON : my raspberry is connected to my Philips TV over HDMI.
But, when my raspberry restarts, it turns ON the TV, so i've a command to switch it off after restarting...

A new option seems to be available in new Philips TV : wake on lan over Wifi and must be activated on the TV Menu.
I'm looking into this...

Regards
-------
Sorry for my english, i'm french :)
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Re: LUA error send command TV

Post by rizzah »

I have looked into that, i enabled those options (you need to enable that wol over wifi and fast startup also). But the fast start up only works for a period of 4 hours. Atleast you have the ability to set three different times, but with a max of 4 hours. So thats kind of useless.
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by kofec »

If some want. I have created python plugin for Domoticz. Any comment are welcome.

https://github.com/kofec/Domoticz-Enigma2
HenkB
Posts: 1
Joined: Friday 21 December 2018 11:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA error send command TV

Post by HenkB »

I am struggling with this issue for hours now, and maybe somebody here can point me to what is going wrong.
Goal : turn off the tv trough a virtual switch (SW_TVSTATUS). If this switch gets the status Off , it should run a script that sends a json post to the philips TV.
I do have the CURL working from the command line on the raspberry pi, it woks like a charm.
But when I copy this into the lua device script, it does not execute the code. The script is triggered, I checked by printing a line in the logfile.
So basically : what needs to be changed in the working commandline CURL syntax to get it working in the lua script ?

working on commandline : curl -X POST -d '{"key":"Standby"}' http://192.168.15.61:1925/1/input/key

script_device_TVUIT:

commandArray={}
if (devicechanged['SW_TVSTATUS'] == 'Off') then
print'TV Off...'
os.execute ('curl -X POST -d "key":"Standby" "http://192.168.15.61/1/key"')
end
return commandArray

I omitted the { and } because if I put them in, I get the error of the topic starter. I assume it is a simple syntax issue, but I do not see it.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests