Compatibility with ESPHome
amotl opened this issue · comments
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"}
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 |-
?
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!
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?
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!
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