This very easy to do using Node Red and an Http Request Node if any one needs it .
Just use http request node with this url: http://your.hassio.ip.address:your_hassio_port_#/api/states/your.entity_id
and get a long lived token from frontend and set token type to bearer and http type to post.
Then pass the desired state to the http node with any other node you want like inject or change node with msg.payload reflecting the state desired in json format.
Is it possible to set the state in this way and then push it into the device?
I would like to avoid that the device will override the new state.
Please explain what you are trying to do. Everything in this thread is bad practice and/or device dependent. Pushing information to devices is usually done through service calls. If you’re trying to change the state of a device and push information back to said device, then you most likely should be creating template entities. But this depends on the device in question.
Hi Petro,
thanks for your answer!
I’m new to Home Assistant and I am studying what could I do with this platform.
I am trying to develop an integration for a switch. When this switch is turned on through Home Assistant I would like to activate another switch implemented through a different integration.
That would be a service call to turn on the other switch. Or just let the users create an automation with home assistant
Hi Petro,
thank you again for your answer and sorry for my late reply.
I had a look at the services documentation, but it is not clear to me how can I use these functionalities inside an integration source code. Is it possible?
Can you give me an example or link me to the right documentation page?
Thank you for your time!
If you’re creating an integration you need to look at the developer documents, not the user documents. Developer documents are linked at the top of the page.
Sorry, I’m not going to write the code for you. This is python, you need to be able to read / understand an api if you plan on writing your own integration.
Typically users do not write their own integrations as this is the hardest thing to do, they make basic automations using the UI. If this is what your after, follow the automation documentation in the docs section at the top of the page.
You’re right, unfortunately, I was not able to find this function in developer documents.
I looked into the accessing the core section but I only found a link to the ServiceRegistry that is not very clear for me.
If you know where I can find more details about this functionality you are welcome, otherwise no problem. I appreciate the hints you have offered me so far.
The first item is the service call in what you linked… This is the api. hass.services is the ServiceRegistry, every call on that object is listed there. Unfortunately, you’ll have to read the descriptions of the methods that you want to use. Hint: The first method is aysnc_call. If you wanted to…
what would you use…
Ok, in this way, I was able to turn on the second switch when the first one was turned on.
Thanks for your support.
I left here a snippet of code that could be useful for other readers.
def turn_on(self):
self.attr_is_on = True
second_switch_id = "my_second_switch"
self.hass.services.call(
domain="switch",
service="turn_on",
service_data={"entity_id": "switch."+switch_id},
)
For people who are not that familiar with NodeRed, APIs, “GET”, REST, “POST” commands, I suggest checking the “Set State” HACS approach shared on the links below. For me, this is by far the most straightforward approach.
Direct Github page:
https://github.com/xannor/hass_py_set_state
https://community.home-assistant.io/t/set-state-without-calling-service/429307/6