Show/add new ESPhome device in Node-red

i try to show/add a new ESPhome Device in Node-Red.

what i do already:

  1. i create a New ESphome Device (D1-Mini with DHT22 sensor), the Devoce work, ican see is online, the log show me all is ok and in HomeAssistant i see the Device too)

But in the settings I can't select the hardware. It doesn't exist there.
I guess I forgot some little thing.
I have already restarted Node-Red on the command line. I use all the software on a Raspberry Pi 4 Docker system

best regards

Welcome to the forums @selfmade.

How exactly are you "adding" the device in Node RED.
some node like node-red-contrib-esphome

or some Home Assistant node.

If the latter, you will need to ask in the HA forums, as very little here use Home Assistant

i think now i take a big understanding misstake, because i configure my ESPhome device only to cominicate over wifi ... mean i get temp or humidy values only over wifi and can show it in the log-file.

Maybe i have to configure the ESPhome-device first to comunicate over Mqtt.
node-red-contrib-esphome i installed already and yes i used this because i think ... i add only node-red-esphome-device and then i get also the values from that device... but i think this way is wrong.

maybe i have to configure first the esp-home device to communicate over mqtt...
thats right?
is there any howtow step by step where i can show what i have to do...?

Thats my esphome-device Programming-code

  name: grobot-anzucht-d1mini
  friendly_name: grobot_anzucht_D1Mini

  board: d1_mini

# Enable logging

# Enable Home Assistant API
    key: "9EowYIpEmzeO8cMYFgAefpsvYs0osg37JgdCClE6OYI="

  password: "77b42abb7619f38e3ad9697c5cfbd292"

    # Set this to the IP of the ESP
    static_ip:    # Set this to the IP address of the router. Often ends with .1
    # The subnet of the network. works for most home networks.
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Grobot-Anzucht-D1Mini"
    password: "YhC00Ywz0CAh"


  # Example configuration entry DHT22 Sensor
  - platform: dht
    pin: D0
      name: "Temperatur Anzucht-Box"
      name: "Luftfeuchtigkeit Anzucht-Box"
    update_interval: 10s

best regards

I know nothing about ESPHome (some here who will of course)
But if you use MQTT - Node RED will be all over it without a fuss

That is by far the best way to work with ESPhome. I use that for all my home-grown sensor platforms.

You really won't need that if you use MQTT. Just decide on a pattern for your MQTT to/from ESPhome devices. Then you can auto-discover them.

add to yaml file

broker: !secret mqtt_host
discovery: false

if you dont use with home assist, remove api from yaml file

open mqtt explorer and see the publish for dht sensor the use it in nodered

I use a set of standard include files so that all of the standard bits are exactly that and only the platform specific things and the name/ip address change.

Yes, thanks, I'm slowly getting the hang of it.
I think I first have to create a mqtt entry for the ESP (server, user and password), I think then a sensor entry.... right? and then when the ESP is finished, then I only have to select and configure the mqtt-node in node-red? something like that, right?
I think that I have to enter in the ESP what it sends... for example... temperature and humidity... he mqtt has to get this data somehow

my esp D1 Mini have this code for mqtt

  name: grobot-d1mini
  friendly_name: ESP-D1Mini-Sensors

  board: d1_mini

# Enable logging

# Enable Home Assistant API
#  encryption:
#    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

  #  DHT22 Sensor
  - platform: dht
    pin: D0
      name: "Temperatur"
      name: "Luftfeuchtigkeit"
    update_interval: 10s
  broker: !secret mqtt_host
  port: !secret mqtt_port
  username: !secret mqtt_username
  password: !secret mqtt_password

i'm not sure is the entry "sensor:" right... because that was only a sample to get the values from the espand his DHT22 on the serial.

yes i use home-assistant...

later I would like to control all data and controls via a home-assistant dashboard (I could then do this at home from a tablet or from the Internet) and/or via a mobile app from my mobile phone.
i was not sure... i have to disable api: or not :frowning:

Here is my MQTT include file

# Configuration for the MQTT Broker.
# NB: This configuration does not use Home Assistant (HA). It uses TotallyInformation's standard MQTT Schema for ESP devices.
# The configuration variables must be set in the master YAML file.

mqtt: # Home MQTT Broker

  broker: $mqtt_broker 
  username: $mqtt_username
  password: $mqtt_password
  topic_prefix: $mqtt_prefix
  id: mqtt_client

  log_topic: # no log to MQTT, it isn't needed and isn't that useful unless you subscribe something to it.

    topic: $mqtt_prefix
    payload: Online
    retain: true

    topic: $mqtt_prefix
    payload: Offline
    retain: true

    topic: $mqtt_prefix
    payload: Shutdown
    retain: true

  discovery: false # No HA discovery, we aren't using HA

