Export sensor data to text file

  message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}};{{ states.sensor.daily_energy_consumption_niska_tarifa.state}};{{
    states.sensor.daily_energy_consumption_visoka_tarifa.state}};{{ states.sensor.pv_production_today.state}}'

if i understand you only problem is double space on the end of word state? sorry i am rooke.

We’re not on the same page here.

I need you to copy this:

{{ states.sensor.daily_energy_consumption_niska_tarifa.state }}
{{ states.sensor.daily_energy_consumption_visoka_tarifa.state }}
{{ states.sensor.pv_production_today.state }}

Then go to Developer Tools

image

Then to Template

image

And pasted what you copied into here (blue arrow)
Copy the results (red arrow) and paste them here.

done. what now ?? copy them to message?

Close enough. :smile:

You could have just copied the results instead of the screenshot, but I have the info I need.

If you right click the column header in excel and change the format to text instead of number do you get the results you want?

unfortunately not .converting line from csv 15.02.2021;10:01:10;9.27 10.88 3.76

(you can try copy line in excel)

to text and than text to columne i recive again date format

Snimka zaslona (53)

That can’t be your original excel file - the columns aren’t the same.

Close whatever you have open there and open the original CSV file as written by HA, don’t change anything in it. Right click the column header. The letter C at the top (blue arrow). Select format cells, then text. What happens?

image

they now wr ting them in number format but again when i convert from text to column i receiving data format

iSnimka zaslona (55)

Well at least we confirmed it’s excel messing with the number formats. Your sensor is working fine.
I’m not sure why you’re changing the columns or what you’re trying to do.

Quick and dirty solution - Change the entire spreadsheet to text. Click top right where row and columns meet. Format Cells to text.

image

What is your goal with this data? What are you trying to accomplish? If you can explain that, maybe I can help you there. If you’re just trying to get averages or something like that you are way over complicating things.

Thanks, Jason on all your time , i fixed all now and it is ok .It is messed up my template file.

also, i change in the template (; with (,) .

i using this data for analysis power consumption and solar productions.

regards

Hey guys… similar idea here. Would like to export sensor.MyPhone_geocoded_location to a CSV that saves updates on-the-move. So obviously if I’m on the move in a city, OR if I’m at home for 12 hours… either way every /5 minutes might not be ideal.
Basically I’m just interested in saving this sensor data; I have no idea how far back it goes but don’t want to lose it. Is this the right place for it? Thanks!

Hi Jaime,

I also wanted to export some data about a sensor for the past 7 days, and I found the homeassistant history API useful. Here is what I used to export it to a file

curl -q -s -X GET \
 -H "Authorization: Bearer $api_token"  \
 -H "Content-Type: application/json"  \
 'http://[IP-of-HA]:8123/api/history/period/2021-10-05T00:00:00Z?filter_entity_id=sensor.tank_level' \
 | jq '.[]|.[] | select (.state != "") | "\(.state), \(.last_changed)"'|tr "\"" " "

The above command gives me a value of the sensor at a given time.

I am also using jq utility to extract only the needed data from the JSON output. But you could use a different method.

you may refer REST API | Home Assistant Developer Docs for more info regarding the APIs.

Hope it is useful.

Cheers

2 Likes

It works perfectly. Thanks! My config:

image

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

Check your states template, it is wrong.
Also read the warning about states templates.

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. :neutral_face:
Do you expect a finished solution that you just have to copy and paste, or do you want to learn something?