Custom Sensor - Issues

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)

  1. Main Raspberry Pi setup with HASS, this is where I want to have all the sensor data.
  2. Another Raspberry Pi (primarily as a media center) with 1-wire sensor(DS18b20).
  3. 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:

image

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 :stuck_out_tongue: )

So the issues i have:

  1. The Name on the Hass UI is incorrectly shown
  2. The sensor data is not shown as a graph

Any help would greatly be appreciated.

The multi-band means HA is getting a bunch of text states instead of float values?

print(json.dumps(json.loads(float(response.text)), indent=2))

I’ve no idea if the above will work, but worth a go.

The print statement is only if there is an error submitting data to HASS.

I’m not familiar with going this route, but in normal HA, the graphs are based on the unit_of_measure. So that unit of measure property might be the cause. What does TEMP_CELSIUS return?

Yes, just as bit-river said too, Your dictionary from the payload may treat the temperature as a string.

this may help:

def state(self):
    """Return the state of the sensor."""
    try:
        x = float(self._state)
    except ValueError:
        x = 0.
    return x