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?