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:

  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:

  - 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.

  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:

      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:

  - service: notify.telegram
      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:

  ievent: bash /home/pi/scripts/ {{command}}

Then from an automation:


- service: shell_command.ievent
    command: rgb-on

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

bash /home/pi/scripts/ rgb-on

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

  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
    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”?

  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:
#!/bin/bash echo $1 > ~/.homeassistant/tts/reminders.txt
#!/bin/bash echo $1 >> ~/.homeassistant/tts/reminders.txt

The shell commands:

  new_reminder_file: bash CORRECT_PATH/scripts/ "{{ text }}"
  add_reminder_file: bash CORRECT_PATH/scripts/ " {{ 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:

    shell1: /bin/bash -c "echo '{{ }}' > ~/.homeassistant/output.txt"

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.

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

I owe you a beer.