Page 1 of 1

bash output pass to dzvents

Posted: Friday 15 May 2020 9:52
by m147
Hello,

I am trying to pass bash output do dzvents, unfortunately no success. Could please some one help me?
I have made helper function for that. It is based on stuff I found in this forum.

Code: Select all

return {
    helpers = {
      bash_var = function(input)
	    local cmd = input
            local f = assert(io.popen(cmd, 'r'))
            s = assert(f:read('*a'))
            f:close()
            print(s)
            output = tonumber(s1)
            print(output)
      end,
  
    }
}
Honestly I don't really know how it exactly works... I am quite noob in programming.
It gives me an output like this in log:
2020-05-15 09:44:00.654 Status: dzVents: 240.0
2020-05-15 09:44:00.654
Notice the empty line. I am not able to use output as number variable like that.

Does anyone have an idea how to remove empty line from output?
Or any suggestion with different approach?

Re: bash output pass to dzvents

Posted: Friday 15 May 2020 11:33
by waaren
m147 wrote: Friday 15 May 2020 9:52 Does anyone have an idea how to remove empty line from output?
I assume the result of the command is 240.0 ? That is what printed.
After that you try to change the type of an unknown variable (s1) to a number and print the converted var. Maybe it is your intention to change the type of s to a number ?

If s1 does not exist or cannot be converted to a number
Or any suggestion with different approach?
To suggest something I would have to understand what the script is in which you want to process the outcome of the cmd.

Re: bash output pass to dzvents

Posted: Friday 15 May 2020 11:57
by m147
Waaren, thank you for your reply.
As I wrote previously, it is copy&pasted code from this forum and it is hard for me to understand it.

I assume the result of the command is 240.0 ? That is what printed.
Yes you are right.
It kinda works, but there is in output also the empty line.

I want to use this helper generally to insert bash output as variable to dzvents scripts.

To be specific, I wrote script which should update device with voltage of UPS

Code: Select all

return {
	on = {
    	timer = {

                'every minute'

	   },
    },
	execute = function(domoticz, timer)
      local results = domoticz.helpers.bash_var('sh /home/michal/domoticz/scripts/ups.sh voltage' )
	  local voltage = domoticz.helpers.output
	  print(voltage)

	  domoticz.devices'ups napeti'.setValues(0, voltage)
	  print(voltage)
	end
}
It use the here above helper. And the helper reads bash script output.

I hope it is understandable from me :)

Re: bash output pass to dzvents

Posted: Friday 15 May 2020 16:56
by waaren
m147 wrote: Friday 15 May 2020 11:57 I want to use this helper generally to insert bash output as variable to dzvents scripts.
To be specific, I wrote script which should update device with voltage of UPS
Do you know there is a UPS plugin ? I use that one to monitor the UPS connected to my Synology..

Can you try with this in global_data.lua

Code: Select all

return
{
    helpers =
    {
        bash_var = function(cmd)
            local f = assert(io.popen(cmd, 'r'))
            local result = assert(f:read('*a'))
            f:close()
            return result
        end,
    }
}
And this in your script

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute',
       },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'get UPS',
    },

    execute = function(domoticz)
        local result = domoticz.helpers.bash_var('sh /home/michal/domoticz/scripts/ups.sh voltage' )
        domoticz.log('The result is ' .. tostring(result),dz.LOG_FORCE)
        domoticz.devices('ups napeti').setValues(0, result)
    end
}

Re: bash output pass to dzvents

Posted: Monday 18 May 2020 7:52
by m147
Hello Waaren,

Thanks for tip with ups plugin, I will look at it but I would like to solve this issue anyway.
Unfortunately still no luck:

Code: Select all

 2020-05-18 07:45:00.084 Status: dzVents: Info: get UPS: ------ Start internal script: ups:, trigger: "every minute"
2020-05-18 07:45:00.550 Status: dzVents: Info: get UPS: The result is 242.0
2020-05-18 07:45:00.550
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: Processing device-adapter for ups napeti: Voltage device adapter
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: OpenURL: url = http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=160&nvalue=0&svalue=242.0
2020-05-18 07:45:00.558
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: OpenURL: method = GET
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: OpenURL: post data = nil
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: OpenURL: headers = nil
2020-05-18 07:45:00.558 Status: dzVents: Debug: get UPS: OpenURL: callback = nil
2020-05-18 07:45:00.558 Status: dzVents: Info: get UPS: ------ Finished ups
2020-05-18 07:45:00.857 Status: EventSystem: Script event triggered: /home/michal/domoticz/dzVents/runtime/dzVents.lua
2020-05-18 07:45:00.884 Error: Error opening url: http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=160&nvalue=0&svalue=242.0
2020-05-18 07:45:00.884 
I thing this empty line is the problem:

Code: Select all

2020-05-18 07:45:00.884 Error: Error opening url: http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=160&nvalue=0&svalue=242.0
2020-05-18 07:45:00.884
When I open the here above url, device is updated correctly.

Re: bash output pass to dzvents

Posted: Monday 18 May 2020 9:23
by waaren
m147 wrote: Monday 18 May 2020 7:52 Hello Waaren,

Thanks for tip with ups plugin, I will look at it but I would like to solve this issue anyway.
Unfortunately still no luck:

Code: Select all

2020-05-18 07:45:00.884 Error: Error opening url: http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=160&nvalue=0&svalue=242.0
2020-05-18 07:45:00.884
When I open the here above url, device is updated correctly.
It looks like there is a (hidden) space type character in the return.
Can you change

Code: Select all

    execute = function(domoticz)
        local result = domoticz.helpers.bash_var('sh /home/michal/domoticz/scripts/ups.sh voltage' )
        domoticz.log('The result is ' .. tostring(result),dz.LOG_FORCE)
        domoticz.devices('ups napeti').setValues(0, result)
    end
into

Code: Select all

   execute = function(domoticz)
        local result = domoticz.helpers.bash_var('sh /home/michal/domoticz/scripts/ups.sh voltage' )
        
        result = result:match('%d+%.*%d*') -- strip anything else then the first decimal number from result

        domoticz.log('The result is ' .. tostring(result),dz.LOG_FORCE)
        domoticz.devices('ups napeti').setValues(0, result)
    end
and try again ?

What is the type / subtype of device 160 as seen on the devices tab? If the device type/ subtype is General / Voltage you can use the method updateVoltage(result)

so the code would look like

Code: Select all

   execute = function(domoticz)
        local result = domoticz.helpers.bash_var('sh /home/michal/domoticz/scripts/ups.sh voltage' )
        
        result = result:match('%d+%.*%d*') -- strip anything else then the first decimal number from result

        domoticz.log('The result is ' .. tostring(result),dz.LOG_FORCE)
        domoticz.devices('ups napeti').updateVoltage(result)
    end

Re: bash output pass to dzvents  [Solved]

Posted: Monday 18 May 2020 9:54
by m147
now it works perfectly! Thank you so much!