Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with ESPHome #67

Open
amotl opened this issue Nov 20, 2021 · 5 comments
Open

Compatibility with ESPHome #67

amotl opened this issue Nov 20, 2021 · 5 comments

Comments

@amotl
Copy link
Member

amotl commented Nov 20, 2021

Hi there,

friends of Kotori recently started playing with the excellent ESPHome by @OttoWinter and contributors (cheers!). It would be sweet to add a corresponding page to the documentation which outlines best practices how both software components can be made work together, based on ESPHome's mqtt.publish_json Action.

It looks pretty straight-forward, yet we will have to figure out how to embed the timestamp (Time) into the MQTT payload.

With kind regards,
Andreas.


- mqtt.publish_json:
    topic: the/topic
    payload: |-
      root["key"] = id(my_sensor).state;
      root["greeting"] = "Hello World";

    # Will produce:
    # {"key": 42.0, "greeting": "Hello World"}
@amotl
Copy link
Member Author

amotl commented Nov 20, 2021

esphome/esphome#719 gives some clues, here it would be for HTTP telemetry, but it can surely be applied to MQTT as well:

- http_request.post:
    url: https://api.telegram.org/bot<BOT_ID>:<BOT_SECRET>/sendMessage
    headers:
      Content-Type: application/json
      X-Custom-Header: !lambda |-
        return "hValue";
    json:
      chat_id: <CHAT_ID>
      disable_web_page_preview: 1
      text: !lambda |-
        char str[20];
        time_t currTime = id(sntp_time).now().timestamp;
        strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", localtime(&currTime));
        return (std::string) "Current time is " + str;

It looks like this example uses Templates (Lambdas):

With templates inside ESPHome, you can do almost everything.

However, using the non-lambda approach like outlined in the first comment, could this work as well in order to bring the current value of the SNTP Time Source into the outbound MQTT payload, just using |- instead of !lambda |- [1]?

- mqtt.publish_json:
    topic: amazonas/ecuador/cuyabeno/node-01/data.json
    payload: |-
      root["key"] = id(my_sensor).state;
      root["time"] = id(sntp_time).now().timestamp;
      root["greeting"] = "Hello World";

[1] Is there actually a difference, or is |- just a shortcut notation for !lambda |-?

@amotl
Copy link
Member Author

amotl commented Nov 20, 2021

We just heard back from the workbench that this snippet works well:

time:
  - platform: sntp
    id: sntp_time

mqtt:
  broker: foobar
  on_message:
    - topic: esp32/bme280
      payload: "status"
      qos: 0
      then:
        - mqtt.publish_json:
            topic: workbench/testdrive/area-42/evb-ea-ind-02/data.json
            payload: |-
              root["time"] = id(sntp_time).now().timestamp;
              root["bme280_temp"] = id(bme280_temperature).state;
              root["bme280_humidity"] = id(bme280_humidity).state;
              root["bme280_pressure"] = id(bme280_pressure).state;

Thanks for sharing!

@amotl
Copy link
Member Author

amotl commented Nov 20, 2021

As a newcomer to ESPHome, I am interested in some more details: I see from the configuration snippet above that

on_message:
  - topic: esp32/bme280

Does that mean that the outbound message is only submitted when an inbound message to the MQTT topic esp32/bme280 has been received.

Would there also be a way to make the ESPHome firmware publish those measurements periodically?

@amotl
Copy link
Member Author

amotl commented Nov 20, 2021

Finding the relevant information was easy. The on_time trigger component of ESPHome will allow you to run all actions as cronjobs.

This powerful automation can be used to run automations at specific intervals at specific times of day. The syntax is a subset of the crontab syntax. [...] Basically, the automation engine looks at your configured time schedule every second and evaluates if the automation should run.

time:
  - platform: sntp
    on_time:

      # Cron syntax, trigger every 5 minutes
      - cron: '* /5 * * * *'
        then:
          - switch.toggle: my_switch

Please note there is also the interval component, which advertises itself as:

This component allows you to run actions at fixed time intervals. For example if you want to toggle a switch every minute, you can use this component. Please note that it’s possible to achieve the same thing with the on_time trigger, but this technique is more light-weight and user-friendly.

# Example configuration entry
interval:
  - interval: 1min
    then:
      - switch.toggle: relay_1

Kudos again to @OttoWinter for conceiving this excellent component framework!

@amotl
Copy link
Member Author

amotl commented Nov 20, 2021

Now, it would be sweet to get a full working example how to bring both details (sensor reading & MQTT publishing vs. periodic actions) together, either based on the interval component or the on_time trigger.

Maybe those would work already? I will be happy to receive feedback.

Using interval

time:
  - platform: sntp
    id: sntp_time

interval:
  - interval: 5min
    then:
      - mqtt.publish_json:
          topic: workbench/testdrive/area-42/evb-ea-ind-02/data.json
          payload: |-
            root["time"] = id(sntp_time).now().timestamp;
            root["bme280_temp"] = id(bme280_temperature).state;
            root["bme280_humidity"] = id(bme280_humidity).state;
            root["bme280_pressure"] = id(bme280_pressure).state;

mqtt:
  broker: daq.example.org
  username: foobar
  password: bazqux

Using on_time

time:
  - platform: sntp
    id: sntp_time
    on_time:

      # Cron syntax, trigger every 5 minutes
      - cron: '* /5 * * * *'
        then:
          - mqtt.publish_json:
              topic: workbench/testdrive/area-42/evb-ea-ind-02/data.json
              payload: |-
                root["time"] = id(sntp_time).now().timestamp;
                root["bme280_temp"] = id(bme280_temperature).state;
                root["bme280_humidity"] = id(bme280_humidity).state;
                root["bme280_pressure"] = id(bme280_pressure).state;

mqtt:
  broker: daq.example.org
  username: foobar
  password: bazqux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant