Hi all, please help with shell_command. I want to write value of same sensor to file, but can’t get working it.
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
Log:
2022-11-02 16:47:47.600 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo {{ states("sensor.daily_rain") }} > /run/weather/daily_rain`, return code: 0:
b'0.03 > /run/weather/daily_rain\n'
Automation runs, but file is not written. Something weird in log. Why?
WZBFL
(Martin)
May 24, 2023, 10:14am
2
Same Problem here. I cant get it to run. Dont understand why.
in Terminal mode i get no state of Entity. Maybe there is the problem? Do we have access to the entitys?
It works with the “notify” function. But then every time it triggers the event, a new line is coming up. This isnt what i neede. i only want to have the value in one single line.
But HOW
Finally this works:
shell_command:
write_daily_rain: /bin/bash -c "echo {{ states.sensor.daily_rain.state }} > /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
wbyoung
January 24, 2024, 11:53pm
4
I wanted to write a multiline JSON blob to a file, and I ended up with:
configuration.yml
:
shell_command:
write_my_settings: ./write-my-settings.sh '{{ my_settings }}'
/config/write-my-settings.sh
(marked as executable):
#!/bin/bash
cat > ./my-settings.yaml <<EOF
# serialized settings automatically written daily
# this file contains a header can be read, for instance with:
#
# sed '/^#/d' my-settings.yaml | jq
$1
EOF
The automation:
alias: Store My Settings
description: ""
trigger:
- platform: time
at: "00:00:00"
condition: []
action:
- service: script.get_my_settings
data: {}
response_variable: my_settings
- service: shell_command.write_my_settings
data:
my_settings: |
{{ my_settings|to_json(pretty_print=true) }}
response_variable: command_response
- if:
- condition: template
value_template: "{{ command_response['returncode'] != 0 }}"
then:
- service: persistent_notification.create
data:
title: Failure to write my settings
message: "{{ command_response['stderr'] }}"
mode: single