Shell command parsing of variable to append in txt file doesn't work

After spending 2 nights of trying - what feels like - a thousand different combinations and reading just as much different websites, I just can’t get the following to work.

My attempt is to append the content of a variable to a text file.

When I set up this:

shell_command:
  shell1: "echo this is a first reminder  > ~/.homeassistant/tts/reminders.txt"
  shell2: "echo this is a second reminder  >> ~/.homeassistant/tts/reminders.txt"

And then use it in the action section of my automation like this:

action:
  - service: shell_command.shell1
  - service: shell_command.shell2

This all works. The file gets created with the 1st line and the 2nd line gets added.
I differentiate between > and >> because sometimes I need the system to start with a fresh empty file, sometimes it needs to append.

However, as soon as I try the same thing with variables, it doesn’t work anymore.
What I want is the attribute of an entity to be added.

shell_command:
  shell1: "echo {{ states.calendar.google_reminder.attributes.message }} > ~/.homeassistant/tts/reminders.txt"
  shell2: "echo {{ states.calendar.google_reminder.attributes.message }} >> ~/.homeassistant/tts/reminders.txt"

In the automation, I tried calling the shell_command both through service: and through service_template: but no luck, the file doesn’t get created and nothing gets added.

I then started experimenting:

shell_command:
      shell1: "echo '{{ states.calendar.google_reminder.attributes.message }}' > ~/.homeassistant/tts/reminders.txt"
      shell2: 'echo "{{ states.calendar.google_reminder.attributes.message }}" >> ~/.homeassistant/tts/reminders.txt'
      shell3: echo '{{ states.calendar.google_reminder.attributes.message }} >> ~/.homeassistant/tts/reminders.txt
      shell4: echo "{{ states.calendar.google_reminder.attributes.message }}" >> ~/.homeassistant/tts/reminders.txt

However, all with the same result (none). I don’t think it is the entity_id that causes the issue because when I try to send the same variable to Telegram, it works fine:

action:
  - service: notify.telegram
    data_template:
      message: "{{ states.calendar.google_reminder.attributes.message }}"

This send very nicely the content to my Telegram Bot.

I also tried by first creating template sensors and then having the shell_command call the template sensor, however without success neither…

I looked in the logging but couldn’t find anything neither…

What could this be ?

I have some shell commands that accept arguments, but I haven’t set one that pulls a value from a template. Here’s an example I have of one shell_command that passes arguments to a script that talks to another device on my network:

shell_command:
  ievent: bash /home/pi/scripts/ievent.sh {{command}}

Then from an automation:

action:

- service: shell_command.ievent
  data:
    command: rgb-on

So the net is that when that automation triggers, the command line sees

bash /home/pi/scripts/ievent.sh rgb-on

I wonder if you could do something similar. Set up the shell command to be

shell_command:
  reminder: echo {{reminder_text}} >> ~/.homeassistant/tts/reminders.txt"

Then use that from any automation/script/etc where you wanted to, with:

- service: shell_command.reminder
  data_template:
    reminder_text: '{{ states.calendar.google_reminder.attributes.message }}'
1 Like

Nope, doesn’t work :(.

Only when the shell command doesn’t contain any variable, it works. But that’s useless :frowning:

Looking at this, I’m wondering if you’ve just got a string-quoting issue. Have you tried putting quotes around “reminder_text”?

shell_command:
  reminder: echo "{{reminder_text}}" >> ~/.homeassistant/tts/reminders.txt"

You’ve tested this from the command line (outside of HASS), right?

Yes I did. I now moved on to writing a bash script to try to get to it:

new_reminder.sh:
#!/bin/bash echo $1 > ~/.homeassistant/tts/reminders.txt

add_reminder.sh:
#!/bin/bash echo $1 >> ~/.homeassistant/tts/reminders.txt

The shell commands:
shell_command:

  new_reminder_file: bash CORRECT_PATH/scripts/new_reminder.sh "{{ text }}"
  add_reminder_file: bash CORRECT_PATH/scripts/add_reminder.sh " {{ text }}"

Also in the data_template I use quotes.

When trying via the command line to send something to the scripts, it works.

1 Like

Did you ever get this working?

An old thread but this tripped me up as well…

Seems that the command_shell component executes the command in different ways dependent on whether the command contains templates/variables. If templates are detected then the component executes the command by calling create_subprocess_exec() whereas without templates it uses create_subprocess_shell() instead. The difference is that the former doesn’t process the file redirection whereas the later does. If using templates then you can avoid having to create a stand-alone shell script by passing the desired command in quotes to /bin/bash -c as follows:

shell_command:
    shell1: /bin/bash -c "echo '{{ states.calendar.google.attributes.message }}' > ~/.homeassistant/output.txt"
13 Likes

Omg, thank you!!!

I literally spent the past 6 hours doing intense googling, restarting the server and rewriting the script, trying to figure out this problem. I signed up to the forum so I can write this response.

THANK YOU!!!
Same as above. This fixed a problem of sending data to serial ports.

I owe you a beer.