Send Rest data to external services

Hi :

I am new to using the Rest Sensors. I want to be able to send the state of certain devices in my home assistant instance to an external server. I created a rest Sensor and I am able to test that it sends messages sucessfully. But I cant figure out how to send a group of sensor states .

If I have light.familyroom ; light.bedroom; light.bathroom. How do I send the state of each of these devices in my rest API endpoints. I can static values with no problem but not sure how to send the states of these light sensor as example. The idea of creating this sensor is to only give limited data to this external server of only the defined entities.

sensor:
  - platform: rest
    name: HOME_STATUS
    resource: api url
    headers:
      Authorization: !secret access_token
      Content-Type: application/json 
    force_update: true
    method: GET
    value_template:

Restful sensors do not send values, they receive them.

I see; so do I need to then do a Rest post command ? The main issue I dont know how to do is to set up the value template to send the values. I tried

value_template: {“light_bath”: “{{ states(‘light.bath’) }}” and it give me an error. Perhaps is because it needs to be a POST command ?

Yes.

The payload option supports templates.

rest_command:
  example_request:
    url: homeassistanturl/api/states/sensor.home_status
    headers:
      authorization: !secret access_token
    method: POST
    payload: > 
      "{"light_bath": "{{states('light_bath')}}"}"

I get : {“message”:“Invalid JSON specified.”} when I use Restful Command.

I am sort of stuck in here. Note that my intention is that the external application calls home assistant for the data. If anyone has any ideas. But it sounds like restful commands is intended for home assisstant to initiate the communication to external API endpoints.

Don’t quote multi-line templates. So this:

    payload: > 
      {"light_bath": "{{states('light_bath')}}"}

Or this (quoted single line template):

    payload: '{"light_bath": "{{states('light_bath')}}"}'

Wait, are you sending this to home assistant?

You said:

Why are you sending it to the home assistant url?

No my intention is that the external application calls Home assistant and home assistant hands the values to it. It still causes the Json issue fixing the quotes

Then why are you doing this in home assistant?

Your two options: push data from home assistant or pull data from the external server.

You are currently doing the first (push) when you want the second (pull).

For that you need to set your external server up to query the HA rest API.

Thank you tom you always very knowledgeable. The reason i was trying to do it though home assistant was to restrict the data pulls to a limited data set if that was possible. Not to expose the whole home assistant. Ill try the API

That works and its a lot easier. I am not sure how restrict the exposed entities but for now this will do.

I don’t believe you can. The API works with any entity.