Dallas Component

This class allows using Dallas (DS18b20) devices with esphomelib. You first have to create a hub or bus where all sensors are connected to (DallasComponent).

Next, use get_sensor_by_address() and get_sensor_by_index() to get individual sensors. You can get the addresses of dallas sensors by observing the log output at startup time.

Example Usage

// Bus setup
auto *dallas = App.make_dallas_component(15);

// By address
App.register_sensor(dallas->get_sensor_by_address("Ambient Temperature", 0xfe0000031f1eaf29));
// By index
App.register_sensor(dallas->get_sensor_by_index("Ambient Temperature", 0));

See Application::make_dallas_component() and Application::register_sensor().

API Reference

class DallasComponent : public PollingComponent

Hub for dealing with dallas temperature sensor.

Uses a OneWire interface.

Get the individual sensors with get_sensor_by_address or get_sensor_by_index.

Public Functions

DallasComponent(ESPOneWire *one_wire, uint32_t update_interval)

Construct the DallasComponent hub with the given OneWire instance pointer.

DallasTemperatureSensor *get_sensor_by_address(const std::string &name, uint64_t address, uint8_t resolution = 12)

Get a DallasTemperatureSensor by address.

Return
A pointer to a DallasTemperatureSensor, use this to setup MQTT.
Parameters
  • address: 64-bit unsigned address for this sensor. Check debug logs for getting this.
  • resolution: The resolution for this sensor, 8-12.

DallasTemperatureSensor *get_sensor_by_index(const std::string &name, uint8_t index, uint8_t resolution = 12)

Get a DallasTemperatureSensor by index.

Note: It is recommended to use sensors by address to avoid mixing up sensor values if one sensor can’t be found (and therefore receives an incorrect index).

Return
A pointer to a DallasTemperatureSensor, use this to setup MQTT.
Parameters
  • index: The index of this sensor, starts with 0.
  • resolution: The resolution for this sensor, 8-12.

void set_one_wire(ESPOneWire *one_wire)

Get the ESPOneWire instance used for this hub.

Manually set the ESPOneWire instance used for this hub.

void setup()

Set up individual sensors and update intervals.

void dump_config()
float get_setup_priority() const

HARDWARE_LATE setup priority.

void update()
ESPOneWire *get_one_wire() const

Protected Attributes

ESPOneWire *one_wire_
std::vector<DallasTemperatureSensor *> sensors_
std::vector<uint64_t> found_sensors_
class DallasTemperatureSensor : public sensor::EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C, DallasComponent>

Internal class that helps us create multiple sensors for one Dallas hub.

Public Functions

DallasTemperatureSensor(const std::string &name, uint64_t address, uint8_t resolution, DallasComponent *parent)

Construct the temperature sensor with the given address.

Parameters
  • address: 64-bit unsigned address of the temperature sensor. Can be 0 to indicate using index.
  • resolution: Resolution used for this sensor. Usually 8-12.
  • update_interval: The interval in ms the sensor should be checked.

uint8_t *get_address8()

Helper to get a pointer to the address as uint8_t.

const std::string &get_address_name()

Helper to create (and cache) the name for this sensor. For example “0xfe0000031f1eaf29”.

uint64_t get_address() const

Get the 64-bit unsigned address for this sensor.

void set_address(uint64_t address)

Set the 64-bit unsigned address for this sensor.

optional<uint8_t> get_index() const

Get the index of this sensor. (0 if using address.)

void set_index(uint8_t index)

Set the index of this sensor. If using index, address will be set after setup.

uint8_t get_resolution() const

Get the set resolution for this sensor.

void set_resolution(uint8_t resolution)

Set the resolution for this sensor.

uint16_t millis_to_wait_for_conversion_() const

Get the number of milliseconds we have to wait for the conversion phase.

bool setup_sensor_()
bool read_scratch_pad_()
bool check_scratch_pad_()
float get_temp_c()
std::string unique_id()

A unique ID for this sensor, empty for no unique id.

See unique ID requirements: https://developers.home-assistant.io/docs/en/entity_registry_index.html#unique-id-requirements

Return
The unique id as a string.

Protected Attributes

uint64_t address_
optional<uint8_t> index_
uint8_t resolution_
std::string address_name_
uint8_t scratch_pad_[9] = {0,}