Save state to text file

Hi Guys,
I’m wanting to save a sensor state to a text file and I’ve come across https://www.home-assistant.io/components/notify.file/

which outputed the below
Home Assistant notifications (Log started: 2019-03-27T13:25:35.741025+00:00)
--------------------------------------------------------------------------------
KmtyOWV-Wus

is there anyway to just have the sensor state and nothing else?

Try hass-cli.

1 Like

Hi,
First try to help in the community so lets see if i can :slight_smile:
I am using notify.file to get a sensor state.
Now i have made an rather ugly solution maybe, to empty the file from the extra added rows.

I use a bash script to “clean” the file. And after the data is used i empty it.

For me it is used to create another script for sending notifications to a lametric clock, there for it has to be specific for the next script to work.

Here is my solution:
I use this in a bash-script execution:

sed -n '3p' /NOTIFY_TO_FILE.txt | tr -d '\n'
sleep 10
> /NOTIFY_TO_FILE.txt

Hope it helps :slight_smile:

3 Likes

I use the notify-file component for this and have the file set up like this:

notify:
  - platform: file
    name: sumppumplog
    filename: sumppumplog.csv
    timestamp: False

And then use this automation:

# Write Count of SumpPumpRuns per Day
- alias: Write SumpPumpCount per Day
  trigger:
    platform: time
    at: '23:59:45'
  action:
    service: notify.sumppumplog
    data_template:
      message: >
        {{ now().strftime('%d/%m/%Y') }}, {{ states.sensor.sumppump_runtime_count.state }}

After the first run I manually edited the header so that I can read the csv properly as a table; the sutomation only adds info at the bottom and leave the previous entrie s unchanged.

12 Likes

Thank you this is really helpful

1 Like

I too found this quite helpful, thanks!

Same here, very helpful thanks!

I know it’s been a while since your post but just wanted to thank you for your solution re. the notify-file component. This is just what I needed.
As a side note to Synology users I believe that you have to stop/start the Home Assistant for it to reload the configuration file (took me a while to figure out!)

1 Like

Hi,
I am trying to replicate the same setup a I have the same need but it fails (running current current version of HA).
When I run the automation, the traces feature from automation indicates that the action is not found :frowning:
Error: Action notify.shellyenergyconsumption not found
Here is its decl in configuration.yaml:
notify:

  • platform: file
    name: shellyenergyconsumption
    filename: /config/tools/ShellyEnergyConsumption/data.csv
    timestamp: false

I have added the directory : /config/tools/ShellyEnergyConsumption/ to the list of authorised directories:
homeassistant:
allowlist_external_dirs:
- ‘/config/tools/ShellyEnergyConsumption/’

in dev-tools/states I can read that the id : notify.shellyenergyconsumption exists in an unknown state.

Could you confirm that your setup still run today ?
Thx in advance for your advice.

This thread is rather old.
Perhaps it’s easier to use the google sheets integration?

As long as nothing is written to the file, the state will be ‘unknown’. As soon as something is written, the state changes to the last written timestamp.

Maybe show the automation that writes to the file.

file notifications are not configured in configuration.yaml any more.

My notification is like this:

notify:
  - platform: file
    name: shellyenergyconsumption
    filename: /config/tools/ShellyEnergyConsumption/data.csv
    timestamp: false
Where should I declare my notification, if not in configuration.yaml ?? I read the linked doc but did not find where to put it.



My automation looks like this:

- id: '192837465'
  alias: Save Shelly Meters Total per Day
  triggers:
  - at: '23:55'
    trigger: time
  action:
    service: notify.shellyenergyconsumption
    data:
      message: "{{ now().strftime('%d/%m/%Y') }}"

I will have a look at google sheet integration to check if it can help.
Thx


must be

 action:
  - service: notify.send_message
    target: 
      entity_id: notify.shellyenergyconsumption
    data:
      message: "{{ now().strftime('%d/%m/%Y') }}"
1 Like

THANKS A LOT!!!

You save my life. I misread the send_message service.

I am happy :slight_smile: