ESP32 publish payload when push a button

Hello,
I would like to program an ESP32 via ESPHome. The switch is to be processed later in Node Red. At the moment I am imagining (via MQTT) a topic that sends a payload 1. In ESPHome I have tried the following so far. MQTT connection is established.

mqtt:
  broker: 192.168.XX.XX
  username: XXXXX
  password: XXXX
  port: 1883

binary_sensor:
  - platform: gpio
    name: handstart
    pin:
      number: GPIO22
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      then:
        - mqtt.publish:
            topic: mansteuerung/input/handstart
            payload: 1

What is your question?

Does it work?

If not, please provide your log.

Hello,
im at work now so i can´t provide a log. By the way… how can i provide a log?
In HA dashboard the button change is visible. In Node Red i didn´t get the topic mansteuerung/input/handstart with payload.
Do i also need to configure the HA configuration.yaml?

In the esphome dashboard, click the button that says logs. The one that is on every device you have configured.

INFO ESPHome 2024.4.0
INFO Reading configuration /config/esphome/mansteuerung.yaml...
INFO Starting log output from 192.168.10.50 using esphome API
INFO Successfully connected to mansteuerung @ 192.168.10.50 in 0.074s
INFO Successful handshake with mansteuerung @ 192.168.10.50 in 0.108s
[15:05:25][I][app:100]: ESPHome version 2024.4.0 compiled on Sep 15 2024, 20:57:39
[15:05:25][C][wifi:580]: WiFi:
[15:05:25][C][wifi:408]:   Local MAC: 48:E7:29:B3:FB:34
[15:05:25][C][wifi:413]:   SSID: [redacted]
[15:05:25][C][wifi:416]:   IP Address: 192.168.10.50
[15:05:25][C][wifi:420]:   BSSID: [redacted]
[15:05:25][C][wifi:421]:   Hostname: 'mansteuerung'
[15:05:25][C][wifi:423]:   Signal strength: -60 dB ▂▄▆█
[15:05:25][C][wifi:427]:   Channel: 11
[15:05:25][C][wifi:428]:   Subnet: 255.255.255.0
[15:05:25][C][wifi:429]:   Gateway: 192.168.10.1
[15:05:25][C][wifi:430]:   DNS1: 192.168.10.1
[15:05:25][C][wifi:431]:   DNS2: 0.0.0.0
[15:05:25][C][logger:166]: Logger:
[15:05:25][C][logger:167]:   Level: DEBUG
[15:05:25][C][logger:169]:   Log Baud Rate: 115200
[15:05:25][C][logger:170]:   Hardware UART: UART0
[15:05:25][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'handstart'
[15:05:25][C][gpio.binary_sensor:016]:   Pin: GPIO22
[15:05:25][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'handstopp'
[15:05:25][C][gpio.binary_sensor:016]:   Pin: GPIO23
[15:05:25][C][captive_portal:088]: Captive Portal:
[15:05:25][C][mdns:115]: mDNS:
[15:05:25][C][mdns:116]:   Hostname: mansteuerung
[15:05:25][C][ota:096]: Over-The-Air Updates:
[15:05:26][C][ota:097]:   Address: mansteuerung.local:3232
[15:05:26][C][ota:100]:   Using Password.
[15:05:26][C][ota:103]:   OTA version: 2.
[15:05:26][C][api:139]: API Server:
[15:05:26][C][api:140]:   Address: mansteuerung.local:6053
[15:05:26][C][api:142]:   Using noise encryption: YES
[15:05:26][C][mqtt:138]: MQTT:
[15:05:26][C][mqtt:140]:   Server Address: 192.168.10.9:1883 (192.168.10.9)
[15:05:26][C][mqtt:141]:   Username: [redacted]
[15:05:26][C][mqtt:142]:   Client ID: [redacted]
[15:05:26][C][mqtt:144]:   Discovery prefix: 'homeassistant'
[15:05:26][C][mqtt:145]:   Discovery retain: YES
[15:05:26][C][mqtt:147]:   Topic Prefix: 'mansteuerung'
[15:05:26][C][mqtt:149]:   Log Topic: 'mansteuerung/debug'
[15:05:26][C][mqtt:152]:   Availability: 'mansteuerung/status'
[15:05:26][C][mqtt.binary_sensor:022]: MQTT Binary Sensor 'handstart':
[15:05:26][C][mqtt.binary_sensor:023]:   State Topic: 'mansteuerung/binary_sensor/handstart/state'
[15:05:26][C][mqtt.binary_sensor:022]: MQTT Binary Sensor 'handstopp':
[15:05:26][C][mqtt.binary_sensor:023]:   State Topic: 'mansteuerung/binary_sensor/handstopp/state'

So the difficulty is in your node red to mqtt communication? Is that correct?

Does other mqtt data show up in mqtt?

Another thought, node red. Why?

Mqtt? Why?

The difficulty is to get the payload and the topic, correct. The ESP32 is shown as connected in Node Red.
I Have two other mqtt devices (not self programmed) which work with node red.
I use node red because i can “handle” it. For my purposes it was pretty easy to use.

It must not be mqtt. That´s what i thought as a beginner would be easy. If you are experienced in Node Red please tell me which node to use better.

Thanks

Try this see if you get an output to the debug panel.

image

[{"id":"36a57b21f87abc0d","type":"mqtt in","z":"0a325c35fc29f44e","name":"","topic":"mansteuerung/binary_sensor/handstart/state","qos":"2","datatype":"auto-detect","broker":"601bef1.d5b981","nl":false,"rap":true,"rh":0,"inputs":0,"x":310,"y":1760,"wires":[["9171ca93b676ceb4"]]},{"id":"34675ab4d6b49563","type":"mqtt in","z":"0a325c35fc29f44e","name":"","topic":"mansteuerung/binary_sensor/handstopp/state","qos":"2","datatype":"auto-detect","broker":"601bef1.d5b981","nl":false,"rap":true,"rh":0,"inputs":0,"x":320,"y":1820,"wires":[["9171ca93b676ceb4"]]},{"id":"9171ca93b676ceb4","type":"debug","z":"0a325c35fc29f44e","name":"debug 207","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1780,"wires":[]},{"id":"601bef1.d5b981","type":"mqtt-broker","name":"Mosquitto","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

If you dont change the topic to mansteuerung/binary_sensor/handstart and mansteuerung/binary_sensor/handstopp

I will ask one question at a time to keep us focussed.

Do you hae other MQTT topics that you are successfully importing into Node Red?

Hi,
just to be sure. I (hopefully") create this “topic: mansteuerung/input/handstart” with ESPHOME. You ask me to change topic to “mansteuerung/binary_sensor/handstart”. Is that correct or is “input” missing?

I can try it after work.

Thanks

Sounds great to me.

(I have two other devices which sends and receive mqtt topics with no problems. These codes are not selfwritten.)

For this ESP32 project (my first selfwritten trial) no successful topic in Node Red.

Thanks

You haven’t answered my question.

When these other two unknown devices write data to mqtt does the result of that data show up in node red?

And in the other direction, can you write data with node red is that data sent to the unknown device

Yes i can see that values changing in a debug node. Question answered?

Hi,
that´s it. Thanks a lot.

My next step is an “mqtt out” node. When a specific battery voltage is reached then an led on the ESP32 should go on.

I´ll try by myself and ask if i can´t get it.

1 Like

I am struggling to see what your question was in the first place, as it appears it was working all along.

It didn´t work. I used this topic in Node Red " topic: mansteuerung/input/handstart". Correct was “mansteuerung/binary_sensor/handstart/state”.

Hi,
hope you can help me… the code for the button with mqtt works for me

binary_sensor:
  - platform: gpio
    name: handstart
#    id: handstartbutton
    pin:
      number: GPIO22
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      - mqtt.publish:
          topic: mansteuerung/input/handstart/state
          payload: "1"

In my dashoboard i can see when the button is pushed but i also like to switch the button from the dashboard. What do i need to change?

Thanks in advance

You will need to add a switch to the esp and move the mqtt action to the switch

switch:
  - platform: template
    name: "Template Switch"
    id: hand_switch
    turn_on_action:
      - mqtt.publish:
          topic: mansteuerung/input/handstart/state
          payload: "1"
    turn_off_action:
      - mqtt.publish:
          topic: mansteuerung/input/handstopp/state
          payload: "0"

Then have the binary sensor turn the switch on and off.

binary_sensor:
  - platform: gpio
    name: handstart
#    id: handstartbutton
    pin:
      number: GPIO22
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      then:
        - switch.turn_on: hand_switch
          

Thanks a lot. Will check tomorrow.

Hello,
this works almost great for me… in the dashboard there is a sliding switch which has two “permanent” states. I need “on” when i click with my mouse and “off” when i release the mouse button.

One other thing
I want to use a payload from one flow in another flow. As far as I know, this works with a *.global (variable).
I have tried this, but I can’t get the payload to the debug node. Where is the error?

[{"id":"5c4848b5cbcb4807","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"7927957d53784ef9","type":"mqtt in","z":"5c4848b5cbcb4807","name":"Handstart","topic":"mansteuerung/binary_sensor/handstart/state","qos":"0","datatype":"auto","broker":"d0d69da0d3c24301","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":100,"wires":[["0e1fec3129616b24"]]},{"id":"97ecb4ff335391f1","type":"inject","z":"5c4848b5cbcb4807","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":110,"y":160,"wires":[["0e1fec3129616b24"]]},{"id":"0e1fec3129616b24","type":"switch","z":"5c4848b5cbcb4807","name":"Handstart on","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":100,"wires":[["f9479f1f3382de62"],[]]},{"id":"f9479f1f3382de62","type":"change","z":"5c4848b5cbcb4807","name":"Handstart 1","rules":[{"t":"set","p":"handstart","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":100,"wires":[[]]},{"id":"d319d7659d50330b","type":"change","z":"5c4848b5cbcb4807","name":"Handstart ESP32","rules":[{"t":"set","p":"handstart","pt":"global","to":"payoad","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":180,"wires":[["8e405ca916549aa3"]]},{"id":"8e405ca916549aa3","type":"debug","z":"5c4848b5cbcb4807","name":"debug 43","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":180,"wires":[]},{"id":"d0d69da0d3c24301","type":"mqtt-broker","name":"","broker":"192.168.10.9","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Thanks