Trying to make a sensor with command_line: sensor does not work, file (script?) not found

I’m running the latest version of home Assistant on a RPI4 CM in a venv environment. I’m using the DSMR integration to get power readings from the P1 port of our digital meter (Belgium Fluvius). The problem I have with the integration is that it only updates every 30 seconds, which is way too slow for my an automation I have running. Ideally I’d like to have it at 1 second. The reason why I want this at ~1 second is that I want to follow power production and power usage and set a dimmer to heat water. So I need to be as close to real time as possible. This is the original topic I want to recreate myself. And it works but it would work much accurately, if my power readings were updated more frequently.

So I thought, I’ll create another sensor which is simply an influx command which updates (scan_interval) every second. Easy peasy yes?

But for some reason I can not get it to work. If I issue the influx command on the RPI in linux as the user “homeassistant” with the venv activated: it works. But not when it’s a sensor. So I figured, it might have to do something with single/double quotes in the influx command (’’ “”). Therefore, I put it in a command under /usr/local/bin/. I checked with the homeassistant user, it has /usr/local/bin in its path and the command works if I just issue it like that as the user “homeassistant”. I also checked permission bits/ownership: all good to go for homeassistant.

Het script is een influx query.

homeassistant@stats:~/.homeassistant $ whoami 
homeassistant
homeassistant@stats:~/.homeassistant $ /usr/local/bin/getcurrentpoweconsumption.sh 
0.004 # ==> 4 Watts, the command works
homeassistant@stats:~/.homeassistant $ cat /usr/local/bin/getcurrentpoweconsumption.sh
#!/bin/bash
/usr/bin/influx -database 'elektriciteit' -execute 'SELECT last("CURRENT_ELECTRICITY_USAGE") FROM "P1 values" fill(null)' | tail -n 1 | awk '{print $2}'
homeassistant@stats:~/.homeassistant $ 

In configuration.yaml, I have this:

sensor:
  - platform: command_line
    name: "Electricity consumption from P1 high poll interval" 
    command: /usr/local/bin/getcurrentpoweconsumption.sh
    unit_of_measurement: "kW"
    scan_interval: 1
  - platform: command_line
    name: "Electricity production from P1 high poll interval" 
    command: bash influx -database 'elektriciteit' -execute 'SELECT last("CURRENT_ELECTRICITY_DELIVERY") FROM "P1 values" fill(null)' | tail -n 1 | awk '{print $2}'
    unit_of_measurement: "kW"
    scan_interval: 1

The only thing I see in /home/homeassistant/.homeassistant/homeassistant.log is the line below every second. That’s strange because I guess I checked everything above (permission bits, $PATH (which is strictly even not needed if the full path is given))

2022-11-18 15:22:58.018 ERROR (SyncWorker_5) [homeassistant.components.command_line] Command failed (with return code 127): /usr/local/bin/getcurrentpoweconsumption.sh

Again, this works as the user homeassistant with the venv activated and $PATH seems to be correct if you ask me.

root@stats:/home/homeassistant/.homeassistant# ls -lah /usr/local/bin/getcurrentpoweconsumption.sh 
-rwxr-xr-x 1 homeassistant root 172 Nov 18 15:17 /usr/local/bin/getcurrentpoweconsumption.sh
/usr/bin/influx -database 'elektriciteit' -execute 'SELECT last("CURRENT_ELECTRICITY_USAGE") FROM "P1 values" fill(null)' | /usr/bin/tail -n 1 | /usr/bin/awk '{print $2}'

root@stats:/home/homeassistant/.homeassistant# /usr/bin/influx -database 'elektriciteit' -execute 'SELECT last("CURRENT_ELECTRICITY_USAGE") FROM "P1 values" fill(null)' | /usr/bin/tail -n 1 | /usr/bin/awk '{print $2}'
0.108
root@stats:/home/homeassistant/.homeassistant# 
(homeassistant) homeassistant@stats:~/.homeassistant $ echo $PATH
/srv/homeassistant/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
(homeassistant) homeassistant@stats:~/.homeassistant $

I even tried to set up SSH keypairs so the user homeassistant can log in as root on localhost (to have FULL) access rights but still, no luck with this. (note the error is different this time). I checked, on the command line the command works (ssh root@localhost influx … … … )

2022-11-18 15:56:12.993 ERROR (SyncWorker_6) [homeassistant.components.command_line] Command failed (with return code 255): /usr/bin/ssh -i /home/homeassistant/.ssh/id_rsa -o UserKnownHostsFile=/home/homeassistant/.ssh/known_hosts root@localhost '/usr/local/bin/getcurrentpowerconsumption.sh'

