I’m using two command-line sensors to get values from a csv file where the 2nd needs the value from the 1st. I only need this to run once or twice a day but if I put this in scan_interval
the 2nd sensor is blank or unavailable until the interval is next up. I’m guessing that the entire command_line:
block is executed together and no values are passed back to HA until it is all done.
I’ve got a poor work-around by giving the 2nd sensor a much shorter interval, but that’s a waste of resource reading the huge csv file too often. I was hoping I could use ```trigger`` but I can’t see how. Do I have to use an automation? Guidance would be very welcome.
command_line:
- sensor:
name: TestAWK
unique_id: testawk
command: >
awk 'BEGIN {FS=","};{if ($1>="{{ utcnow().strftime('%Y-%m-%dT%H:%M') }}" && $2+3.68 {{iif(states('sensor.newtide_now')|float(0)>states('input_number.tide_height_target')|float(0),'<','>')}}= {{ states('input_number.tide_height_target')|float(0)-states('sensor.forcing_pwr')|float(0) }}) print($0)}' /config/www/NewTides.csv | awk 'BEGIN {FS=","};NR==1{print($1)}'
value_template: "{{ as_datetime(value+'+00:00') }}"
scan_interval: 43200
device_class: timestamp
- sensor:
name: TestAWKend
unique_id: testawkend
command: >
awk 'BEGIN {FS=","};{if ($1>="{{ states('sensor.testawk') }}" && $2+3.68 {{iif(states('sensor.newtide_now')|float(0)>states('input_number.tide_height_target')|float(0),'>','<')}}= {{ states('input_number.tide_height_target')|float(0)-states('sensor.forcing_pwr')|float(0) }}) print($0)}' /config/www/NewTides.csv | awk 'BEGIN {FS=","};NR==1{print($1)}'
value_template: "{{ as_datetime(value+'+00:00') }}"
scan_interval: 43200
device_class: timestamp
I’m also fretting over having to pipe into a 2nd awk
command, although I’m pretty certain it can be done by putting exit
after print
. The 1st template renders to:
awk 'BEGIN {FS=","};{if ($1>="2024-09-03T13:48" && $2+3.68 >= 4.2396) print($0)}' /config/www/NewTides.csv | awk 'BEGIN {FS=","};NR==1{print($1)}'
And NewTides.csv has records looking like:
2024-09-03T13:00:00,-2.902
2024-09-03T13:10:00,-2.857
2024-09-03T13:20:00,-2.789
2024-09-03T13:30:00,-2.699
2024-09-03T13:40:00,-2.584
2024-09-03T13:50:00,-2.445
2024-09-03T14:00:00,-2.282
All the 2nd awk
command is doing is selecting just the first line which matches the expression but I cant quite get it to work without that pipe. Can anyone help? I’ve tried adding exit
but nothing is returned:
awk 'BEGIN {FS=","};{if ($1>="2024-09-03T13:48" && $2+3.68 >= 4.2396) print($1); exit}' /config/www/NewTides.csv