esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
291 stars 34 forks source link

http_request with on_message logger #3098

Closed quentin9696 closed 2 years ago

quentin9696 commented 2 years ago

The problem

I try to make HTTP request on_message action with the logger component. This will help me to have a central logging with Loki, since I got a large number of device to manage.

This syntax don't works with logger component.

logger:
  level: VERY_VERBOSE
  on_message:
    then:
      - http_request.post:
          url: http://xxx.xxx.xxx.xxx:3100/loki/api/v1/push
          headers:
            Content-Type: application/json
          body: !lambda |-
            return "{\"streams\": [{ \"stream\": { \"room\": \"desk\" }, \"values\": [ [ \"" + to_string(id(sntp_time).now().timestamp) + "000000000\", \"" + message + "\" ] ] }]}";

The same syntax works with a sensor, so it's not an issue with my Loki. Also, I capture network packets on my loki server and I can't see anything. I can get other packet comming from my ESP (prometheus metrics), so ESP is well connected on my wifi network and packets transit from it.

FYI, here is a working workflow with a basic DHT sensor:

sensor:
  - platform: dht
    pin: 4
    temperature:
      name: "temperature"
      id: "dht22_temperature"
      on_value:
        then:
          - http_request.post:
              url: http://192.168.0.137:3100/loki/api/v1/push
              headers:
                Content-Type: application/json
              body: !lambda |-
                return "{\"streams\": [{ \"stream\": { \"room\": \"desk\" }, \"values\": [ [ \"" + to_string(id(sntp_time).now().timestamp) + "000000000\", \"" + to_string(x) + "\" ] ] }]}";
    humidity:
      name: "humidity"
      id: "dht22_humidity"
    update_interval: 2s
    model: DHT22

Which version of ESPHome has the issue?

ESPHome version 2022.2.5

What type of installation are you using?

Docker

Which version of Home Assistant has the issue?

N/A

What platform are you using?

ESP32

Board

esp-wrover-kit

Component causing the issue

logger

Example YAML snippet

check issue description above

Anything in the logs that might be useful for us?

No response

Additional information

No response

quentin9696 commented 2 years ago

Just an update:

The message variable return the formatted code with ansi color balise. Do you know how to clean up ansicolor garbage to format correctly my message ?

It works when I don't use message variable.

quentin9696 commented 2 years ago

Finally solve my issue.

As already mention, message variable return the log entry formatted with ansicolor. I simply use isprint method to cleanup non printable character since Loki return me 400 http code with message malformed json when I keep those non printable characters