#--- End of mqtt ---#

It is included in all of my ESPhome driven devices.

I don't use Home Assistant.

This is a typical platform configuration - for a d1 mini in this case:

# D1 Mini Configuration for D1M05 (D1 Mini)

# Pins (=GPIO)| Input? | Output? | Comments
  # D0 = 16   | Y      | Y       | High @ boot, Wake, I/P: No Interrupt, O/P: No PWM or I2C
  # D1 = 5    | Y      | Y       | I2C SCL
  # D2 = 4    | Y      | Y       | I2C SDA
  # D3 = 0    | Y      | Y       | 
  # D4 = 2    | Y      | Y       | Onboard LED
  # D5 = 14   | Y      | Y       | 
  # D6 = 12   | Y      | Y       | 
  # D7 = 13   | Y      | Y       | 
  # D8 = 15   | Y      | Y       | 
  # RX = 3    | Y      | Y       | 
  # TX = 1    |        | Y       | 
  # A0 = ADC0 | Y      |         | 

substitutions: # All have to be strings, the substitution process will convert them to the correct types
  devicename: d1m02
  upper_devicename: D1M02
  device_location: Rear Hall
  device_description: Rear Hall custom sensor platform using ESPhome firmware
  mqtt_prefix: ESP/D1M02 #$mqtt_root/$devicename # Argh! combined substitutions don't work!
  esp_board: d1_mini
  last_update: '"2022-12-12 10:47:12"'
  mqtt_root: ESP
  mqtt_broker: !secret mqtt_broker 
  mqtt_username: !secret mqtt_username
  mqtt_password: !secret mqtt_password
  wifi_power_save_mode: light # none (default for esp8266), light (default for esp32), high
  wifi_fast_connect: "true"
  wifi_ssid: !secret WiFi_SSID
  wifi_password: !secret WiFi_password
  wifi_domain: !secret WiFi_domain
  wifi_gateway: !secret WiFi_gateway
  wifi_subnet: !secret WiFi_subnet
  wifi_dns1: !secret WiFi_DNS1
  wifi_ap_password: !secret WiFi_AP_Password

  web_auth_username: !secret Web_Auth_Username
  web_auth_password: !secret Web_Auth_Password

  publish_interval: 50s
  log_level: info # none, error, warn, info, debug, verbose
  #esp_platform: ESP8266
  sun_latitude: !secret sun_latitude
  sun_longitude: !secret sun_longitude 
# --- End of substitutions: --- #  
<<: !include includes/mqtt.yaml # For some reason, the config checker doesn't like this as a package
packages: # These allow merging whereas global includes do not
  common: !include includes/common.yaml
  logger: !include includes/logger.yaml
  wifi: !include includes/wifi.yaml
  common_sensors: !include includes/common-sensors.yaml # Includes number, text and binary
  common_switches: !include includes/common-switches.yaml
  common_intervals: !include includes/common-intervals.yaml
# --- End of packages: --- #

# Basic configuration for ESP8266 based boards.
# Board names:
  board: $esp_board

    number: D4 # GPIO-2
    inverted: true

i2c: #
  frequency: 400000.0 # default 50000.0, can be up to 800k - try 400k on a d1 mini?

sensor: # NB: See also common_sensors

  # Temperature & Humidity
  #- <<: !include includes/sensors/th_dht.yaml
  # DHT based Temperature+Humidity Sensors -
  - platform: dht
    pin: D5
    model: AM2302
      name: "Temperature"
      id: temperature
      state_topic: $mqtt_prefix/DHT22/Temperature
      name: "Humidity"
      id: humidity
      state_topic: $mqtt_prefix/DHT22/Humidity
      accuracy_decimals: 1
      # filters:
      # - offset: 4.2
  # Ambient light intensity
  - <<: !include includes/sensors/light_bh1750.yaml

  - platform: gpio # 1-wire sound sensor
    pin: D3
    name: "Sound"
    id: sound
    state_topic: $mqtt_prefix/Sound/Sound

  - platform: template # Template text sensor wifi channel
    id: wifi_channel
    lambda: |-
      // --- ESP8266 version ---
      std::string out;
      char buffer[64];
      sprintf(buffer, "%u", wifi_get_channel());
      return out;
      // --- ESP32 version ---
      //wifi_ap_record_t wifidata;
      //std::string out;
      //if (wifidata.primary != 0) {
        //char buffer[64];
        //sprintf(buffer, "%u", wifidata.primary);
      //return out;
1 Like

