I would like to define a workflow in thin-edge that in one state does the following:
[publish_device_id]
script = "sh -c 'tedge mqtt pub -r \"custom/device/thinEdgeId\" {thinEdgeID: $(tedge config get device.id)}'"
on_success = "successful"
in this case I receive:
{thinEdgeId: MY_DEVICE_ID}
How do I have to escape the payload so that I receive?
{"thinEdgeId": "MY_DEVICE_ID"}
I tested several variants:
[publish_device_id]
script = "sh -c 'tedge mqtt pub -r \"custom/device/thinEdgeId\" { \"thinEdgeId\": \" $(tedge config get device.id) \"}'"
on_success = "successful"
but they did not deliver the expected result.
yeah escaping is hard, especially when you’re talking about multi levels of escaping (toml → shell).
I found the solution by reading the toml webpage which showed me how to do a multi-line literal string.
Using a toml multi-line literal strings will really help simplify things for you here, so \
are really interpreted as the shell escape character and not a toml escape character.
So the following works:
[publish_device_id]
script = '''sh -c '
tedge mqtt pub -q 1 -r custom/device/thinEdgeId "{\"thinEdgeID\": \"$(tedge config get device.id)\"}"
'
'''
on_success = "successful"
And if you’re publishing a retain message then you should also set QoS to 1, e.g. -q 1
However, since I know a little about your context (the fact that you’re using the tedge-container-bundle to run thin-edge.io), it would just be easier to add a new s6-overlay init script to your custom docker image which executes when the container starts.
For example, s6-overlay supports adding of init scripts in the /etc/cont-init.d/
folder, and you can then use a naming convention to control the execution order of the scripts, below is a script which will execute after the default 50_configure.sh
script which is included in the tedge-container-bundle image.
file: /etc/cont-init.d/60_my_custom_script
tedge mqtt pub -q 1 -r custom/device/thinEdgeId "{\"thinEdgeID\": \"$(tedge config get device.id)\"}"
And the file also needs to be executable:
chmod a+x /etc/cont-init.d/60_my_custom_script