It works perfectly. Thanks! My config:
Hi guys,
just setting this up now but tried lots of different configs but didn’t succeed.
I copy my config.yaml:
notify:
- name: filenotify
platform: file
filename: /config/exterior.csv
timestamp: true
And my automations.yaml:
- id: '1601208243990'
alias: sensor_values_to_file
description: ''
trigger:
- platform: time_pattern
minutes: /5
condition: []
action:
- service: notify.filenotify
data:
message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}};{{states.sensor.sensor.bme280_temperature_3.state}}'
mode: single
No file is being created at all. I tried different folder paths, but nothing.
Any ideas?
Thank you
Thank you for you answer.
Finally succeed on making it work.
But I get the following data (on .txt, but .csv essentially is the same):
<template TemplateState(<state sensor.bme280_temperature_3=6.3; unit_of_measurement=°C, device_class=temperature, friendly_name=Temperatura @ 2022-02-02T23:06:41.675382+01:00>)>
I need a .txt file to be created every time the automation is run (periodically) with only the temperature value and unit i.e “15ºC”.
Is there any way to get the message from home assistant with only this data? or it has to be extracted somehow with excel macros or any other resource?
My purpose is to use this temp value an show it as an overlay in my video surveillance stream in Blue Iris, similar as what is done via Blue Iris tools.
Thanks
Show your automation.
sensor = “DH11”
serial_port = ‘/dev/ttyACM0’
baud_rate = 9600
path = “%s_LOG_%s.txt” % (str(datetime.now()), sensor)
ser = serial.Serial(serial_port, baud_rate)
with open(path, ‘w+’) as f:
while True:
line = ser.readline()
f.writelines([line.strip(), " t = %s \n " % (datetime.now())])
- id: '1643839718086'
alias: Temperatura Text
description: ''
trigger:
- platform: time_pattern
seconds: /30
condition: []
action:
- service: notify.exporttxt
data:
message: '{{states.sensor.bme280_temperature_3}}'
mode: single
I’m not sure where to put your code.
My sensor is a BME280 which is running in a wemos d1 mini and reporting to home assistant via ESPHome. It is not plugged to the HA server but connected via wifi
You didn’t read the link i posted about states templates.
Do you expect a finished solution that you just have to copy and paste, or do you want to learn something?
Of course I want to learn, a lot!!
Currently I am not even an amateur about HA…
Yes I read the warning note, but I tried to use states(‘sensor.temperature’) instead of states.sensor.temperature.state but then I got the following error when configuring the automation via the GUI:
Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]
The message that I try and get the error is the following:
service: notify.exporttxt
data:
message: '{{states('sensor.bme280_temperature_3'}}'
whereas this message it actually works:
service: notify.exporttxt
data:
message: '{{states.sensor.bme280_temperature_3}}'
That is the reason why I could not follow the warning note recommendation.
Your problem are the quotes (use different ones outside and inside the template),
service: notify.exporttxt
data:
message: "{{ states('sensor.bme280_temperature_3') }}"
^ ^
and a missing closing bracket.
Your message that actually works, returns a state object and not the state.
This would work:
message: '{{ states.sensor.bme280_temperature_3.state }}'
And another tip:
Check your templates in Developer Tools/TEMPLATE
Thank you very much! finally got it running flawlessly!
I did not know what was the developer tools for, now I see how useful is to test your templates and others…
I am only one step away from victory now. I need the .txt file to have the only so that when the last state changes, this should be erased and the new one written in the same line, so that there is only one line of the state value.
So for example, if the last temperature was 11.5 and now changes to 11.3, the .txt file should only show:
11.3
instead of showing
11.5
11.3
Maybe there is a way to only read the last line in the Blue Iris Security software Macros but if I get the .txt file like that would be the best solution.
Do you have any suggestion?
Regards!
I use shell_command
in an automation to rename my text files monthly. I imagine you could use the same process to delete the file before writing a new one.
I’m looking for a long term data storage solution for a few sensors and I wanted to understand how the notify to an external file works.
In my config I have the recorder setup to store only 7 days of data. If I use notify to write to the external file at the end of each day, will it be appending day #8 to #7? Or will I be only getting days #2 thru #8?
I’ve been playing with influxdb but would honestly prefer not to if I can avoid it.
It depends on which entity’s value you use. I use a history_stats
sensor to give me a total “yesterday” value. I write that out at something like 1AM. This way I always get only one day’s worth of data.
Here’s how I’ve defined a history_stats
sensor to give me yesterday’s total run time of my boiler’s burner.
sensor:
- platform: history_stats
name: Burner on yesterday
entity_id: binary_sensor.boiler_burner
state: 'on'
type: time
end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
duration:
hours: 24
@CaptTom,
I love your idea of using a shell command in an automation to rename or delete text files in a an automation. Would you be so kind to post an example? Thanks!
That binary_sensor.boiler_burner
in the above shows on/off indicating whether my boiler burner is firing or not. In that example my history_stats
sensor returns the total run-time of the boiler for yesterday.
Continuing with that example, a daily automation has an action which saves that previous days’ value to a text file
action:
- service: notify.burner_summary
data_template:
message: '{{ now().strftime(''%x'') }},{{ states.sensor.burner_on_yesterday.state
}}'
The file containing these daily burner run-time values is defined using the notify
integration in configuration.yaml:
notify:
- platform: file
name: burner_summary
filename: burner_summary.txt
timestamp: False
After a while there are a lot of lines in this file, one for each day. On the first of every month, I run this automation to rename that file, so next time the above runs, it’ll start a new file:
- id: id_burner_rename
alias: run_burner_rename
trigger:
- at: 00:01:00
platform: time
condition:
- condition: template
value_template: '{{ now().day == 1 }}'
action:
service: shell_command.rename_burner_log
- id: id_boiler_zone1
The automation above references a shell_command
I have defined in configuration,yaml:
shell_command:
rename_burner_log: "mv -n burnerlog.txt {{ (as_timestamp(now(),0) - (60*60*24*27)) | timestamp_custom('burnerlog_%y_%m.txt') }}"
Eventually I get a number of monthly burnerlog files, and I’ll occasionally pull them off the HA device and save them somewhere long-term.
It’s not quite the same thing as what’s in this thread, but if you also landed here trying to find a simple way to export sensor data to a text file / CSV, here’s a custom component that makes it easy:
I tried the solution above, but my CSV file is not being created, and nothing is being exported from the sensors.
notify:
- name: filenotify
platform: file
filename: Solar.csv
# timestamp: true
- alias: sensor_values_to_file
trigger:
- platform: time
at: '03:26:00'
action:
- service: notify.filenotify
data_template:
message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}};{{ states.sensor.total_pv.state }};{{ states.sensor.solarman_daily_production.state }};{{ states.sensor.solarman_daily_energy_bought.state }};{{ states.sensor.solarman_daily_load_consumption.state }};{{ states.sensor.today_max_pv }};{{ states.sensor.max_pv_daily.state }};{{ states.sensor.max_pv_weekly.state }};{{ states.sensor.max_pv_monthly.state }};{{ states.sensor.max_pv_yearly.state }}'
May you please assist as I cant figure out what I am doing wrong?
I know this is from February. But, am new here and catching up.
Saw no answer, so I thought I’d chime in.
I believe I had to manually create the file for it to be used.