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

2 Likes

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}}"

4 Likes

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.

1 Like

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.

I’m in the same boat, but don’t use brewfather! is there a way to get the ispindel http post to hit HA directly and populate the data in a sensor ? I currently use ubidots to view my data and send webhooks to HA to drive a smart plug connected to a brew pad to control temp. It would be far better to have the data in HA directly - graphing fermentation, battery, temp etc.
Is the RestAPI the way to go here, with a template sensor? I do not have experience of either, but assume its do-able ?

Nah, me either. Not sure how you would transform the payload into something useable. I might lean towards pointing it at node red or something as you may get a bit more flexibility.

Hey Neil!

Have you looked at using MQTT on the iSpindel? It basically sends out a simple message in JSON format that can be read by an MQTT broker. If you’re not already using it, you’d can set up an MQTT broker on HA and then use the MQTT sensors.

I used to use my iSpindel to connect to my HA like this and found it to be very well suited. Also, you can get a better polling rate than Brewfather allows (15mins), if you want higher resolution. I just wanted to use Brewfather to store the fermentation associated info with the batch, recipe etc. for convenience.

Thanks for the reply… I need to try with mqtt. I want sure of I needed something else running to take the mqtt messages and then send them to HA. But can the HA mqtt add on take the ispindel output directly? If so, it would be a nice clean option. I don’t use brewfather at all, and doing it this way would tidy up the automations I currently have via ubidots webhooks firing to HA

Hey mate,
No worries, and sorry about the delay here.
Yep, you can use the MQTT sensor directly with the iSpindel. You just need to make sure that you have configured the iSpindel to use the MQTT service when you set it up.

If you look at the relevant part of the source for the iSpindel, you can get an idea of what variables are output. You’ll also find it useful to get an MQTT client tool (such as MQTT X) on your PC whilst you are getting the whole thing sorted.

The code that I had used for this is here, but you’ll need to change the state topic to reflect the name you used for your iSpindels:

- platform: mqtt
  name: brew_ispindel_tilt
  state_topic: "ispindel/iSpindel/tilt"
  unit_of_measurement: '°'
  force_update: true
        
- platform: mqtt
  name: brew_ispindel_temp
  state_topic: "ispindel/iSpindel/temperature"
  unit_of_measurement: '°C'
  force_update: true
  device_class: temperature

- platform: mqtt
  name: brew_ispindel_battery
  state_topic: "ispindel/iSpindel/battery"
  unit_of_measurement: 'V'
  force_update: true
  device_class: battery

- platform: mqtt
  name: brew_ispindel_gravity
  state_topic: "ispindel/iSpindel/gravity"
  unit_of_measurement: ''
  force_update: true

- platform: mqtt
  name: brew_ispindel_interval
  state_topic: "ispindel/iSpindel/interval"
  unit_of_measurement: 's'
  force_update: true
  
- platform: mqtt
  name: brew_ispindel_rssi
  state_topic: "ispindel/iSpindel/RSSI"
  force_update: true

Cheers,
Michael.

Thanks. I managed to get it working pretty quickly once I realised I’d been using the wrong IP in my MQTT setup!
I’m used to using ubidots to view the data, but the charts in HA are very basic compared (although I am looking at some HACS options). I may end up sticking with my current approach, and just using the webhooks from ubidots into HA.

Quick update after playing with the apex charts - far better than the standard charts and definitely something I can use in place of ubidots:
image

Yeah, the default charts are pretty rubbish! I have been using mini-graph-cards and they’ve been pretty good. The apex charts look decent too, I will try to check them out in more detail sometime.

Hi. I have integrated the iSpindel to my Home Assistant, but I want the battery to show in percent. I have this sensor in my configuration.yaml

sensor:
  - platform: mqtt
    name: "iSpindel Batteri Prosent"
    state_topic: "brew/ferment/tilt/battery"
    value_template: "{{ (states('sensor.ispindel_batterylevel') | float/4.3*100) | round(0) }}"
    unit_of_measurement: "%"

The problem with this is that it shows the percentage from 0 volt up to 4.3. I want to have 3 volt as 0%. Is there a bright head that can guide me?