sp2003
(Sp2003)
1
Hello everyone,
I have created an automation that, using the notify.file service, writes a text file with the values of some sensors every 5 minutes.
alias: metern2
description: ""
trigger:
- platform: time_pattern
minutes: /5
condition: []
action:
- service: notify.metern2
data:
message: |-
{{ '2(' + states.sensor.consumo_live_w.state + '*W)'}}
{{ '2(' + states.sensor.consumo_energia_totale.state + '*Wh)'}}
{{ '2_1(' + states.sensor.tensione.state + '*V)'}}
{{ '2_2(' + states.sensor.corrente.state + '*A)'}}
{{ '2_3(' + states.sensor.frequenza_live.state + '*Hz)'}}
{{ '2_4(' + states.sensor.power_factor.state + '*F)'}}
mode: single
How can I clear the content of the file before writing to it? I would like the text file to only contain the latest reading and not the history.
tom_l
2
You could create a shell command to delete and recreate the file. Then call that before writing to it.
four2six
(J.)
3
another shell command that should work is
truncate -s 0 filename.ext
1 Like
sp2003
(Sp2003)
4
Can you show me the correct syntax in my automation, please?
tom_l
5
Configuration.yaml
shell_command:
clear_file: "truncate -s 0 filename.ext"
Don’t forget to change the filename and you have to restart Home Assistant after saving this config (a reload will not be sufficient).
Your automation
action:
- service: shell_command.clear_file
- service: notify.metern2
data:
message: |-
{{ '2(' + states.sensor.consumo_live_w.state + '*W)'}}
{{ '2(' + states.sensor.consumo_energia_totale.state + '*Wh)'}}
{{ '2_1(' + states.sensor.tensione.state + '*V)'}}
{{ '2_2(' + states.sensor.corrente.state + '*A)'}}
{{ '2_3(' + states.sensor.frequenza_live.state + '*Hz)'}}
{{ '2_4(' + states.sensor.power_factor.state + '*F)'}}
mode: single