Hello,
this is my first post here.
I’m using Rpi with HA installed manually (it’s easier for me this way).
I’m trying to add a new sensor through a python script and can’t do it.
I have a python script that works with the sensor individually:
import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
# - 2/3 = +/-6.144V
# - 1 = +/-4.096V
# - 2 = +/-2.048V
# - 4 = +/-1.024V
# - 8 = +/-0.512V
# - 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1
print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
print('-' * 37)
# Main loop.
while True:
# Read all the ADC channel values in a list.
values = [0]*4
for i in range(4):
# Read the specified ADC channel using the previously set gain value.
values[i] = adc.read_adc(i, gain=GAIN)
# Note you can also pass in an optional data_rate parameter that controls
# the ADC conversion time (in samples/second). Each chip has a different
# set of allowed data rate values, see datasheet Table 9 config register
# DR bit values.
#values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
# Each value will be a 12 or 16 bit signed integer value depending on the
# ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
# Print the ADC values.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
# Pause for half a second.
time.sleep(0.5)
the example works fine but I’m not sure how to chane it to get a sensor reading.
I’ve tried this for the python script:
DOMAIN = ‘O2_gas_sensors_state’
import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
# - 2/3 = +/-6.144V
# - 1 = +/-4.096V
# - 2 = +/-2.048V
# - 4 = +/-1.024V
# - 8 = +/-0.512V
# - 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1
print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
print('-' * 37)
# Main loop.
def setup(hass, config):
value = adc.read_adc(0, gain=GAIN)
hass.states.set('hello.world', str(value))
return True
and leaving my configuration.yaml the same.
I’ve reloaded the configuration and rebooted.
I don’t get a reading of the sensor, I don’t see any error in the log…
What am I doing wrong?
Thanks,
Ziv
That’s creating a sensor. Putting something in that only gets created at startup I believe. So if you check your ‘hello’ domain and look at the ‘world’ device, you will see that that domain.device has a value.
Actually, according to the docs, you need to call the python scripts in order to execute them. So if you are changing that to set a device, then you need to call the script by calling the device it points to. Check out the documentation:
Ok,
So it turns out it’s better to use the Example sensor platform as mentioned here:
I’ve set it up and it works.
Now, Ive made a copy to the file and gave it a different name: /home/homeassistant/.homeassistant/custom_components/sensor/ads_o2.py
instead of: /home/homeassistant/.homeassistant/custom_components/sensor/example.py
Of course Ive edited the new file as follows:
from homeassistant.helpers.entity import Entity
import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""
add_devices([ads_o2()])
class ads_o2(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
ADC_INPUT = 0
@property
def name(self):
"""Return the name of the sensor."""
return 'O2 Level'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%"
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = adc.read_adc(ADC_INPUT, gain=GAIN)
also, I’ve added this line in sensors.yaml next to the example one:
- platform: example
- platform: ads_o2
when I try to check the config and reload it gives me the error of:
Configuration invalid
Testing configuration at /home/homeassistant/.homeassistant
Failed config
General Errors:
- Platform not found: sensor.ads_o2
Successful config (partial)
Tried to reboot - NO GO
Tried to install that sensor’s library inside the virtual env - NO GO
I will say again, the library and sensor work great when I use them directly via python (both 2.7 & 3).
Well, I do see a bug in your code. Maybe that will fix the issue. It may not be importing properly:
from homeassistant.helpers.entity import Entity
import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""
add_devices([ads_o2()])
class ads_o2(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
# Create an ADS1115 ADC (16-bit) instance.
self._adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
ADC_INPUT = 0
@property
def name(self):
"""Return the name of the sensor."""
return 'O2 Level'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%"
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self._adc.read_adc(ADC_INPUT, gain=GAIN)
you are calling adc in update, but adc has no reference to the adc object inside your __init__ func. So it will error whenever it tries to update. I changed it to _adc and attached it to the object by using self._adc.
Probably still not working correctly. You should turn on errors in your logs so you can see why the script is failing. Looking at the script again, I noticed you have the same issue that I stated before with the other attributes of your platform. You should move the 2 attributes outside your base class to make them global because they aren’t being seen properly.
I recommend learning python so you can debug this for yourself. As you can see, I’m not always on this.
I moved GAIN and ADC_INPUT outside your init function and into the overall file. This makes them global across the all the functions.
from homeassistant.helpers.entity import Entity
import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15
GAIN = 1
ADC_INPUT = 0
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""
add_devices([ads_o2()])
class ads_o2(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
# Create an ADS1115 ADC (16-bit) instance.
self._adc = Adafruit_ADS1x15.ADS1115()
@property
def name(self):
"""Return the name of the sensor."""
return 'O2 Level'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%"
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self._adc.read_adc(ADC_INPUT, gain=GAIN)
when I added the logger component to the configuration I finally started to see things…
Turns out the script hit an error due to insufficient permissions of i2c to the homeassistant user.
Googled it up and found that running sudo addgroup homeassistant i2c
and maybe also sudo addgroup homeassistant input
and logging out or rebooting the system, did the trick
No problem! Also, as an FYI, You should use the reply button under the last persons post so they get a notification when you reply to them. I didn’t see this for a few days because I decided to check this thread to see how you were doing. The system is kinda stupid in that regard.
Do you have HA working with the ADS1115 and I2C? If so, could you post the steps and code to get it working? Been waiting for something like this for awhile now to work directly with analog sensors over I2C.