Git Product home page Git Product logo

Comments (5)

amotl avatar amotl commented on July 19, 2024

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 |-?

from kotori.

amotl avatar amotl commented on July 19, 2024

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!

from kotori.

amotl avatar amotl commented on July 19, 2024

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?

from kotori.

amotl avatar amotl commented on July 19, 2024

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!

from kotori.

amotl avatar amotl commented on July 19, 2024

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

from kotori.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.