Shell command state sensor to file - dont work?

Hi,

i want to reopen an topic : https://community.home-assistant.io/t/state-to-file/483167

i have the same Problem. Nothing is written to the file. In my case everything is ok

  • permission is set (white listed)
  • try with “notify” is working
shell_command:
   write_daily_rain: echo {{ states("sensor.daily_rain") }} > /run/weather/daily_rain

automation:
 - alias: sensor_daily_rain_to_file
   trigger:
    - platform: state
      entity_id: sensor.daily_rain
    - platform: homeassistant
      event: start
   action:
    - service: shell_command.write_daily_rain

logger:
   default: warn
   logs:
      homeassistant.components.shell_command: debug
  • Use quotes around “echo …”, i.e.
shell_command:
   write_daily_rain: 'echo {{ states("sensor.daily_rain") }} > /run/weather/daily_rain'

  • Is the file created?

EDIT: Changed double quotes to single ones

Hi,

sorry. in version something is working on Terminal:
2023-05-24 19_31_30-Terminal – Home Assistant – Mozilla Firefox

But in file i get this:
2023-05-24 19_33_00-File editor – Home Assistant – Mozilla Firefox

it’s like command shell can’t interpret sensor states or something like this?

Well, states("") is not replaced by the sensor value in an actual shell, indeed :thinking:
Why are you doing something completely different than in the OP, all of the sudden?

Hi,

because i reopen the older Topic from: # jiri.prchal.

He never got an answer, but i come to the same problem on my code.
Befor i implement such code to HA, i check it in the CLI of HA first.
And when i use this command in HA direct, nothing happens. The file is still empty. → AND no Error Messages…

I try this way, because of an completely different problem I discribed here:

https://community.home-assistant.io/t/trouble-with-totalizer-reset-all-the-time/571308

my Idea: to temporary save the values in file.

Any other ideas?

You can use the file notifier to write to a file directly from HA

Hi,

yes i know, it works already with notify. But i don’t want to have new line each value. I only want to leave it on one single line.

Ideas?

Hi Martin,

I had a similar problem and managed it by removing the existing file as first step with a shell_command, then use notify to export the data to a file and remove the first two lines afterwards, also with a shell_command.
Meanwhile I switched to pyscript using os.write() to export the data to a file.