Help with home assistant api call


I am new to home assistant, i want to make a script that posts to the states api. This is the same api that is available in the web gui for home assistant.

This is the api call and json string that i want put into a script. I realize all this does is overwrite the value in home assistant, it doesn’t actually call the service and I am fine with that.


If i look on the python remote api then the instructions are:

Of course, it’s possible to set the state as well:

import homeassistant.remote as remote
from homeassistant.const import STATE_ON

api = remote.API('', 'YOUR_PASSWORD')
remote.set_state(api, 'sensor.office_temperature', new_state=123)
remote.set_state(api, 'switch.livingroom_pin_2', new_state=STATE_ON)

If I look on the RESTful API the instructions are:

POST /api/states/<entity_id>
Updates or creates the current state of an entity.

Expects a JSON object that has at least a state attribute:

    "state": "below_horizon",
    "attributes": {

However I’m not sure how i call the RESTful API from an actual script, there are no examples that I can learn from.

is it something like this?

    method: POST
    url: ''
    password: 'xxxxxx'
    content_type: application/json
    payload: '{
    "state": "on",
    "attributes": {
        "fan_mode": "auto",
        "operation_mode": "on"

The REST api is easy to access using cURL (linux command, but available for other OS’s). This can be done with other tools or with Python using urllib or requests

Curl example:

curl -X POST \
-H “Content-Type: application/json” \
-d ‘{“state”: “on”, “attributes”: {“device_class”: “motion”, “friendly_name”: Driveway Motion"}}’ \

I use this from my camera system (Xeoma) to turn on/off a virtual motion sensor.

I have no experience with programming or scripts, I would understand how to modify that to put my own variables in, but I don’t know what a curl command is, i assume curl is something i could write at the command prompt, anyway I’m quite clueless as how I can put a curl command into home assistant so i can fire of a curl command from automation.yaml

sorry I am very very very noob, I’m doing things outside of my experience level (which is basically installing home assistant a few weeks ago lol)

Ok. First things then.

What OS are you wanting to run this from? This is to cause a state change within Home Assistant, so I’m guessing you want to run this from a different system. Correct? So what OS does that system use?

I have hassio on a rasp pi 3b.

I have a open/close sensor and i want to change the state and attributes of an entity (climate.panasonic_aircon), when the sensor changes state. I am ok writing the automation.yaml for this I just get stuck on this part

  # magic happens and the state / attributes of the entity changes

Ok. But I see you want to change the state of a device. To do that something has to trigger that state change. This is usually from some “external” source or from an automation or manual flip of a switch inside hassio.

So maybe I’m confused or lost. What are you actually trying to do?

Short answer

- alias: set climate state to off when aircon turns off
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
      from: 'on'
      to: 'off'
    # change the state of climate.panasonic_aircon to off

Long answer:

I have a dumb air conditioner.

I use it with the broadlink climate custom component:

If the air conditioner is turned on / off using the original IR remote then home assistant and the climate control doesn’t know that the aircon has be turned on.

I added a zigbee door / window sensor to the air conditioner to detect if it is turned on / off

I thought I might be able to use this to update home assistant to tell it what the air conditioner is doing.

edit: i wrote in my post that the state was reverting back, but it’s not the state is sticking. As I test i changed the climate.panasonic_aircon state to “tomatoes” and it is staying as tomatoes, so that is good.

tomatoes…I like that… ok I’m tired and I need some more nicotine. But here’s a shot.

Take a look at the climate component “Generic Thermostat” for some ideas and here’s a snip from my climate automations

  - service: climate.set_temperature
      entity_id: climate_front_room_thermostat
      ac_mode: False
      temperature: 70

There’s a lot of options, but you would pass them to the entity_id through the data tag. Match the options with your climate component you are using. That’s not a exact example as I’m using the vera temperature component. But gives you an idea.

1 Like

Thanks. Unfortunately that’s not what I am after I already know how to call the climate service.

I want to change (overwrite) the state without calling the service. I can do it using the web interface, but I don’t know how to put that into a script.

The curl example I gave before should work with the “script component” just modify it to work. Does the same as the web interface.

However. With using it is limited to what is provided within I don’t run anymore as it was too limiting as I have a bi-directional setup with a VeraPlus unit as my device controller. So I like having command line access and doing http calls between the two systems.

I fired up ssh into my hassio and i typed the curl command, it is available.

I still don’t know how to make a script with curl command as there is no documentation or examples.

like do i type that example into a text file and save it with the .py extension, where do i save it etc… i have done a lot of googling but I can’t find any tutorial or existing example that i can use to figure out how to fire off that curl command from inside an automation in hassio.

actually i think i found it?

actually when i check in HA i should be using the rest_command i think

I think it’s supposed to be this:


    method: POST
    url: ''
    password: 'xxxxxx'
    content_type: application/json
    payload: '{
    "state": "idle",
    "attributes": {
        "fan_mode": "auto",
        "operation_mode": "idle"


- alias: set climate state to idle when aircon turns off
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
      from: 'on'
      to: 'off'
    - condition: template
      value_template: "{{ not is_state('climate.panasonic_aircon', 'idle') }}"     
    - service: rest_command.set_climate_state_idle

but i get an error in configuration.yaml and I’m not sure what the issue is.

Try putting the payload all on one row

Im been strugeling for 15 hours straight now trying to accomplish the same thing as you.
Im using xeoma and has moved from Switchking to HA recently.

Erlier i sent http request like h_ttp://switchking_ip/devies/“device number”/turn_on when motion was detected.

As i understand you have made some binary sensor to listen to request from xeoma.

Trued to used command_line platform, restful-command and i have no clue what im doing wrong.
Even tried to send http request as h_ttp://ip-to my ha server/api/services/switch.turn_on/switch.my_entity
And also h_ttp://ip-to my ha server/api//switch.turn_on/switch.my_entity
But no luck.
when running h_ttp://ip-to my ha server/api/services/switch.turn_on/switch.my_entity i get 405 not allowed and h_ttp://ip-to my ha server/api//switch.turn_on/switch.my_entity (404 not found).

Can you please guide me in the right direction before i go nuts?

Best Regards

Are you passing the x-auth in the header? It sounds like you have authentication enabled. In the API docs there’s an example of passing the auth credentials as well.