michel72
(Michel - espthings.io)
January 14, 2022, 4:51pm
1
Hi,
I would like to read a specific parameter from an api and create an action on the result being true.
Api json output:
Current esphome config, which I modified based on somebody else’s idea…
sensor:
- platform: wifi_signal
name: "WiFi Signal $devicename"
update_interval: 60s
- platform: template
name: "Gesloten"
id: gesloten
http_request:
useragent: esphome/device
timeout: 4s
time:
- platform: sntp
on_time:
- seconds: /10
then:
- lambda: |-
HTTPClient http;
http.begin("https://testapi.local/api/1/toggle-variable?api_key=SECRET");
http.GET();
DynamicJsonBuffer doc(12000);
JsonObject& root = doc.parseObject(http.getStream());
float ip = atof(root["date"]["toggle-variable"][1]["state"].as<char*>());
id(gesloten).publish_state(ip);
But the sensor is not returning anything.
What I would like to do is perform an action (light.turn_on) when id: d4544d56-8a77-40e8-84bd-8be7e2a58ea3 has state: true
and turn it off when id: d4544d56-8a77-40e8-84bd-8be7e2a58ea3 state: false
I am not a coder, so this is quit hard for me.
Anyone that could help me out?
Cheers,
Michel
michel72
(Michel - espthings.io)
January 18, 2022, 6:27pm
3
I hope it’s not Covid infecting peoples willingness to help
pmcenaney
(Patrick McEnaney)
February 8, 2022, 2:27am
5
Did you end up figuring this out? I’ve been trying to do the exact same thing (with the same example you’ve used, lol)
michel72
(Michel - espthings.io)
February 8, 2022, 7:24am
6
No, I did not. If I ever do, I’ll share it here.
Cheers,
Michel
# http请求
http_request:
useragent: esphome/device
id: my_request
timeout: 4s
# 定时操作
interval:
- interval: 3s
then:
- if:
condition: # 定义用于判断的条件
wifi.connected:
then:
- logger.log: "WiFi已连接"
- http_request.get:
url: http://c3-hx711.local/sensor/percentage
on_response:
then:
- if:
condition: # 判断条件 请求状态==200表示成功
- lambda: "return status_code == 200;"
then:
- logger.log: "请求成功"
- lambda: |-
std::string json_string = id(my_request).get_string(); // 获取http返回的字符串
ESP_LOGI("Rx", "%s", json_string.c_str());
// 尝试解析 JSON
json::parse_json(json_string.c_str(), [](JsonObject root) {
id(my_value).publish_state(root["value"]);
});
id(water_signal_count)++;
else:
- lambda: id(water_signal_count) = 0;
- logger.log: "请求失败"
- logger.log:
format: "Response status: %d, Duration: %u ms"
args: [status_code, duration_ms]
这是我做的示例,能用,但是有bug
当url: http://c3-hx711.local/sensor/percentage设备没有在线,会导致看门狗重启
# http请求
http_request:
useragent: esphome/device
id: my_request
timeout: 4s
# 定时操作
interval:
- interval: 5s
then:
- if:
condition: # 定义用于判断的条件
wifi.connected:
then:
- logger.log: "WiFi已连接"
- lambda: |-
WiFiClient client;
HTTPClient http;
bool begin_status = false;
int httpCode = 0;
int wicon,wicon_out;
std::string buffer;
ESP_LOGD("http", "Init Complete");
begin_status = http.begin(client,"c3-hx711.local",80,"/sensor/vol",false);
if(!begin_status){
ESP_LOGD("http", "http.begin failed");
} else {
ESP_LOGD("http", "http.begin OK");
}
http.useHTTP10(true);
ESP_LOGD("http", "http.useHTTP10 flag set");
ESP_LOGD("http", "http.GET");
httpCode = http.GET();
if(!httpCode){
ESP_LOGD("http", "http.GET NOK");
} else {
ESP_LOGD("http", "http.GET OK");
}
if (httpCode > 0) {
ESP_LOGD("http", "HTTP response code is %i", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
// 为HTTP响应设置JSON对象
// 创建一个 DynamicJsonDocument 对象,大小为 4000 字节
DynamicJsonDocument root(4000);
// 从 HTTP 流中反序列化 JSON 内容
DeserializationError error = deserializeJson(root, http.getStream());
// 将 JSON 文档序列化为字符串
String jsonStr;
serializeJson(root, jsonStr); // 使用 serializeJson() 进行紧凑输出
ESP_LOGD("Json", "%s",jsonStr.c_str());
if (root.containsKey("id"))//判断是否有id元素
{
ESP_LOGD("Json", "JSON Parse OK");
}
else {
ESP_LOGD("Json", "Current weather condition not found, JSON parsing failed");
}
}
}
else {
ESP_LOGD("http", "Wrong HTTP response: %i", httpCode);
}
我的http链接是 http://c3-hx711.local/sensor/vol
注意:http和https是不一样的
虽然是获取到了Json数据,如果我的c3-hx711传感器不在线,http请求就会超时,造成设备重启
michel72
(Michel - espthings.io)
August 9, 2024, 6:15am
10
You are on the wrong forums. The Chinese home assistant forum is elsewhere.