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:
SET THE STATE OF AN ENTITY
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('127.0.0.1', '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": {
"next_rising":"2016-05-31T03:39:14+00:00",
"next_setting":"2016-05-31T19:16:42+00:00"
}
}
However I’m not sure how i call the RESTful API from an actual script, there are no examples that I can learn from.
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
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)
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 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
action:
# 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?
- alias: set climate state to off when aircon turns off
trigger:
- platform: state
entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
from: 'on'
to: 'off'
action:
# 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
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.
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.
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 hass.io it is limited to what is provided within hass.io. I don’t run hass.io 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.
- alias: set climate state to idle when aircon turns off
trigger:
- platform: state
entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
from: 'on'
to: 'off'
condition:
- condition: template
value_template: "{{ not is_state('climate.panasonic_aircon', 'idle') }}"
action:
- service: rest_command.set_climate_state_idle
but i get an error in configuration.yaml and I’m not sure what the issue is.
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?
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.