Automation, saving sensor value in csv

Hello all,

I’m having trouble with my automation:
I want to save the value of my sensor in a csv data after an automation was triggert:
switch is working. sensor value showing in card on the dashboard is working as well.
Is there any other solution or is this the right way to do it?
Thank you for your help

- id: '1684537330458'
  alias: Neue Automatisierung mit CSV-Speicherung
  description: ''
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: '14:49:00'
  condition: []
  action:
    - service: switch.turn_on
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        hours: 0
        minutes: 0
        seconds: 0
        milliseconds: 500
    - service: switch.turn_off
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        seconds: 6
    - service: file.write
      data:
        path: /config/sensor_values.csv
        content: >
          {{ now().strftime('%Y-%m-%d %H:%M:%S') }},
          {% set sensor_value = states('sensor.0x00124b0003364a01_l2')|float %}
          {% set multiplied_value = sensor_value * 3 %}
          {% if multiplied_value < 1 %}
            FEHLERHAFT
          {% else %}
            OK
          {% endif %}
      mode: "a"

To my knowledge, Home Assistant doesn’t have a native file.write service call. Is the example you posted pseudo-code or are you using a custom integration that provides file.write?

Here’s a way to do it using the native File integration.

What exactly is your trouble (you are not saying this)?

I am using the File Notify integration to write sensor data to a CSV.
This is an example of the notification setup in configuration.yaml:

# File Notifications setup
notify:
  - platform: file
    name: tempnotify
    filename: '/share/logs/templog.csv'
    timestamp: true

And this is an example of an accompanying automation:

- id: '1633501438128'
  alias: SYS - Write temperatuur sensor data to CSV
  description: ''
  trigger:
  - platform: time_pattern
    seconds: /30
  condition:
  - condition: state
    entity_id: binary_sensor.ping_t7v13_01
    state: 'on'
  - condition: state
    entity_id: binary_sensor.ping_esp01_dht11
    state: 'on'
  action:
  - service: notify.tempnotify
    data_template:
      message: ',{{ states(''sensor.t7v13_01_temperature'') }},{{ states(''sensor.esp01_dht11_temperature'') }}'
  mode: restart
  initial_state: true

Note that the conditions are added to ensure that the ESP devices are online, so that the CSV is only filled with realistic data.

1 Like

thank you for the fast response.

So I’m not that familiar with yaml. The file.write I saw on a python script.

So doing it with the notify.[string] is there also a chance to check conditions first with the value of the sensor and writing the given message into the csv?

Or is the better way to get it done in a python script?

Thank you

The example you posted, ‘Neue Automatisierung mit CSV-Speicherung’, is an automation.

file.write is a python method and doesn’t exist as a service call for use in scripts and automations.

Yes.

I know that this is an automation.
Is hard to get information because of the versions/date of home assistant. there are always small changes and those have an big affect to all.

So here is now What I did:

- id: '1684537330458'
  alias: Neue Automatisierung mit CSV-Speicherung
  description: ''
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: '14:49:00'
  condition: []
  action:
    - service: switch.turn_on
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        hours: 0
        minutes: 0
        seconds: 0
        milliseconds: 500
    - service: switch.turn_off
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        seconds: 6
  condition:
    - condition: state
      entity_id: sensor.0x00124b0003364a01_l2
      state: 'on'
    - condition: state
      entity_id: sensor.0x00124b0003364a01_l2
      state: 'on'
  action:
    - service: notify.DATEN
        data:
        message: 
          "{{% set sensor_value = states('sensor.0x00124b0003364a01_l2')|float %}
          {% set multiplied_value = sensor_value * 3 %}
          {% if multiplied_value < 1 %}
            FEHLERHAFT
          {% else %}
            OK
          {% endif %}}"
     mode: restart
     initial_state: true

Thank you

The latest example you posted contains several errors, plus the following doesn’t make sense:

Here it checks if the sensor’s value is on

        - condition: state
          entity_id: sensor.0x00124b0003364a01_l2
          state: 'on'

and then here it attempts multiply the same sensor’s value by 3

{% set sensor_value = states('sensor.0x00124b0003364a01_l2')|float %}
{% set multiplied_value = sensor_value * 3 %}

The sensor’s value cannot be both on and a number. So which one is it?

So the conditions in my code are superfluous since I’m checking the state of the sensor in both the trigger condition and the action. So the entity state: 'on' is not necessary/faulty.

So first I want to set the state of the entity sensor.0x000 to a new variable sensor_value to have the opportunity to multiply it by 3. The result is a new var called multiplied_value, which is checked by conditions.

