ESPHome Sensor via NabuCasa webhook

Is it possible to create a sensor with ESPHOME that is later working in a remote location and transmits its values via webhook.
e.g to monitor the temperature at work and push the data to my HA at home via webhook. At work there is a WLAN for this.

Creating a local sensor for this task is very easy, but i don’t know how to push the data via webhook.

I want to keep the data as private as possible and my local HA isnt exposed to the internet.

Any hints? Are there any other possibilities?

Greetings from germany

1 Like

+1, very interested by that too if it’s possible without exposing directly HA on Internet :wink:

In the past, I have done something similar using the following setup:

  • Remote sensor posts values to a could based mqtt server. I used CloudMQTT (free).

  • HA’s mqtt server is configured to mirror cloud values to local mqtt instance.

  • HA configured for a regular MQTT sensor.

Doesn’t require HA to be exposed to internet. Traffic to and from cloud MQTT server can be encrypted with SSL.

This isn’t an easy setup though. Especially the part for configuring MQTT mirroring. I wrote up a forum post a few years back with instructions though.

Well, i dislike the mqtt way, but this is my personal taste.
I will take a look at ESP-Easy and mycropython. I am not sure if both are able to call https://-urls.

I will keep posting my results.

Why not set up a vpn between your office and home?

So the ESP has to have a vpn client? I think this is impossible due to performance of the chip.

No, lan to lan.

A simple EPS8266 with an temp/humidity sensor connected to our public WLAN would do the job.

A vpn-solution like a raspi with vpnclient controlled via remote gpio from my home HA-Server would be “overkill”. But you are right, this could work.

Did you ever get this working? I‘m also looking for a solution for the same scenario.

Grüße

From the webhook documentation at https://www.home-assistant.io/docs/automation/trigger/ .

" . The payload is available in an automation template as either trigger.json or trigger.data . URL query parameters are available in the template as trigger.query . Remember to use an HTTPS URL if you’ve secured your Home Assistant installation with SSL/TLS."

So if you include data in the POST, then you can get it via a template as trigger.json or trigger.data . If you included it as a parameter in the GET, it is trigger.query.

No VPN needed if you have webhooks exposed, just include your data in the HTTP request.

Yes, but ESPHOME wasn’t capable on doing HTTP request until the last version. HTTP Request — ESPHome
I didn’t try it but now it should work.

Ah, I missed the part that it required ESPHome.

How do I get this data into a sesor? Or access it from outside the automation to plot it on a card?

Not yet finished but putting data to a webhook works. To store the data i will use a input_number and update the value within an automation using the service input_number.set_value

esphome:
  name: workesp
  platform: ESP8266 
  board:  d1_mini_pro

wifi:
  networks:
  - ssid: top
    password: secret

time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Berlin
    on_time:
      # Every 5 minutes
      - seconds: 0
        minutes: /5
        then:
         http_request.post:
            verify_ssl: false
            url: https://hooks.nabu.casa/xxxcccvvv
            headers:
              Content-Type: application/json
            json:
              temp: !lambda |-
                return ((std::string) to_string(id(s_temp).state));
              humi: !lambda |-
                return ((std::string) to_string(id(s_hum).state));
              soil: !lambda |-
                return ((std::string) to_string(id(s_soil).state));

http_request:
  useragent: device_1431
  timeout: 10s
  
i2c:
  scan: True

sensor:
  - platform: adc
    pin: A0
    name: "Blumentopf"
    update_interval: 120s
    id: s_soil
    filters:
      - multiply: 3.3
  - platform: dht12
    update_interval: 120s
    temperature:
      name: "Temperatur"
      id: s_temp
    humidity:
      name: "Feuchte"
      id: s_hum

Thank you I"ll give that a try!

Can confirm this to work. ESPHome node in remote location and webhook via nabu casa.
It is old thread but I had some problems to make this so posting here.

Only thing I couln’t figure out yet how to make sensors on HA side with only one webhook.
I’m sending two temperature values in json and than comes through just fine. But seems that HA can trigger only once to one webhook.

This was in HA and only the first one works. Second fails with log input that webhook id already in use. So if you use only one sensor this works or create a second webhook for the other.

template:
  - trigger:
      - platform: webhook
        webhook_id: <webhook id 1>
    sensor:
      - name: "ilp_temp"
        state: "{{trigger.json.ilp_temp}}"
        unit_of_measurement: "°C"
  - trigger:
      - platform: webhook
       #NOTE: Doesnt work with the same hook id
        webhook_id:  <webhook id 1>
    sensor:
      - name: "cabinet_temp"
        state: "{{trigger.json.cabinet_temp}}"
        unit_of_measurement: "°C"

I used node red then to create a webhook and split the sensors to HA. Nore red created webhook id is also on the ESPHome. Just activate it from the cloud page and use the nabu casa hook, not the one with local IP.

This is the relevant ESPHome conf


http_request:
    useragent: offsite tempsensor
    timeout: 10s

sensor:
    - platform: dallas
      address: 0x020301a279f7ae28
      name: "Mökin lämpötila"
      id: cabinet_temp
    - platform: dallas
      address: 0xfe0300a2796d0c28
      name: "ILP lämpötila"
      id: ilp_temp
      on_value:
        then:
          http_request.post:
            verify_ssl: false
            url: https://hooks.nabu.casa/copy_url_from_cloud_page
            headers:
              Content-Type: application/json
            json:
              ilp_temp: !lambda |-
                  return ((std::string) to_string(id(ilp_temp).state));
              cabinet_temp: !lambda |-
                  return ((std::string) to_string(id(cabinet_temp).state));

One more caveat to here is that sensor decimals doesnt get cut. Have to figure out that still but at least it works now. Have to get rid of those and apply some filtering.
image

Nice,

now I use this esphome conf:

esphome:
  name: workesp
  platform: ESP8266 
  board:  d1_mini_pro

wifi:
  networks:
  - ssid: top
    password: secret

time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Berlin
    on_time:
      # Every 5 minutes
      - seconds: 0
        minutes: /5
        then:
         http_request.post:
            verify_ssl: false
            url: https://hooks.nabu.casa/xxxcccvvv
            headers:
              Content-Type: application/json
            json:
              temp: !lambda |-
                return ((std::string) to_string(id(s_temp).state));
              humi: !lambda |-
                return ((std::string) to_string(id(s_hum).state));
              soil: !lambda |-
                return ((std::string) to_string(id(s_soil).state));

http_request:
  useragent: device_1431
  timeout: 10s
  
i2c:
  scan: True

sensor:
  - platform: adc
    pin: A0
    name: "Blumentopf"
    update_interval: 120s
    id: s_soil
    filters:
      - multiply: 3.3
  - platform: dht12
    update_interval: 120s
    temperature:
      name: "Temperatur"
      id: s_temp
    humidity:
      name: "Feuchte"
      id: s_hum

It sends the values every 5 minutes. On the home assistant side I use an automation triggered by the web hook. In this automation I set the received values to helpers.

{{trigger.json.Feuchte | round(1) }}

So you can handle all values from the remote sensor in one automation.

2 Likes

@lubeda
But makes the sensor to be in input_number domain or did you make it as sensor somehow?

Yes, but this is no problem (I think). You can set the unit of measurement for your graphs.