Home Assistant : Q&A, Tips & Tricks, Your Configs

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093
I did, the big difference is the working Temperatures is from ZHA and the non working ones are from a custom integration I wrote myself.

The ZHA one's are part of a device and my own is only entities, and I am having a hell of a time trying to register a device in python at the moment.
Share the code, sure with someone here might be able to help you with a fix.

You might just need to assign a class to the entity.
 

TedLasso

Expert Member
Joined
Feb 23, 2016
Messages
3,235
Anyone using the Xiaomi miio integration for Philips bulbs? I have two bulbs which have stopped working after the latest HA update.
Are they Hue bulbs? Have to say I have loads of Hue bulbs and strip lights (with their bridge) and it's been super reliable for the 4 years have had them. The one integration I still love , is a IFTTT routine setup with Alexa so that if timer is reached, all the bulbs flash. I don't know when I last logged into IFTTT but it still works today.

FWIW, all my hue bulbs still working with 2021.12.1
 

Veroland

Executive Member
Joined
Aug 24, 2005
Messages
6,132
Share the code, sure with someone here might be able to help you with a fix.

You might just need to assign a class to the entity.
This is not my finest code but here goes:

Code:
from homeassistant.const import TEMP_CELSIUS, PRESSURE_HPA, VOLUME_MILLILITERS, ILLUMINANCE
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import device_registry as dr

import psycopg2

def setup_platform(hass, config, add_entities, discovery_info=None):
    add_entities([TempSensor(), HumiditySensor(), PressureSensor(), RainSensor(), LightSensor()])

class TempSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def device_info(self):
        return {
            "identifiers": {
                # Serial numbers are unique identifiers within a specific domain
                ("sensor", self.unique_id)
            },
            "name": self.name,
            "manufacturer": "",
            "model": "",
            "sw_version": ""
        }

    @property
    def unique_id(self):
        """Return the entity id of the sensor."""
        return 'sensor.outside_temperature'


    @property
    def name(self):
        """Return the name of the sensor."""
        return '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.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select temperature_external as DS18b20 from weather.temperature WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()
        tempFloat = row[0]

        self._state = "{0:.2f}".format(tempFloat)

class HumiditySensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Humidity'

    @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 '%'

    @property
    def icon(self):
        return 'mdi:water-percent'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select humidity from weather.humidity WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()

        self._state = "{0:.1f}".format(row[0])
        # self._state = 0

class PressureSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Air Pressure'

    @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 PRESSURE_HPA

    @property
    def icon(self):
        return 'mdi:gauge'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select pressure from weather.pressure WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()

        self._state = "{0:.1f}".format(row[0])
        # self._state = 0


class RainSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Rainfall Today'

    @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 VOLUME_MILLILITERS

    @property
    def icon(self):
        return 'mdi:water'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select rain as rainfall from weather.rain WHERE read_time > CURRENT_DATE")
        rainVolume = 0
        for row in cur.fetchall():
            rainVolume = rainVolume + row[0]

        rainf = float(int(rainVolume) * 0.2794)

        self._state = "%.2f" % round(rainf,2)
        # self._state = 0

class LightSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Light 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 ''

    @property
    def icon(self):
        return 'mdi:lightbulb'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select light from weather.light order by read_time DESC")
        row = cur.fetchone()

        self._state = row[0]
 

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093
This is not my finest code but here goes:

Code:
from homeassistant.const import TEMP_CELSIUS, PRESSURE_HPA, VOLUME_MILLILITERS, ILLUMINANCE
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import device_registry as dr

import psycopg2

def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities([TempSensor(), HumiditySensor(), PressureSensor(), RainSensor(), LightSensor()])

class TempSensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def device_info(self):
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
("sensor", self.unique_id)
},
"name": self.name,
"manufacturer": "",
"model": "",
"sw_version": ""
}

@property
def unique_id(self):
"""Return the entity id of the sensor."""
return 'sensor.outside_temperature'


@property
def name(self):
"""Return the name of the sensor."""
return '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.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()