Any syntax error occur because of the The curly braces are not set right?

Thank you for your paciance

It is if the state is a number instead of on/off.

- id: '1684537330458'
  alias: Neue Automatisierung mit CSV-Speicherung
  description: ''
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: '14:49:00'
  condition: []
  action:
    - service: switch.turn_on
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        milliseconds: 500
    - service: switch.turn_off
      data: {}
      target:
        entity_id:
          - switch.0x00124b000d617210_l1
    - delay:
        seconds: 6
    - service: notify.daten
      data:
        message: >
          {{ iif(states('sensor.0x00124b0003364a01_l2') | float(0) * 3 < 1, 'FEHLERHAFT', 'OK' }}
  mode: restart

One more addition: if you want to write to a CSV-file (Comma-Separated Values) you have to add commas to the message string.
This is shown in my example:

- service: notify.tempnotify
    data_template:
      message: ',{{ states(''sensor.t7v13_01_temperature'') }},{{ states(''sensor.esp01_dht11_temperature'') }}'

The message string starts with a comma because each line starts with a time stamp (as defined in my notify example).
The contents of the CSV could with my example be like this:

2023-08-15T17:28:30.488878+00:00 ,26.2,25.3
2023-08-15T17:29:00.646317+00:00 ,26.2,25.3
2023-08-15T17:29:30.485110+00:00 ,26.2,25.3
2023-08-15T17:30:00.623172+00:00 ,26.2,25.3
2023-08-15T17:30:30.482360+00:00 ,26.2,25.3

If you need another separator you can of course replace the comma with that sign.
You only have to ensure that this character is not used anywhere else in the data.

Thank you thats perfect.
I looked up the immediate if (iff) on the home assistant homepage. That confused me at the beginning. But I understand it now and it makes it much clearer.

I looked up at the HA homepage and was trapped with this entry then:

action:
  service: notify.notify
  data:
    message: "The sun is {% if is_state('sun.sun', 'above_horizon') %}up{% else %}down{% endif %}!"

So could it be as well a way to get there or is it just to complicated/not the right way to do this.

So the only additional entry I need is in my configuration.yaml

notify:
  - platform: file
    name: daten
    filename: '/config/www/CSV/daten.csv'
    timestamp: true

You made my day and I learned more in HA.
Thanks to all of you. Have a nice evening :slight_smile:

and whats the differents between these to writings

message: >
message:   (without >)
action:
  service: notify.notify
  data:
    message: "The sun is {% if is_state('sun.sun', 'above_horizon') %}up{% else %}down{% endif %}!"
action:
  service: notify.notify
  data:
    message: >
      The sun is {% if is_state('sun.sun', 'above_horizon') %}up{% else %}down{% endif %}!

Reference:
Important Template Rules

Ok thank you all,

last question:
is there any opportunity to get ‘FEHLERHAFT’ in bold to highlight it in the csv or even colourize it?
Like in the markdown card you have ** TEXT ** to bold it.

Thank you

Comma Separated Values (CSV) is not a markup language meant for displaying formatted text.

In addition, the example you posted only stores the words FEHLERHAFT or OK and neither are in CSV format.

is it possible to send the message via persistent notification in the same automation if the battery status is faulty. (FEHLERHAFT).
Or even to a card without using HACS, card mod…

I found that with persistent notification you can use as a notify but you can’t dismiss it.

Thank you all

so I have like this in my automation for persistent notification:

service: persistent_notification.create
data:
  message: >
    {% set sensor_value = states('sensor.0x00124b0003364a01_l2')|float%} {% if
    sensor_value * 3 > 1 %} 1   BATTERIE - FEHLERHAFT {% endif %}{% set sensor_value1 =
    states('sensor.0x00124b0003364a01_l1')|float%} {% if sensor_value1 > 1 %}
    LAMPE - FEHLERHAFT {% endif %}

But it should only be triggert by condition if the battery is faulty during the automation.
And I know it always shows notification even if the message is empty.

So I think I need a 2. automation for the persistent notification:

alias: notification
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - if:
      - condition: template
        value_template: >
         
          {% set sensor_value = states('sensor.0x00124b0003364a01_l2')|float%}
          { sensor_value * 3 < 1 }
       
    then:
      - service: persistent_notification.create
        data:
          message:
            "BATTERIE FEHLERHAFT"
mode: restart

I have another attempt. But I get no notification:

alias: notification
trigger:
  - platform: state
    entity_id: sensor.0x00124b0003364a01_l2
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state | float * 3 < 1 }}"
        sequence:
          - service: persistent_notification.create
            data:
              message: BATTERIE DEFEKT