Page 1 of 1

How to use os.execute in dzVents script

Posted: Monday 07 December 2020 18:33
by Bospieper
Hi,
I have a small script wich is telling me on a given time if the bedroom door is open or close. This is because I have an airco in that room. So 5 minutes before the airco is on the script checks if the door is open or close. In case of open it sends a notify to my mobile phone. What I want is a spoken message by izsynth by a bluetooth speaker. The speaking part comes from a tutorial from Gadgets-Freaks called Domoticz voice feedback.
Here is the script:

Code: Select all

return {
	on = {
		timer = {
			'at 22:40'
	        	}, 
	        devices = {
	             'deurcontact'
	             },
        },
            logging = {
		        level = domoticz.LOG_ERROR, -- set to domoticz.LOG_ERROR when all ok
                marker = 'Slaapkamer'
                 },        
	execute = function(dz, item, devices)
	
		local deurcontact = dz.devices('Slaapkamer - deurcontact')
		
		if item.isTimer then
		    if deurcontact.state == 'Off' then
		    dz.log('Slaapkamerdeur is dicht', dz.LOG_INFO)
		    
	    elseif deurcontact.state == 'On'  then
	           dz.notify('Slaapkamer', 'slaapkamer deur staat open', dz.PRIORITY_NORMAL,nil,nil, dz.NSS_PUSHOVER)
	           os.execute("sh /home/pi/domoticz/scripts/play_sound.sh  nl  ' test ' ") 
   		end
	end 
end
}

In the terminal when I give the command: sh play_sound.sh nl 'test' the text is converted to a mp3 file and is audible but in the script nothing happens. I've tried several things from the Domoticz forum but nothing helps.
Maybe some can piont me out what I am doing wrong.
Thanx.

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 20:00
by waaren
Bospieper wrote: Monday 07 December 2020 18:33 Maybe some can point me out what I am doing wrong.
Kind of hard to guess what you tried. "several things" might be clear to you but it does not help in preventing me of pointing you to a topic you have already seen and tried.

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 20:18
by Bospieper
Hi Waaren,
You are right for me it's clear in what I have tried. As stated before in the terminal setting the play_sound.sh is working.
Tried sudo before the command line (there are topics about rights etc)
I give the play_sound.sh all the rights 777 as user pi
tried several combinations with the quotation marks
tried several combinations with the parentheses
made the play_sound.sh executable.

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 20:47
by waaren
Bospieper wrote: Monday 07 December 2020 20:18 As stated before in the terminal setting the play_sound.sh is working.
You might want to look at this topic where a similar issue was discussed and some possible solutions were posted.

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 22:00
by Bospieper
Hi Waaren,
I have tried several solutions as mentioned in that topic but unfortunately no positive result. There was one who mentioned something like member to the audiogroup. I looked in to it and saw that only root is member. Don't now if that is right?

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 22:22
by waaren
Bospieper wrote: Monday 07 December 2020 22:00 Hi Waaren,
I have tried several solutions as mentioned in that topic but unfortunately no positive result. There was one who mentioned something like member to the audiogroup. I looked in to it and saw that only root is member. Don't now if that is right?
Probably depending on which user you use for domoticz?

Re: How to use os.execute in dzVents script

Posted: Monday 07 December 2020 22:31
by Bospieper
The user is pi within Domoticz and is also the user in the CLI

Re: How to use os.execute in dzVents script

Posted: Tuesday 08 December 2020 0:25
by waaren
Bospieper wrote: Monday 07 December 2020 22:31 The user is pi within Domoticz and is also the user in the CLI
You could try to add user pi to the audiogroup or execute the sh command as root by using sudo

Re: How to use os.execute in dzVents script

Posted: Tuesday 08 December 2020 16:27
by Bospieper
Hi Waaren,
Working late :D
I've added user pi to the audio group, that was not the solution.

And yes I already used the sudo before the sh command, also not THE solution.

I am out of options and so do you I presume.

Re: How to use os.execute in dzVents script

Posted: Tuesday 08 December 2020 17:39
by erem
i assume you refer to this: https://gadget-freakz.com/domoticz-text-to-speech/
that article is riddled with extra html like <br> and bad formatting.

i assume you removed all that since you state it works from the command line.

that leaves the activation from lua/domoticz as a cause of failure.

you can try to change

Code: Select all

os.execute("sh /home/pi/domoticz/scripts/play_sound.sh  nl  ' test ' ") 
into

Code: Select all