[<code]
homeassistant@stats:~/.homeassistant $ /usr/bin/ssh -i /home/homeassistant/.ssh/id_rsa -o UserKnownHostsFile=/home/homeassistant/.ssh/known_hosts root@localhost ‘/usr/local/bin/getcurrentpowerconsumption.sh’
0.623
homeassistant@stats:~/.homeassistant $
[/code]

The to rule out any exotic stuff, I tried this:

sensor:
  - platform: command_line
    name: "Electricity consumption from P1 high poll interval" 
    command: "/usr/local/bin/getcurrentpowerconsumption.sh"
    command_timeout: 5
    scan_interval: 15
  - platform: command_line
    name: "touch file"
    command: "/usr/bin/touch /home/homeassistant/.homeassistant/file.test"
    scan_interval: 5

And even touching a file goes wrong:

2022-11-18 20:55:46.867 ERROR (SyncWorker_3) [homeassistant.components.command_line] Command failed (with return code 127): /usr/bin/touch /home/homeassistant/.homeassistant/file.test
2022-11-18 20:55:47.213 ERROR (SyncWorker_7) [homeassistant.components.command_line] Command failed (with return code 127): /usr/local/bin/getcurrentpowerconsumption.sh

I’m at the end of everything I could have thought of. Anyone else who sees the obvious I’m overlooking?

OK so I actually got further with this. I included a file sensors.yaml in my configuration.yaml and split it up. Now I Have this in the sensors.yaml. Now the log file no longer complains that it cannot find the script but now the sensor value is always ‘0’. It’s not “unknown” (NULL) but zero. Strange, if I issue the commands I get 0.xyz kW. Even if I read something above 1.0, it’s still 0. Anyone a clue?

I guess might be value_template . I didn’t include it since I get no sensor at all if I include it and have no idea what I can do to make a value_template that works. (and if I even need one)

- platform: command_line
  name: Electricity consumption from P1 high poll interval 
  command: '/usr/bin/influx -database ''elektriciteit'' -execute ''SELECT last("CURRENT_ELECTRICITY_USAGE") FROM "P1 values" fill(null)'' | /usr/bin/tail -n 1 | /usr/bin/awk ''{print $2}'''
  unit_of_measurement: "kW"
  scan_interval: 1
  command_timeout: 5

- platform: command_line
  name: Electricity delivery from P1 high poll interval 
  command: '/usr/bin/influx -database ''elektriciteit'' -execute ''SELECT last("CURRENT_ELECTRICITY_DELIVERY") FROM "P1 values" fill(null)'' | /usr/bin/tail -n 1 | /usr/bin/awk ''{print $2}'''
  unit_of_measurement: "kW"
  scan_interval: 1
  command_timeout: 5

OK this is really driving me crazy … I’ve tried virtually everything:

  • tried to setup a sensor via the influxdb sensor: can’t make it work
  • tried to query influxdb with curl, also doesn’t work.
  • tried to write the output of the command to a file, then cat the file to get the reading: doesn’t work.
  • tried to write the output of the command to a file, then use the file platform: I can’t make the configuration valid.

Is there someone who can please take a look at this? I think I’m so close in my second post. I just don’t understand why the value stays ‘0’. And it’s only once 0. The value doesn’t seem to get updated after I restart Home Assistant.

OK, I finally got the influx platform up and running. I see it’s only accurate 2 digits after the comma (10W) but I guess it’s more than good enough. I could multiply the value by a 1000 but that would take me another 3 weeks to find out how to do it. I think I’m at peace with this result.

So I’m running influxdb v1.8 at the local computer which resolves to a local IP on port 8086. I could also put 127.0.0.1 in here I guess. The database is “elektriciteit”. There is a measurement called “P1 values” and the fields I want the value to come out of are “CURRENT_ELECTRICITY_USAGE” and “CURRENT_ELECTRICITY_DELIVERY”. The where clause only selects the last 10 seconds. If you don’t do this, it will have a dramatic - negative - effect on performance.

- platform: influxdb
  api_version: 1
  ssl: false
  host: influxdb.mydomain.org
  port: 8086
  database: "elektriciteit"
  scan_interval: 1
  queries:
    - name: "current electricity usage"
      measurement: '"P1 values"'
      field: "CURRENT_ELECTRICITY_USAGE"
      where: 'time > now() - 10s'
      unit_of_measurement: "kW"
      group_function: last
    - name: "current electricity delivery"
      measurement: '"P1 values"'
      field: "CURRENT_ELECTRICITY_DELIVERY"     
      where: 'time > now() - 10s'
      unit_of_measurement: "kW"
      group_function: last