# Current Conditions data
cur.execute(
"select temperature_external as DS18b20 from weather.temperature WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()
tempFloat = row[0]

self._state = "{0:.2f}".format(tempFloat)

class HumiditySensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return 'Humidity'

@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 '%'

@property
def icon(self):
return 'mdi:water-percent'

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()

# Current Conditions data
cur.execute(
"select humidity from weather.humidity WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()

self._state = "{0:.1f}".format(row[0])
# self._state = 0

class PressureSensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return 'Air Pressure'

@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 PRESSURE_HPA

@property
def icon(self):
return 'mdi:gauge'

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()

# Current Conditions data
cur.execute(
"select pressure from weather.pressure WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()

self._state = "{0:.1f}".format(row[0])
# self._state = 0


class RainSensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return 'Rainfall Today'

@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 VOLUME_MILLILITERS

@property
def icon(self):
return 'mdi:water'

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()

# Current Conditions data
cur.execute(
"select rain as rainfall from weather.rain WHERE read_time > CURRENT_DATE")
rainVolume = 0
for row in cur.fetchall():
rainVolume = rainVolume + row[0]

rainf = float(int(rainVolume) * 0.2794)

self._state = "%.2f" % round(rainf,2)
# self._state = 0

class LightSensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return 'Light 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 ''

@property
def icon(self):
return 'mdi:lightbulb'

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()

# Current Conditions data
cur.execute(
"select light from weather.light order by read_time DESC")
row = cur.fetchone()

self._state = row[0]
That is above my knowledge..... hopefully someone can help.

Think somewhere you might need to do this: https://www.home-assistant.io/docs/configuration/customizing-devices/#device-class in order to get it to show as temp sensor. You can do it from device customization.
 

Veroland

Executive Member
Joined
Aug 24, 2005
Messages
6,132

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093
In my code the device class is set to sensor, HA picks this up from the fact that my file name is sensor.py.

The sensors works correctly in all other ways I can see.
View attachment 1212444
Entity type would be sensor. Device class would be temperature, humidity or battery.

 
Last edited:

Veroland

Executive Member
Joined
Aug 24, 2005
Messages
6,132
Entity type would be sensor. Device class would be temperatuur, humidity or battery.

You sir, are a bloody genius!

I got so bugged down in the details I forgot about the bigger picture. Just added the device class to my entity and it works

Code:
    @property
    def device_class(self):
        return DEVICE_CLASS_TEMPERATURE;
 

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093
You sir, are a bloody genius!

I got so bugged down in the details I forgot about the bigger picture. Just added the device class to my entity and it works

Code:
 @property
def device_class(self):
return DEVICE_CLASS_TEMPERATURE;
Pleasure! Glad I could point you in the right direction
 

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093
This is not my finest code but here goes:

Code:
from homeassistant.const import TEMP_CELSIUS, PRESSURE_HPA, VOLUME_MILLILITERS, ILLUMINANCE
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import device_registry as dr

import psycopg2

def setup_platform(hass, config, add_entities, discovery_info=None):
    add_entities([TempSensor(), HumiditySensor(), PressureSensor(), RainSensor(), LightSensor()])

class TempSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def device_info(self):
        return {
            "identifiers": {
                # Serial numbers are unique identifiers within a specific domain
                ("sensor", self.unique_id)
            },
            "name": self.name,
            "manufacturer": "",
            "model": "",
            "sw_version": ""
        }

    @property
    def unique_id(self):
        """Return the entity id of the sensor."""
        return 'sensor.outside_temperature'


    @property
    def name(self):
        """Return the name of the sensor."""
        return '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.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select temperature_external as DS18b20 from weather.temperature WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()
        tempFloat = row[0]

        self._state = "{0:.2f}".format(tempFloat)

class HumiditySensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Humidity'

    @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 '%'

    @property
    def icon(self):
        return 'mdi:water-percent'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select humidity from weather.humidity WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()

        self._state = "{0:.1f}".format(row[0])
        # self._state = 0

class PressureSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Air Pressure'

    @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 PRESSURE_HPA

    @property
    def icon(self):
        return 'mdi:gauge'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select pressure from weather.pressure WHERE read_time > CURRENT_DATE order by read_time DESC")
        row = cur.fetchone()

        self._state = "{0:.1f}".format(row[0])
        # self._state = 0


class RainSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Rainfall Today'

    @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 VOLUME_MILLILITERS

    @property
    def icon(self):
        return 'mdi:water'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select rain as rainfall from weather.rain WHERE read_time > CURRENT_DATE")
        rainVolume = 0
        for row in cur.fetchall():
            rainVolume = rainVolume + row[0]

        rainf = float(int(rainVolume) * 0.2794)

        self._state = "%.2f" % round(rainf,2)
        # self._state = 0

class LightSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self):
        """Initialize the sensor."""
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return 'Light 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 ''

    @property
    def icon(self):
        return 'mdi:lightbulb'

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        conn = psycopg2.connect("****Connection Settings*****")
        cur = conn.cursor()

        # Current Conditions data
        cur.execute(
            "select light from weather.light order by read_time DESC")
        row = cur.fetchone()

        self._state = row[0]
Looking at the code now, did you built some sort of weather station ? If so mind sharing bit more info ?
 

Veroland

Executive Member
Joined
Aug 24, 2005
Messages
6,132
Looking at the code now, did you built some sort of weather station ? If so mind sharing bit more info ?

Sure, I stared playing with this before I found HA.

It essentially started with a ds18b20 and a rain gauge hooked to a Pi3.
I then expanded to a Weather:Bit which allows some onboard programming and threw them into a cheap plastic container.

The I use a Pi3 connected with Serial over USB to pull the various stats from the Weather:Bit every 5 mins.
The Pi then saves it to a Postgres DB.

After I started with HA I wrote my own integration that is called from HA and reads the db for the specific sensor and then return the value.

Recently I added saving the data to InfluxDb as well, as this is where my zigbee sensors for HA also gets saved, This allows me to create some funky dashboards in Grafana.

The setup:
20180828_163235.jpg

Grafana:
1639475557516.png
Rain for the last 7 days:
1639475622529.png
 

w1tw0lf

Expert Member
Joined
Sep 29, 2009
Messages
1,093

Veroland

Executive Member
Joined
Aug 24, 2005
Messages
6,132
Awesome project.

Been looking at this one, https://www.robotics.org.za/WS-WH24CP-1?search=weather and would also want to integrate with HA. But he ultimate would be https://weatherflow.com/tempest-weather-system/ unfortunately no available local. Found one that was close by where I stay, https://tempestwx.com/map/15614/-26.1502/28.1664/13 and was using the public api to pull info into HA, but with latest update the public api that was discontinued stopped working.
They both look awesome. Mine just started out as something to do but at some point I would like to get a proper weather station going.

I was looking at feeding into Weather Underground but as my equipment is not industrial spec I cannot grantee the accuracy.
 

furpile

Expert Member
Joined
Jul 14, 2014
Messages
3,879
Best option for standalone gate access? Shelly 1 running on 12v?
I use a Sonoff SV. Runs off the 24V from the batteries for the motor (D5). Never had an issue with this one. The one on the garage door went offline a lot, because the batteries are so small on the Xtrac. Since I put it on a dedicated PSU, it has been solid as well.

For the gate, I think there is a way to use the output directly from the D5 to read the status, but I have not done this. Can also add a reed switch if needed.
 

alqassam

Expert Member
Joined
Aug 11, 2014
Messages
4,102
I use a Sonoff SV. Runs off the 24V from the batteries for the motor (D5). Never had an issue with this one. The one on the garage door went offline a lot, because the batteries are so small on the Xtrac. Since I put it on a dedicated PSU, it has been solid as well.

For the gate, I think there is a way to use the output directly from the D5 to read the status, but I have not done this. Can also add a reed switch if needed.
Also using a SV. Want an easy option for someone that doesn't have home assistant.
 

furpile

Expert Member
Joined
Jul 14, 2014
Messages
3,879
Updated my HA core to newest version, now Supervisor is missing. It shows my installation is unsupported (running in Virtualbox) because of unsupported software. According to the link it gives:

The solution​

Remove any additional software (including standalone containers) you have installed on your host and restart the Supervisor.
The supervisor logs don't tell me anything about additional software that I can see?

Anybody know what the issue is here? Is the host only the HA install?
 

furpile

Expert Member
Joined
Jul 14, 2014
Messages
3,879
Updated my HA core to newest version, now Supervisor is missing. It shows my installation is unsupported (running in Virtualbox) because of unsupported software. According to the link it gives:

The supervisor logs don't tell me anything about additional software that I can see?

Anybody know what the issue is here? Is the host only the HA install?
Updated the Home Assistant OS and now the logs show it is Deepstack running in Portainer that it is complaining about. Will need to upgrade the PC to Win10 at some point so that Deepstack can run in docker I suppose.
 
Top