os.execute("/bin/bash -c"  "/home/pi/domoticz/scripts/play_sound.sh  nl  ' test ')" 
and maybe insert something like this into play_sound.sh

Code: Select all

echo "test from play_sound.sh"  >/home/pi/play_sound.txt

Re: How to use os.execute in dzVents script

Posted: Tuesday 08 December 2020 19:53
by Bospieper
Hi Erem,
Thanx for also looking in into this problem. Your suggestion of:
os.execute ("/bin/bash -c" "/home/pi/domoticz/scripts/play_sound.sh nl ' test ')"
give some errors related to the quotation marks. [24:40] ')' expected near '/home/pi/domoticz/scripts/play_sound.sh nl ' test ') '
I've tried some possibilities with the double quotation marks but that doesn't seem to help.

Re: How to use os.execute in dzVents script

Posted: Tuesday 08 December 2020 21:04
by AllesVanZelf
I os.execute like this in one of my scripts:

Code: Select all

os.execute('/home/pi/bin/sendsnapshot-nacht.sh')
Make sure the sh file is executable. I used single quotes ' instead of ". Not sure if that makes a different here.

In another script I play a sound by using a dummy switch as "switch on" switch, with the sh file as script://play-sound.sh

In DzVents you can trigger this dummy.

Re: How to use os.execute in dzVents script

Posted: Wednesday 09 December 2020 12:38
by erem
i created this script test.sh in my home directory

Code: Select all

#!/bin/bash
# test script
echo "the test script ran with parameter $1 \"$2\" " >/home/rob/test.txt
and made it executable chmod +x ./test.sh

i created this dzvents script

Code: Select all

return {
	on = {
		timer = {
			'every minute',				-- causes the script to be called every minute
	}
	},
	execute = function(domoticz, timer)
	    os.execute("/home/rob//test.sh nl 'text to be spoken' ")  
	    domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
	end
}
in the log i see

2020-12-09 12:21:00.159 Status: dzVents: Info: Timer event was triggered by every minute
2020-12-09 12:21:00.159 Status: dzVents: Info: ------ Finished os-ex-test
2020-12-09 12:22:00.236 Status: dzVents: Info: ------ Start internal script: os-ex-test:, trigger: "every minute"
2020-12-09 12:22:00.243 Status: dzVents: Info: Timer event was triggered by every minute
2020-12-09 12:22:00.243 Status: dzVents: Info: ------ Finished os-ex-test

and the file test.txt contains

the test script ran with parameter nl "text to be spoken"

this should get you on the path to a working solution

mind you, the file test.txt will be created with the owner that runs domoticz, root in my case.
if you want to read it as pi, add a chmod 755 /home/rob/test.txt after the echo statement in test.sh

suc6

Re: How to use os.execute in dzVents script

Posted: Thursday 10 December 2020 15:40
by Bospieper
Hi Erem,
Thanx for your reaction. Because of testing, adjusting files etc,etc the result is no sound whatsoever.
So I have to deal first with that problem before executing your suggestion.
Time is here a factor of importance because I almost don't have any :evil:
But as soon I have the audio working again I let you know the results.
Thanx in advance.
Piet

Re: How to use os.execute in dzVents script

Posted: Saturday 19 December 2020 20:18
by Bospieper
So, everything is up and running again even spotify on my raspberry :D
I am now trying your suggestion and let you know.

Re: How to use os.execute in dzVents script

Posted: Monday 21 December 2020 13:52
by Bospieper
erem wrote: Wednesday 09 December 2020 12:38 i created this script test.sh in my home directory

Code: Select all

#!/bin/bash
# test script
echo "the test script ran with parameter $1 \"$2\" " >/home/rob/test.txt
and made it executable chmod +x ./test.sh

i created this dzvents script

Code: Select all

return {
	on = {
		timer = {
			'every minute',				-- causes the script to be called every minute
	}
	},
	execute = function(domoticz, timer)
	    os.execute("/home/rob//test.sh nl 'text to be spoken' ")  
	    domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
	end
}
in the log i see

2020-12-09 12:21:00.159 Status: dzVents: Info: Timer event was triggered by every minute
2020-12-09 12:21:00.159 Status: dzVents: Info: ------ Finished os-ex-test
2020-12-09 12:22:00.236 Status: dzVents: Info: ------ Start internal script: os-ex-test:, trigger: "every minute"
2020-12-09 12:22:00.243 Status: dzVents: Info: Timer event was triggered by every minute
2020-12-09 12:22:00.243 Status: dzVents: Info: ------ Finished os-ex-test

and the file test.txt contains

the test script ran with parameter nl "text to be spoken"

this should get you on the path to a working solution

mind you, the file test.txt will be created with the owner that runs domoticz, root in my case.
if you want to read it as pi, add a chmod 755 /home/rob/test.txt after the echo statement in test.sh

suc6
Hi Erem,
So I tested but still no luck. First I implemented your scripts in my system with minor changes for the path to test.sh etc. This is working, I can see the text file changing when I adjust for example the line " the test script ran with parameter..........". So the os.execute works but I am not having the same lines as you have in my logfiles. The line Info:--------Start internal script: os-ex-test: trigger: 'every minute' is missing in my logfile.

I then changed in your Dzvents script the line: os.execute("/home/rob//test.sh nl 'text to be spoken' ") to os.execute(" /home/pi/domoticz/scripts/sh play_sound.sh nl 'dit is een test' ") I also used the // in your line :)
In that way my os.execute runs every minute and I can test the several options. This is what my log shows:
Status: dzVents: Info: Test: ------ Start internal script: Test:, trigger: "every minute"
2020-12-21 13:48:00.385 Status: dzVents: Info: Test: Timer event was triggered by every minute
2020-12-21 13:48:00.385 Status: dzVents: Info: Test: ------ Finished Test
2020-12-21 13:48:00.387 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
Any suggestions?
Grz. Piet

Re: How to use os.execute in dzVents script

Posted: Monday 21 December 2020 14:11
by erem
>>-Start internal script: os-ex-test: trigger: 'every minute' is missing in my logfile.
os-ex-test is the name of my dzevents script.
you see Start internal script: Test:, trigger: "every minute" as your dzvents script is named Test


try and change
os.execute(" /home/pi/domoticz/scripts/sh play_sound.sh nl 'dit is een test' ")

to
os.execute(" /home/pi/domoticz/scripts/play_sound.sh nl 'dit is een test' ")

if the play_sound.sh script is executable, no need to call sh.
if play_sound.sh is not executable, do chmod +x play_sound.sh to make i executable.

Re: How to use os.execute in dzVents script

Posted: Wednesday 23 December 2020 14:32
by erem
and also, if your domoticz runs as root, try and see if play_sound.sh functions when run as root
to become root, do sudo su, this should change the $ prompt to the # prompt