Comments (5)
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.
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.
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.
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.
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)
- docker-compose up is taking very long HOT 2
- Video tutorial HOT 1
- Panels are not updated on instant dashboards after update to Grafana 9.3.1 and Kotori 0.27.0 HOT 1
- Support new devices for DAQ-SIG
- Add ISEMS project to gallery
- Add "Well Depth Monitor" to project gallery
- Grafana: Adjust a few integration details
- Error channel reports `'NoneType' object has no attribute 'endswith'`
- Docker is sunsetting Free Team organizations HOT 5
- [Proposal] Add a generic device-based addressing scheme for "WAN" networks HOT 1
- Modernize firmware builder to use PlatformIO
- bunch Β» munch Β» benedict
- Support FIWARE NGSI-LD, NGSIv2, and Ultralight 2.0 protocols
- Support Sparkplug MQTT protocol HOT 2
- Make plumbing less opinionated
- Receiving telemetry data via UDP HOT 1
- Support for InfluxDB 2.x HOT 7
- Support receiving data via AMQP
- Problem when using unicode characters in channel name or field name
- Unhandled exception: module 'pandas' has no attribute 'tslib' HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotori.