Hi, I am clearly missing something. I can’t seem to find a good example for the @property for a SensorEntity of a Unix timestamp. The data coming in from an API is just an INT. Bear in mind I’m a copy/paste pleb, and I am borrowing someone else’s code. It is working partially now, and counts the seconds since the new data comes in.
How do I get it to display ISO 8601 or human readable format?
its for a HACs Integration. It currently displays the correct secondary information as a Sec/Min since last changed.
SensorEntity in Question
class SomeCodeLastTimestampSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Last Timestamp"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-last-{self.config_entry.entry_id}"
@property
def native_value(self):
"""Return the native value of the sensor."""
return (self.coordinator.data.get("last_reading_timestamp"))
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:clock-outline"
The incoming timestamp is formatted as such.
1646578980
I have tried the following, and I usually try to add into the last lines of the code block.
AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
@property
def native_unit_of_measurement(self) -> str:
"""Return the native unit of measurement."""
return datetime.datetime
Timestamp gives an INT found error(it complains of no TZ too), which I guess I should expect compared to ISO T time
@property
def device_class(self) -> str:
"""Return the device class, if any."""
return SensorDeviceClass.TIMESTAMP
fails to setup
@property
def device_class(self) -> str:
"""Return the device class, if any."""
return SensorDeviceClass.LAST_RESET
@property
def native_value(self) -> StateType | datetime:
"""Return the state."""
return super().native_value
@property
def native_value(self) -> StateType | datetime:
"""Return the state."""
return getattr(self.SomeCode, self.sensor_attribute)
Full sensor.py
"""Sensor platform for SomeCode."""
from datetime import date, datetime, timedelta, timezone
from typing import Any, Final, cast, final
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
StateType
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import ENERGY_KILO_WATT_HOUR, DEVICE_CLASS_ENERGY, POWER_KILO_WATT, DEVICE_CLASS_POWER, DEVICE_CLASS_MONETARY, CURRENCY_DOLLAR
from .const import NAME, DOMAIN, ICON, CONF_DEVICE_ID, ATTRIBUTION
async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = [
SomeCodeTotalConsumptionSensor(coordinator, entry),
SomeCodeLiveConsumptionSensor(coordinator, entry),
SomeCodeTotalCostSensor(coordinator, entry),
SomeCodeLastTimestampSensor(coordinator, entry),
SomeCodeTariffPeriodSensor(coordinator, entry),
]
async_add_devices(entities)
class SomeCodeSensor(CoordinatorEntity):
"""SomeCode Sensor class."""
def __init__(self, coordinator, config_entry):
super().__init__(coordinator)
self.config_entry = config_entry
@property
def native_unit_of_measurement(self) -> str:
"""Return the native unit of measurement."""
return None
@property
def device_class(self) -> str:
"""Return the device class."""
return None
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self.config_entry.entry_id)},
"name": NAME,
"model": self.config_entry.data[CONF_DEVICE_ID],
"manufacturer": NAME,
}
@property
def extra_state_attributes(self):
"""Return the extra state attributes."""
return {
"attribution": ATTRIBUTION,
"id": self.config_entry.data[CONF_DEVICE_ID],
"integration": DOMAIN,
}
@property
def icon(self):
"""Return the icon of the sensor."""
return ICON
class SomeCodeTotalConsumptionSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Total Consumption"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-total-{self.config_entry.entry_id}"
@property
def state_class(self) -> str:
"""Return the state class."""
return SensorStateClass.TOTAL_INCREASING
@property
def native_value(self):
"""Return the native value of the sensor."""
return self.coordinator.data.get("total_watt_hours") / 1000
@property
def device_class(self) -> str:
"""Return the device class."""
return DEVICE_CLASS_ENERGY
@property
def native_unit_of_measurement(self) -> str:
"""Return the native unit of measurement."""
return ENERGY_KILO_WATT_HOUR
class SomeCodeLiveConsumptionSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Live Consumption"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-live-{self.config_entry.entry_id}"
@property
def device_class(self) -> str:
"""Return the device class."""
return SensorDeviceClass.POWER
@property
def state_class(self) -> str:
"""Return the state class."""
return SensorStateClass.MEASUREMENT
@property
def native_value(self):
"""Return the native value of the sensor."""
return (self.coordinator.data.get("last_reading_watt_hours") * 60) / 1000
@property
def native_unit_of_measurement(self) -> str:
"""Return the native unit of measurement."""
return POWER_KILO_WATT
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:lightning-bolt-outline"
class SomeCodeTotalCostSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Total Cost"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-cost-{self.config_entry.entry_id}"
@property
def state_class(self) -> str:
"""Return the state class."""
return SensorStateClass.TOTAL_INCREASING
@property
def native_value(self):
"""Return the native value of the sensor."""
return (self.coordinator.data.get("total_cost"))
@property
def device_class(self) -> str:
"""Return the device class."""
return DEVICE_CLASS_MONETARY
@property
def native_unit_of_measurement(self) -> str:
"""Return the native unit of measurement."""
return CURRENCY_DOLLAR
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:currency-usd"
class SomeCodeTariffPeriodSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Tariff Period"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-peak-{self.config_entry.entry_id}"
@property
def native_value(self):
"""Return the native value of the sensor."""
return (self.coordinator.data.get("is_peak"))
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:progress-clock"
class SomeCodeLastTimestampSensor(SomeCodeSensor, SensorEntity):
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "SomeCode Last Timestamp"
@property
def unique_id(self) -> str:
"""Return the unique id."""
return f"SomeCode-last-{self.config_entry.entry_id}"
@property
def native_value(self):
"""Return the native value of the sensor."""
return (self.coordinator.data.get("last_reading_timestamp"))
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:clock-outline"