Hi there
Im experiencing a few issues with getting a custom sensor to work properly with Hass.
The hardware setup: Rpi (HASS) <—Restful API— RPi (OSMC)
- Main Raspberry Pi setup with HASS, this is where I want to have all the sensor data.
- Another Raspberry Pi (primarily as a media center) with 1-wire sensor(DS18b20).
- Data is pushed from RPi (OSMC) to RPi Hass via Restful API (python code)
On the Hass, I have created a custom sensor using example from:
https://home-assistant.io/developers/platform_example_sensor/
Modded the example a bit example.py:
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import config_validation as cv
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
import logging
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'example_sensor'
DEFAULT_NAME = 'Example Sensor'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""
add_devices([ExampleSensor(config[DOMAIN].get(CONF_NAME, DEFAULT_NAME))])
class ExampleSensor(Entity):
"""Representation of a Sensor."""
def __init__(self,name):
"""Initialize the sensor."""
self._state = None
self._name = name
@property
def name(self):
"""Return the name of the sensor."""
return 'Example Temperature'
@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 TEMP_CELSIUS
# 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 = 23
I have removed the update function as I dont want Hass to fetch the data. Im pushing the data from the other RPi(OSMC) to Hass.
For the HASS configuration (sensors.yaml):
- platform: example name: OLED Test
Im trying to pass the Name which I wish to show for the sensor in the Hass UI. The sensor does show up in HASS, but the name is not reflected.
On the Hass UI, I was hoping that the senor data would be shown as a graph, but it shows up as below(bits?), which is not really useful:
On the other RPi (media center), I have the below code to push data to HASS via the Restful API:
def post_hass(temperature):
url = 'http://192.168.0.14:8123/api/'
endpoint = "states/sensor.example_temperature" ##this is entity_id
payload={'state': temperature}
headers = {'x-ha-access': 'password', 'content-type': 'application/json'}
response = post(url+endpoint, headers=headers, json=payload)
if (response.status_code != 200):
print("HASS Publish Failed".upper())
print(json.dumps(json.loads(response.text), indent=2))
Is there something that Im doing incorrectly? (i bet i am )
So the issues i have:
- The Name on the Hass UI is incorrectly shown
- The sensor data is not shown as a graph
Any help would greatly be appreciated.