Create a custom enitity/sensor from a webhook JSON payload

Hi all,

I am new here, but been using home assistant for the last couple of months after migrating from OpenHAB. So far I think it is great, but am struggling with my most recent configuration, I wonder if anyone can shed some light on the correct way to progress. Just point me in the right direction, because i believe i might be barking up the wrong tree.

I have a service that pushes data to a webhook (brewfather). From this I have been successful in triggering an automation and passing the payload to that automation (just a notification)

- alias: Capture Ispindel from Brewfather
  trigger: 
  - platform: webhook
    webhook_id: brewfather
  action:
    service: notify.mobile_app_pixel_2
    data:
      message: '{{trigger.json}}'

This passes the raw payload JSON, just so i know i can get at it and the webhook is working.

The JSON contains the following data items:

id:
type:
time:
temp:
sg:
angle:
battery:
rssi:

What ideally want to be able to do is set up a bunch of sensors (or a entity) that I can use to replay this data onto the dashboards (and ultimately log in grafana).

I can’t find how to setup custom sensors or entities in home assistant so i can pass the JSON payload to set their values.

I have trawled through the docs and forums and it looks like templates are the way to go, but it seems that can only be used to map one sensor to another.

What am I missing here?

Cheers in advance.

Mat

Might be easiest to just use the restful api

Look at POST /api/states/<entity_id>

Then have your webhook event call that restful command.

There are probably better examples, but I think you could make a command like this:

rest_command:
  update_webhook_sensor:
    url: https://my_home_assistant_url/api/states/{{sensor_id}}
    method: POST
    headers:
      authorization: !secret ha_secret_bearer_token
    payload: '{"state":"{{state}}", "attributes":{{attributes_json}} }'
    content_type:  'application/json; charset=utf-8'
    verify_ssl: true

Now in a webhook trigger, call that command

- alias: Capture Ispindel from Brewfather
  trigger: 
  - platform: webhook
    webhook_id: brewfather
  action:
    service: rest_command.update_webhook_sensor
    data:
      # Make sure the sensor id is unique. Can name it whatever you want really.
      sensor_id: "sensor.{{trigger.json.id}}"
      # Make the state whatever you want. Can be a value from the json even.
      state: "{{'Available' if trigger.json else 'Unavailable'}}"
      # Just store the entire json as individual attributes.
      attributes_json: "{{ trigger.json }}"

There will most certainly be some tweaks required. Probably have to fix the rest_command payload template at the very least lol.

If there are multiple types, you could use that to map them to the right sensor type (binary_sensor, climate, etc). Assuming these were all valid types that were supported…you could do:

sensor_id: "{{trigger.json.type}}.{{trigger.json.id}}"

1 Like

Thanks Jim,

Will have a look and see what I can come up with

Actually, I think what you are suggesting is that i update the sensor through the restful API.

I think where I am failing is a can’t work out how to add a (blank) custom sensor to fill with data. Rather than a sensor from an integration. I assume you actually need a sensor to update through the API

I think i’m missing a fundamental piece of how this works

Nope, this will just create a sensor if the endpoint doesn’t exist.

Try it yourself. Open curl and post to /api/states/sensor.this_is_cool and give it a state. It will show up.

But do note, this sensor will disappear on all reboots until a new webhook trigger is received. In which case I would maybe create a template sensor that tracks this webhook sensor so you have something there on reboots.

This is where custom integrations get an advantage as they can store their info in a database to live beyond reboots.

Lovely. That makes sense

This is exactly what I am attempting to do Mat! Have you had any success with this method? I started using the webhook trigger to set the value of an input number, but I think I like the idea of storing it in a sensor better.

I love using the iSpindel to track fermentation, such neat data to have! :beers:

Cheers,
Michael.

I struggled massively with that and decided to change tack and use node-red to connect to the brewfather web hook and used home assistant extension to feed it into sensors.

Also worth noting that there is a brewfather addon for node-red which means you can pull details of your batches. I still have a couple of wrinkles to iron out but i’m getting there

Hey, that’s pretty cool - I didn’t even really consider checking there. It lead me to realise that Brewfather has an API that can be used to gather data about your brews etc. It looks like this is what the Node Red solution is doing. Having never used a REST API or been a much of a programmer, I thought I’d give it a crack - because I didn’t already had enough to do in life, right?

So using a REST sensor and checking the API for content is probably a comparatively straightforward way of doing this. If I make any headway, I’ll chuck it up on here.

Sounds good. Like I said i have a couple of kinks to iron out but would be happy sharing my node red flows of that is of interest.