start script with parameters containing comma's

Moderator: leecollings

Post Reply
GammaKappa
Posts: 23
Joined: Friday 12 January 2018 11:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

start script with parameters containing comma's

Post by GammaKappa »

I use a simple bash script to send out an MQTT message :

Code: Select all

#!/bin/bash
# Generic MQTT send script
# $1 = topic
# $2 = message
mosquitto_pub -h 192.168.1.xxx -t $1 -m $2 
I call this script with the Blockly element Start script ... with parameter(s) ... where the parameters are the MQTT's topic and message

This works fine if I call it from Blockly with a simple argument like TestMessage
If the message argument contains comma's and is enclosed within double quotes, the script returns error 256
clip3.jpg
clip3.jpg (66.94 KiB) Viewed 1328 times

How do I correctly pass an argument that contains comma's ?
User avatar
waltervl
Posts: 5369
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: start script with parameters containing comma's

Post by waltervl »

I do not know for blockly.
But when you have a switch called "Server Offline" You can also use the On and Off action commands in the device (edit button).
See wiki https://www.domoticz.com/wiki/Managing_ ... Off_Action

With bash you normally do not need quotes unless you expect spaces in the argument.
If the command needs quotes you can put them in the bash script:

Code: Select all

mosquitto_pub -h 192.168.1.xxx -t $1 -m "$2"
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
GammaKappa
Posts: 23
Joined: Friday 12 January 2018 11:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: start script with parameters containing comma's

Post by GammaKappa »

Some further testing resulted in :

When I run the bash script from the command line with as second argument :
1,2,3 -> no problem, the MQTT message is sent out as 1,2,3
"1,2,3" -> no problem, the MQTT message is sent out as 1,2,3

When the script is called from Blockly with as second argument :
1,2,3 -> only the first digit is passed, the MQTT message is sent out as 1
"1,2,3" -> all digits are passed but the MQTT message is sent out as 1 2 3 (without the comma's)

So now the question is how do I keep the comma's in the message when calling the script from Blockly ?
User avatar
waltervl
Posts: 5369
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: start script with parameters containing comma's

Post by waltervl »

Comma's seem intentially to be removed from parameter field due to further processing.... see source code https://github.com/domoticz/domoticz/bl ... er.js#L434

As a workaround you perhaps can use the On/Off action script fields on a device as mentioned before.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
GammaKappa
Posts: 23
Joined: Friday 12 January 2018 11:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: start script with parameters containing comma's

Post by GammaKappa »

Thanks for the tip about why the comma's are being removed ! This put me on the road to a solution.

As a workaround I now set the second argument as 1_2_3 (no comma's but underscores).
This argument is passed by Blockly to the bash script as 1_2_3 as expected (and not secretly removing characters).

In the bash script I replace the underscores again by comma's, and the MQTT message is sent as 1,2,3 .

The workaround you suggested by using the On/Off switch to start the script might also work, but in my case the particular switch also sets other actions not mentioned in the example above. Keeping all the actions in one place (Blockly) makes it easier for me to manage.
User avatar
waltervl
Posts: 5369
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: start script with parameters containing comma's

Post by waltervl »

Yes, that also will work ;)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests