Closed voicevon closed 5 years ago
If it´s just for turning on and off something, I use the on_message trigger in the mqtt section. Then I send an unretained message with just the topic, eg: /d1mini/luz_quincho And the on_message automation toggles a switch. Works great for me
Thank you @HFsi . I am using the switch to verity the mqtt_subscribe_sensor.
Actually, I just want to get an number from somewhere(like mqtt dash), This number determines how long time my motor will keep running. I tried your method, I am sure that my target number is inside the payload of that message, but it's "Hidden", I don't know how to get that number from on_message
trigger.
I believe the issue might be connected to failed subscriptions. Try with the latest edge version.
I am sure the connection to mqtt broker has no problem, because there is another binary_gpio_sensor is working fine. Is it possible publish is OK ,but subscript fails?
Since I am using vs-code + Platformio, I can sync the esphomelib source code and compile it. I want to know whether the edge branch (the default branch under OttoWinter/esphomelib) is exactly as same as edge (need to uninstall and reinstall the Hass.io's addon).
Today, I reinsatalled Hassio + Esphomeyaml edge.The new UI and uploading function works great! But unfortunately , The mqtt_on_message is not good yet. Here is my code:
esphomeyaml:
name: feeder
platform: ESP8266
board: nodemcu
wifi:
ssid: 'xxx'
password: 'xxx'
# Enable logging
logger:
# Enable Home Assistant API
api:
password: '1234567890'
ota:
password: '1234567890'
switch:
- platform: gpio
pin: 5
id: motor_p1
internal: false //BTW: no compiling error, but doesn't work.
- platform: gpio
pin: 4
id: motor_p2
status_led:
pin:
number: 2
inverted: true
mqtt:
broker: 'xxxx'
username: 'xxxx'
password: 'xxxx'
id: mqtt
on_message:
- topic: feeder/switch/motor/command
then:
- lambda: |
id(motor_p1).turn_on();
1) Complied, and uploaded without any problem, But no response when I send message. 2) I copied all code to vs-code, comment these two lines
// motor_p1->set_internal(true);
// motor_p2->set_internal(true);
3)The GPIO_switch works very well. 4) I repeated step1, try to find information from vs-code terminal window. But nothing about "mqtt_on_message" I am sure: 4a) the topic string has no problem. 4b) I tried both retained message and unretained message.
5) During node setup , there is no debug information about "mqtt_on_message" triger.
INFO Successfully connected to feeder.local
[12:55:02][I][mqtt.client:209]: MQTT Connected!
[12:55:02][I][application:089]: setup() finished successfully!
[12:55:02][I][application:097]: You're running esphomelib v1.10.0-dev compiled on Dec 26 2018, 20:24:06
[12:55:02][C][status_led:023]: Status LED:
[12:55:02][C][status_led:024]: Pin: GPIO2 (Mode: OUTPUT, INVERTED)
[12:55:02][C][wifi:324]: WiFi:
[12:55:02][C][wifi:228]: SSID: 'FuckGFW'
[12:55:02][C][wifi:229]: IP Address: 192.168.123.238
[12:55:02][C][wifi:231]: BSSID: CC:34:29:F9:53:BA
[12:55:02][C][wifi:233]: Hostname: 'feeder'
[12:55:02][C][wifi:238]: Signal strength: -63 dB \u2582\u2584\u2586\u2588
[12:55:02][C][wifi:239]: Channel: 7
[12:55:02][C][wifi:240]: Subnet: 255.255.255.0
[12:55:02][C][wifi:241]: Gateway: 192.168.123.1
[12:55:02][C][wifi:242]: DNS1: 192.168.123.1
[12:55:02][C][wifi:243]: DNS2: 0.0.0.0
[12:55:02][C][switch.gpio:034]: GPIO Switch 'motor_p1':
[12:55:02][C][switch.gpio:035]: Pin: GPIO5 (Mode: OUTPUT)
[12:55:02][C][switch.gpio:034]: GPIO Switch 'motor_p2':
[12:55:02][C][switch.gpio:035]: Pin: GPIO4 (Mode: OUTPUT)
[12:55:02][C][logger:094]: Logger:
[12:55:02][C][logger:095]: Level: DEBUG
[12:55:02][C][logger:096]: Log Baud Rate: 115200
[12:55:02][C][ota:130]: Over-The-Air Updates:
[12:55:02][C][ota:131]: Address: 192.168.123.238:8266
[12:55:02][C][ota:133]: Using Password.
[12:55:02][C][mqtt.client:058]: MQTT:
[12:55:02][C][mqtt.client:060]: Server Address: xxx
[12:55:02][C][mqtt.client:061]: Username: 'von'
[12:55:02][C][mqtt.client:062]: Client ID: 'feeder-6001941006e2'
[12:55:02][C][mqtt.client:064]: Discovery prefix: 'api'
[12:55:02][C][mqtt.client:065]: Discovery retain: YES
[12:55:02][C][mqtt.client:067]: Topic Prefix: 'feeder'
[12:55:02][C][mqtt.client:069]: Log Topic: 'feeder/debug'
[12:55:02][C][mqtt.client:072]: Availability: 'feeder/status'
[12:55:02][C][switch.mqtt:048]: MQTT switch 'motor_p1':
[12:55:02][C][switch.mqtt:055]: State Topic: 'feeder/switch/motor_p1/state'
[12:55:02][C][switch.mqtt:055]: Command Topic: 'feeder/switch/motor_p1/command'
[12:55:02][C][switch.mqtt:048]: MQTT switch 'motor_p2':
[12:55:02][C][switch.mqtt:055]: State Topic: 'feeder/switch/motor_p2/state'
[12:55:02][C][switch.mqtt:055]: Command Topic: 'feeder/switch/motor_p2/command'
[12:55:02][I][application:114]: Running through first loop()
[12:55:02][D][switch:037]: 'motor_p1' Turning OFF.
[12:55:02][D][switch:055]: 'motor_p1': Sending state OFF
[12:55:02][D][switch:037]: 'motor_p2' Turning OFF.
[12:55:02][D][switch:055]: 'motor_p2': Sending state OFF
[12:55:02][I][application:141]: First loop finished successfully!
[12:55:02][I][application:097]: You're running esphomelib v1.10.0-dev compiled on Dec 26 2018, 20:24:06
[12:55:02][C][status_led:023]: Status LED:
[12:55:02][C][status_led:024]: Pin: GPIO2 (Mode: OUTPUT, INVERTED)
[12:55:02][C][wifi:324]: WiFi:
[12:55:02][C][wifi:228]: SSID: 'xxx'
[12:55:02][C][wifi:229]: IP Address: 192.168.123.238
[12:55:02][C][wifi:231]: BSSID: CC:34:29:F9:53:BA
[12:55:02][C][wifi:233]: Hostname: 'feeder'
[12:55:02][C][wifi:238]: Signal strength: -63 dB \u2582\u2584\u2586\u2588
[12:55:02][C][wifi:239]: Channel: 7
[12:55:02][C][wifi:240]: Subnet: 255.255.255.0
[12:55:02][C][wifi:241]: Gateway: 192.168.123.1
[12:55:02][C][wifi:242]: DNS1: 192.168.123.1
[12:55:02][C][wifi:243]: DNS2: 0.0.0.0
[12:55:02][C][switch.gpio:034]: GPIO Switch 'motor_p1':
[12:55:02][C][switch.gpio:035]: Pin: GPIO5 (Mode: OUTPUT)
[12:55:02][C][switch.gpio:034]: GPIO Switch 'motor_p2':
[12:55:02][C][switch.gpio:035]: Pin: GPIO4 (Mode: OUTPUT)
[12:55:02][C][logger:094]: Logger:
[12:55:02][C][logger:095]: Level: DEBUG
[12:55:02][C][logger:096]: Log Baud Rate: 115200
[12:55:02][C][ota:130]: Over-The-Air Updates:
[12:55:02][C][ota:131]: Address: 192.168.123.238:8266
[12:55:02][C][ota:133]: Using Password.
[12:55:02][C][mqtt.client:058]: MQTT:
[12:55:02][C][mqtt.client:060]: Server Address: xxxx
[12:55:02][C][mqtt.client:061]: Username: 'von'
[12:55:02][C][mqtt.client:062]: Client ID: 'feeder-6001941006e2'
[12:55:02][C][mqtt.client:064]: Discovery prefix: 'api'
[12:55:02][C][mqtt.client:065]: Discovery retain: YES
[12:55:02][C][mqtt.client:067]: Topic Prefix: 'feeder'
[12:55:02][C][mqtt.client:069]: Log Topic: 'feeder/debug'
[12:55:02][C][mqtt.client:072]: Availability: 'feeder/status'
[12:55:02][C][switch.mqtt:048]: MQTT switch 'motor_p1':
[12:55:02][C][switch.mqtt:055]: State Topic: 'feeder/switch/motor_p1/state'
[12:55:02][C][switch.mqtt:055]: Command Topic: 'feeder/switch/motor_p1/command'
[12:55:02][C][switch.mqtt:048]: MQTT switch 'motor_p2':
[12:55:02][C][switch.mqtt:055]: State Topic: 'feeder/switch/motor_p2/state'
[12:55:02][C][switch.mqtt:055]: Command Topic: 'feeder/switch/motor_p2/command'
I don't know whether this is the issue only on Esp8266 or not. @HFsi , Were you using Esp8266 or Esp32?
I use ESP8266. How do you send the MQTT message? In my case, the topic that controls the trigger isn't correlated to any id or internal config, eg:
on_message:
Where "luz" is nothing, simply a word to activate de below script. I don't know if I'm clear enough
@HFsi Thank you very much. I tried your way.
mqtt:
broker: 'xxxx'
username: 'xxxx'
password: 'xxxx'
on_message:
- topic: abcd/efgh
then:
- script.execute: qwert
script:
- id: qwert
then:
- switch.turn_on: motor_p1
but has no affect. The second trying is removing script.
on_message:
- topic: abcd/efgh
then:
- switch.turn_on: motor_p1
Still no response.
I sent message with 9(maybe more) styles those are combined with Qos{0/1/2} + retained/none-retained + payload={empty/ number/string}.
It seems the difference between you and me is (or might be) 1) The board. NodeMCU vs Sonoff. They are so similar 2) Another potential thing is mqtt server I am using " RespB3 plus“ + ”mosquito“. I don't no about your side. 3) I have two RespPi,Same hardware. First is Hassio+mqtt. The second server is Hassio + Esphomeyaml.
@HFsi , Did you see any debug information about on_message's setup or received?
This is how I create the message for the topic, in scripts.yaml
toogle_luz_quincho: sequence:
Maybe can help
Thank you @HFsi . But I couldn't understand that. (My background is C programmer)
I debugged c++ source code, I found the reason closely right now. I hope the below can help @OttoWinter BTW: I am using the newest version You're running esphomelib v1.10.0-dev compiled on Dec 27 2018, 09:50:54␛[0m
// modified main.cpp, (coomment two lines )
// motor_p1->set_internal(true);
// motor_p2->set_internal(true);]
//This is invoked before mqtt_connected,
void MQTTClientComponent::subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos) {
MQTTSubscription subscription{
.topic = topic,
.qos = qos,
.callback = std::move(callback),
.subscribed = false,
.resubscribe_timeout = 0,
}; Serial.println("===================== MQTTClientComponent::subscribe()" );
this->resubscribe_subscription_(&subscription);
this->subscriptions_.push_back(subscription);
}
//this is invoked after mqtt_connected
void MQTTClientComponent::resubscribe_subscriptions_() {
Serial.printf("---------------------Subscriptions size = %i",this->subscriptions_.size());
Serial.println();
for (size_t i = 0; i < this->subscriptions_.size(); i++) {
Serial.printf("------------------------------------ %i",i);
this->resubscribe_subscription_(&this->subscriptions_[i]);
}
Serial.println();
}
1)In main.cpp. there is two gpio_switch , and one mqtt_on_message_trigger.
1A) Before mqtt_connected, I saw two subscription ,it is motor_p1 and motor_p2 I guess. But It should be 3 subscriptions.
1B) After mqtt_connected, subscription size =2
2) In main.cpp, I removed all gpio_switch, also removed on_message_trigger.
2A) Before mqtt_connected, No subscription.
2B) After mqtt_connected, subscription size =0
3) Conclusion:
3a) The on_message topic has not been subscript to broker.
3b) Mqtt_onmessage is using other way to subscribe a topic.
4) Some thought
4a)In main.cpp setup(), add this line: App.get_mqtt_client()->subscribe("abcd/efg", callback_function ,0);
I don't know the code of callback_function.
Operating environment/Installation (Hass.io/Docker/pip/etc.): Hass.io
ESP (ESP32/ESP8266/Board/Sonoff): Esp8266
Affected component: https://esphomelib.com/esphomeyaml/components/sensor/mqtt_subscribe.html After resetting,The node got the correct value on the first time, but when I publish a new value, the esp node has no response, I even tried send a non-number payload with
"abc"
, the debugger didn't show any warning information.Problem-relevant YAML-configuration entries:
Traceback (if applicable):
Additional information: My other client is my mobile phone with mqtt-dash app that it subscript the same topic. it shows the sensor with the updated value.