Hello, I have following sensor configured in configuration.yaml:
command_line:
- sensor:
unique_id: solar_current_hour_yield
name: Solar power current hour yield
command: 'python3 /homeassistant/pyscript/solar_panels_live_hour_report_ha.py'
unit_of_measurement: kWh
value_template: '{{ value }}'
Python script is this one, the output is a floating point value e.g. “0.34”:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from urllib.request import urlopen, Request
from datetime import datetime
from json import loads as json_loads
def get_readings_for_current_hour():
"""
Get readings for current hour so far in kWh.
Assumes that subsequent readings for current hour are 0 kW
"""
now = datetime.now()
try:
response = urlopen(Request("http://192.168.1.16/yields.json"))
except:
# "Can't reach the panels"
return -1
r = response.read().decode("utf-8")
data = json_loads(r).get("DayCurves").get("Datasets")[0].get("Data")
# Inverter groups readings for every 10 minutes. Reading for 12:00 are what's accumulated during 11:50-12:00
readings_count_this_hour = now.minute // 10 + 1
for day_data in data:
parsed_date = datetime.strptime(day_data.get("Timestamp"), "%Y-%m-%d").date()
offset = day_data.get("Offset")
if (parsed_date == now.date()):
# Getting only readings for current hour, which are the last n ones in the list
return round((sum(day_data.get("Data")[-readings_count_this_hour:])/6000), 2)
output = get_readings_for_current_hour()
print(output)
And every 30 seconds (default polling interval) I just keep getting the error:
Logger : homeassistant.components.command_line.utils
Source : components/command_line/utils.py:54
integration : Command Line ( documentation , problems )
For the first time it happened : 12:58:12 ( 24 cases )
Last recorded : 13:09:42
Command failed (with return code 2): python3 /homeassistant/pyscript/solar_panels_live_hour_report_ha.py
When I run the script from the terminal it works just fine. But the command line sensor keeps returning that same error whatever I do.
What I’ve tried so far:
- chmod 777 on the script
- chown hassio on the script
- running with sudo
- running with python
- editing script to just
print("23")
also as suggested in one thread I’ve tried creating these sensors and both worked just fine:
command_line:
- sensor:
name: CPU Temperature
command: "cat /sys/class/thermal/thermal_zone0/temp"
unit_of_measurement: "°C"
value_template: '{{ value | multiply(0.001) }}'
- sensor:
command: python3 -c "import requests; print(requests.get('https://pypi.python.org/pypi/homeassistant/json').json()['info']['version'])"
name: HA release
There are multiple threads with the same issue, but I’m either doing something wrong, or the solutions that worked for others don’t work for me.
It looks like there’s some issue with permissions, but I can’t figure out what sort of permissions I need to give to whom for this to start working