Ok… So I managed to get this working both locally and via the Nabu Casa URL.
It would be useful if this was more integrated with the API and/or Logger(?) so you could define the URL.
This method works great but as you can probably tell - I’m not too familiar with ESP … yet.
Use:
To send data to Home Assistant via Nabu Casa’s remote access (or other alterative) from any ESP device that has a WiFi connection.
This means you can connect to any WiFi network and send updates remotely.
Requirements:
-You must have remote connection on your Home Assistant (e.g. Nabu Casa)
Example: https://YOUR_NABU_CASA_URL_HERE.ui.nabu.casa
-Generate a Long-Lived Access Token
– This is stored in the secrets.yaml as bearer_token
Substitutions / basic config
Firstly I checked everything was working good with the local IP.
I then changed the code (as-is now) to work with the Nabu Casa IP.
I’ve also included some basic config for the espnodemcu.
substitutions:
hostname: AnyTestName
BearerLocalIP: http://YOUR_LOCAL_IP_HERE:8123
NabuCasaAddress: https://YOUR_NABU_CASA_URL_HERE.ui.nabu.casa
BearerSensorName: sensor.YOUR_SENSOR_NAME_HERE
esphome:
name: testnodemcuv2
platform: ESP8266
board: nodemcuv2
logger:
api:
ota:
web_server:
port: 80
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
captive_portal:
Adding a test sensor
I added the Uptime Sensor on the ESP as a test to send the data over the HTTP command.
This uses the ID ‘uptime_sensor’ which is used in the HTTP command but this can be changed to which ever ID you like.
sensor:
- platform: uptime
name: $hostname Uptime Sensor
id: uptime_sensor
update_interval: 60s
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(uptime_sensor).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? to_string(days) + "d " : "") +
(hours ? to_string(hours) + "h " : "") +
(minutes ? to_string(minutes) + "m " : "") +
(to_string(seconds) + "s")
).c_str();
Adding a Button to call the HTTP command
I then added a test Button which would call the HTTP on press.
button:
# Button on press calls the http command
- platform: template
name: $BearerSensorName
on_press:
then:
- http_request.post:
method: POST
####
## Change IP and Sensor name within the substitutions
## You can use the local or remote IP
## Remember the URL has /api/states/ hard coded
####
url: $NabuCasaAddress/api/states/$BearerSensorName
headers:
Authorization: !secret bearer_token
Content-Type: application/json
verify_ssl: false
json:
####
## You can customize this part but remember to include 'state'
root["state"] = id(uptime_human).state;
####
## Add more data / attributes here
## Refer to Home Assistant API Docs
## https://developers.home-assistant.io/docs/api/rest/
####