As we all know, an automation has trigger, condition and action. In the part of action, can i send a http request? Just like, when the light turned on, call a restful API do something.
@anon43302295 Thanks a lot for your reply! I’ve seen this link and tried it. Actually it told me how to add a rest_command to configuration.yaml, and I can call it in automation. But how to call it? Like another automation rule,
- alias: open light when switch off initial_state: true trigger: - platform: state entity_id: switch.plug_158d0001b8e41d from: 'on' to: 'off' action: - service: light.turn_on entity_id: light.gateway_light_7811dcaf1e8a
If i want to call a rest_command in an automation rule, how to define the part of action?
Look at the part which says “the name used to expose the service”.
The service is what your automation calls. Instead of light.turn_on, you would call your service.
oh got it. I asked a silly question… One more question, i tried get method and succeeded. When i used post method, how to add parameters? Like some json strings that contain the information i want to send. I add a string in payload field but terminal told me wrong like follow:
ERROR:homeassistant.core:Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/home/sun/.local/lib/python3.5/site-packages/homeassistant/components/rest_command.py", line 99, in async_service_handler
headers=headers
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/helpers.py", line 99, in __iter__
ret = yield from self._coro
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/client.py", line 267, in _request
conn = yield from self._connector.connect(req)
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/connector.py", line 402, in connect
proto = yield from self._create_connection(req)
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/connector.py", line 748, in _create_connection
_, proto = yield from self._create_direct_connection(req)
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/connector.py", line 831, in _create_direct_connection
req=req, client_error=client_error)
File "/home/sun/.homeassistant/deps/lib/python3.5/site-packages/aiohttp/connector.py", line 796, in _wrap_create_connection
return (yield from self._loop.create_connection(*args, **kwargs))
File "/usr/lib/python3.5/asyncio/base_events.py", line 682, in create_connection
yield from self.sock_connect(sock, address)
File "/usr/lib/python3.5/asyncio/selector_events.py", line 402, in sock_connect
return (yield from fut)
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 266, in result
raise CancelledError
concurrent.futures._base.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/sun/.local/lib/python3.5/site-packages/homeassistant/components/rest_command.py", line 99, in async_service_handler
headers=headers
File "/home/sun/.local/lib/python3.5/site-packages/async_timeout/__init__.py", line 35, in __exit__
self._do_exit(exc_type)
File "/home/sun/.local/lib/python3.5/site-packages/async_timeout/__init__.py", line 80, in _do_exit
raise asyncio.TimeoutError
concurrent.futures._base.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/home/sun/.local/lib/python3.5/site-packages/homeassistant/core.py", line 1031, in _event_to_service_call
yield from service_handler.func(service_call)
File "/home/sun/.local/lib/python3.5/site-packages/homeassistant/components/rest_command.py", line 109, in async_service_handler
_LOGGER.warning("Timeout call %s.", request.url)
UnboundLocalError: local variable 'request' referenced before assignment
You’ll get better help if you show exactly what you tried. It is hard to know what might have gone wrong with “some json strings”.
Yeah, it’s my fault.
This is the define in configuration.
rest_command:
example_request:
url: ‘http://10.131.253.99:8080/smartLab/paper’
method: post
payload: ‘hello’
This is the call in automation.
- alias: open light when switch off
initial_state: true
trigger:
- platform: state
entity_id: light.gateway_light_7811dcaf1e8a
from: ‘off’
to: ‘on’
action:- service: rest_command.example_request
What i want is when the state of device change (it’s light in this example), hass will send a http request to the server with information about the device (i just used “hello” to test in this example).