Python scripts permissions

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

GDIFF = (30/1.8)
VR0  = 0.223
G0  = 2
I  = (1.24 / 10000)

def conv_voltageto_temperature_c(voltage):
  Rpt1000 = (voltage/GDIFF+VR0)/I/G0
  temp = (Rpt1000-1000)/3.85
  temp = int(temp)
  return temp

te_voltage = float(hass.sensor.te_volts)
te_value = conv_voltageto_temperature_c(te_voltage)
temperature = str(te_value)
temperature

Yes, a python_script has access to entity states, but the code is doing it incorrectly. It should be:

te_voltage = float(hass.states.get('sensor.te_volts').state)

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:

te_volts = hass.states.get('sensor.te_volts')
if te_volts:
    te_temp = conv_voltageto_temperature_c(float(te_volts.state))
else:
    te_temp = "unknown"
hass.states.set('sensor.te_temp', te_temp)

You don’t have to worry about converting the float to a str because hass.states.set() will do that automatically.

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:

- trigger:
  - platform: state
    entity_id: sensor.te_volts
  - platform: homeassistant
    event: start
  action:
  - service: python_script.temp

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?

Entities and the State Machine are two different things. Entities write their state to the State Machine. The State Machine is basically a representation of the entities in the system. (See Core Architecture | Home Assistant Developer Docs (home-assistant.io) for more info.)

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.)