I’m trying to set up a python script to run as a command line sensor to do a conversion of the voltage level from an ads1115 ADC connected to an ESP8266. My python programming experience/expertise is elementary. But, I did get it working in pyscript jupyter notebook before and I get all green check marks on reloading python scripts, however, I get the error below from the log file. I thought python scripts had permissions to hass objects and entities. So why would I get this error? What did I miss?(python code below)
2022-07-05 01:14:57 ERROR (SyncWorker_3) [homeassistant.components.python_script.temp.py] Error executing script: Not allowed to access HomeAssistant.sensor
But this python_script really doesn’t do anything. If you want it to create a sensor from the resulting temperature, you’d have to add a line at the end to write to the HA State Machine. E.g.,:
hass.states.set('sensor.te_temp', te_value)
UPDATE:
Actually, you’ll want the code to properly handle the case where the sensor’s state doesn’t exist (maybe the script runs before the sensor first updates or something.) So:
Also, you may have already figured this out, but you’ll want to run this script every time sensor.te_volts updates, and possibly once at startup, which you could do with an automation like this:
Thank you for your response. I will try this out as soon as I get back to it. My only question for now is does hass.states.set() automatically create a new sensor entity for hass?
But an entry can be added to the State Machine without a corresponding entity existing. (E.g., see the HTTP Sensor.) So, when this script calls hass.states.set(), it is creating (or updating) an entry in the State Machine. It is not creating an entity.
EDIT:
The terminology can be a bit confusing. Instead of “entity”, maybe I should use the term “device”.
Calling hass.states.set() is kind of like using the “Set State” feature at the top of the STATES tab on the Developer Tools page. You can create an “entity” in the State Machine, although there is no “device” backing it up. Or you can change the state of an entity that does exist, and does have a device backing it up, but your change is only to the entry in the State Machine, and is probably temporary because it will get overwritten the next time the corresponding device updates (and rewrites its state to the State Machine.)