put mqtt after api
sensor is last
if you use home assist, you need api enabled.

you can type in cmd windows to see debug logs
esphome logs filename.yaml

Wow thank you very much. I see that I still lack a lot of knowledge. This is a very, very good example for me to look for all the commands and study them more closely. That helps me a lot... but now I have to sit down over the weekend and study it carefully in order to understand it.
this is how I understand the API if anyone is using IOBroker or Openhab then you don't need the API statement...

you write in code

<<: !include includes/mqtt.yaml # 

What mean "<<: " ?
Do I then have to remove it again when uploading to the ESP?

is it possible to show a small example please?

this example is for pzem004T.yaml (i dont have dht sensor, i have an energy meter)

open cmd in windows and to execute command you need to be in esp home directory. (is not working if you are not in that directory)

Microsoft Windows [Version 10.0.19045.2846]
(c) Microsoft Corporation. All rights reserved.

C:\Users\pn>cd C:\Users\pn\esphome-jk-bms

C:\Users\pn\esphome-jk-bms>esphome logs pzem004T.yaml
←[32mINFO Reading configuration pzem004T.yaml...←[0m
←[32mINFO Starting log output from pzem004t/debug←[0m
←[32mINFO Connected to MQTT broker!←[0m
[14:23:42][D][pzemac:049]: PZEM AC: V=227.3 V, I=0.175 A, P=1.0 W, E=606588.0 Wh, F=50.0 Hz, PF=0.03
[14:23:45][I][mqtt:216]: MQTT Connected!
[14:23:46][D][pzemac:049]: PZEM AC: V=227.2 V, I=0.176 A, P=1.0 W, E=606588.0 Wh, F=50.0 Hz, PF=0.03
[14:23:46][D][sensor:126]: 'Voltage': Sending state 227.20000 V with 1 decimals of accuracy
[14:23:47][D][sensor:126]: 'Current': Sending state 0.17600 A with 3 decimals of accuracy
[14:23:47][D][sensor:126]: 'Power': Sending state 1.00000 W with 2 decimals of accuracy
[14:23:47][D][sensor:126]: 'Energy': Sending state 606588.00000 Wh with 0 decimals of accuracy
[14:23:47][D][sensor:126]: 'Frequency': Sending state 50.00000 Hz with 1 decimals of accuracy
[14:23:47][D][sensor:126]: 'Power Factor': Sending state 0.03000 with 2 decimals of accuracy
[14:23:49][D][pzemac:049]: PZEM AC: V=227.2 V, I=0.175 A, P=1.0 W, E=606588.0 Wh, F=50.0 Hz, PF=0.03
[14:23:49][D][sensor:126]: 'Voltage': Sending state 227.20000 V with 1 decimals of accuracy
[14:23:49][D][sensor:126]: 'Current': Sending state 0.17500 A with 3 decimals of accuracy
[14:23:49][D][sensor:126]: 'Power': Sending state 1.00000 W with 2 decimals of accuracy
[14:23:49][D][sensor:126]: 'Energy': Sending state 606588.00000 Wh with 0 decimals of accuracy
[14:23:49][D][sensor:126]: 'Frequency': Sending state 50.00000 Hz with 1 decimals of accuracy
[14:23:49][D][sensor:126]: 'Power Factor': Sending state 0.03000 with 2 decimals of accuracy


oooh... maybe i wrote it wrong... my HA-System and ESPhome is all running under Linux (Raspberry PI)

how do you compile yaml file under raspberry ?

yes... i used esphome (running on my raspberry-pi-HA-System)... compile the files and then the raspberry-pi esphome

It is the YAML insertion operator.

Configuration Types — ESPHome

Nope, the compile includes the external YAML for you at that point.

ESPhome uses Python so it works the same on other platforms.

Do you need this to insert other files?
I had (so my weak memory) only seen it that way
!include includes/mqtt.yaml

I am very interested in your ESPhome example above. It looks very structured.
However, some instructions are not entirely clear to me.
for example

mqtt_root: ESP

I can't find that in any documentation
Is that a special instruction?

Been a while since I did it. Not sure if it is necessary. But it works. :slight_smile:

That means that all my ESP based devices use ESP/xxxxx as the root MQTT topic. It comes in the substitutions section so that you can use mqtt_root as a variable elesewhere. But as you can see from the line a little above:

mqtt_prefix: ESP/D1M02 #$mqtt_root/$devicename # Argh! combined substitutions don't work!

Annoyingly, that's the only place you can't use it. However, a quick search of my code reveals that it doesn't look as though it gets used at all currently. :woozy_face:

I currently have around 7 devices, all using a master YAML will various includes.