Trying to make http put automation

I am not a programmer, my terms may be wrong… that http command is what I would issue if I were in a browser. I need to send them from HA.

That would be outbound from HA to devices.

Look at the Command line Switch

switch:
  platform: command_line
  switches:
    arest_pin_four:
      command_on: "/usr/bin/curl -X GET http://192.168.1.10/digital/4/1"
      command_off: "/usr/bin/curl -X GET http://192.168.1.10/digital/4/0"
      command_state: "/usr/bin/curl -X GET http://192.168.1.10/digital/4"
      value_template: '{{ value == "1" }}'
      friendly_name: Kitchen Lightswitch

So curl would be installed on a Hassio Pi build? I will look into that, have other command line queries running already.

I see that it is! Will test asap.

Thanks

I think curl is installed already (let me know if I am wrong)!?

It can be run from the command line in teminal, just checked.

So, I have to build a switch? It cannot be called directly as an action from an automation? I guess I expected something like “http publish” or http send" as an actionable item.

Maybe you dont need a switch at all:
Check out the answer from lolouk44 here: Pass arguments to shell_command on event

I DONT KNOW IF HIS CODE WORKS, but it looks like something in the right direction!

/Tonkin

In these cases, I create a shell command that uses cURL:

shell_command:
    my_cmd: ‘/usr/bin/curl http://192.168.1.121/control?cmd=event,Real=65’

This will create a service called shell_command.my_cmd, which you can use in your automations.

and like this if you want to pass a paramater to the call:

shell_command:
  garage_opened: './garage_opened.sh --device="{{ device }}" --door={{ door }}'

automation:
  alias: Garage Door is Open
  trigger:
    platform: state
    entity_id: sensor.garage_door
    state: 'open'
  action:
    service: shell_command.garage_opened
    data:
      device: Garage Door
      door: 1

/T

Ah @bachya… I like they way that looks. Less complex, will try these when I get a chance… Hope to tonight.

I need to pass data from wunderground to the last part. So, would I need some template that inserted

{{ states.sensor.pws_temp_f.state }}

somehow into the command?
I am sure that I could not just do
my_cmd: ‘/usr/bin/curl http://192.168.1.121/control?cmd=event,Real=states.sensor.pws_temp_f.state

Something like this!

shell_command:
  my_cmd: ‘/usr/bin/curl http://192.168.1.121/control?cmd=event,Real={{ state }}’

automation:
  alias: ...
  trigger:
    ...
  action:
    service: shell_command.my_cmd
    data_template:
      data:
        state: {{ states.sensor.pws_temp_f.state }}

Take a look at the links I posted. The describe the same.

The last bit might look like this (I can never figure that part out):

  action:
    service: shell_command.my_cmd
    data_template:
      state: {{ states.sensor.pws_temp_f.state }}

/T

That makes sense… I have had to template a couple of things so far and I just do not understand the process. If I did, I have a feeling it would make my life in HA easier. Just like python, it would help. I do not come from a programming background, networking and infrastructure are where my skills are strongest.

I will throw something like this in HA and report back once it is refined, as I did not find anything like this in my search. Will have a go at the links again, just do not get it. I am getting better at json!

Good luck!
Out of curiosity: What are you trying to pass (pws_temp_f) and for what purpose?

/T

@Tonkin
This:

 action:
   service: shell_command.my_cmd
   data_template:
     state: {{ states.sensor.pws_temp_f.state }}

Basically means: create a temp variable called ‘state’, assign it the value {{ states.sensor.pws_temp_f.state }} and pass it as data to the shell_command.
Your shell command then replaces all instances of {{state}} with its value.

It’s always 50/50 for me - so I posted both :smiley:
Thanks for the clarification!

I have a few Wemos D1’s with tiny OLED’s and SHT30’s in the house. I would like to display basic outside weather data on them. Currently the above action sends out the data via MQTT and then sends it to the display. Under ESPEasy only 1 MQTT import task is allowed with 4 MQTT imported variables. Under previous versions it would work with 2 MQTT import tasks, though not supported, if you put them in the right task slots. That gave me 8 import slots as follows: temp, feelslike, humidity, wind speed, wind gust speed and wind direction and 2 slots left for mqtt borne commands to reboot the unit and to request an ip number update task.

But, now the 2 MQTT tasks are not working under latest versions, so I have to find a different way to populate one unit which will global sync to the other units. More painful config, but right way to do it.

So this automation:

- id: "Broadcast Real Temp HTTP"
  alias: 'Broadcast Real Temp HTTP'
  hide_entity: True
  trigger:
    - platform: time
      minutes: '/1'
      seconds: '0'
  action:
   service: shell_command.wu_temp_http
   data_template:
     state: {{ states.sensor.pws_temp_f.state }}

Results in this error:

invalid key: "OrderedDict([('states.sensor.pws_temp_f.state', None)])" in "/config/automation/http.yaml", line 11, column 0
4:46 PM helpers/entity_component.py (ERROR)

I have tried a few things and just can’t get the automation to load. What is wrong with it? The sensor is there and is named the same… The automations are split out of configuration.yaml, so the format is a tad different.

So, I have the automation loading and calling the service…

- id: "Broadcast Real Temp HTTP"
  alias: 'Broadcast Real Temp HTTP'
  hide_entity: True
  trigger:
    - platform: time
      minutes: '/1'
      seconds: '0'
  action:
    - service: shell_command.wu_temp_http
      data_template:
        state: "{{ states.sensor.pws_temp_f.state }}"

But, I am getting an error running the service call that blows up HA after 3 attempted runs! It will not run from the command line with a value replacing the state part either. The command does work from a browser.
Command line run:

config curl http://192.168.1.121/control?cmd=event,RealTemp=10
zsh: no matches found: http://192.168.1.121/control?cmd=event,RealTemp=10
shell_command:
  wu_temp_http: ‘/usr/bin/curl http://192.168.1.121/control?cmd=event,RealTemp={{ state }}’
2018-03-07 21:54:00 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall shell_command.wu_temp_http: state=30.4>
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1010, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py", line 86, in async_service_handler
    process = yield from create_process
  File "/usr/lib/python3.6/asyncio/subprocess.py", line 225, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "uvloop/loop.pyx", line 2368, in __subprocess_run
  File "uvloop/handles/process.pyx", line 564, in uvloop.loop.UVProcessTransport.new
  File "uvloop/handles/process.pyx", line 87, in uvloop.loop.UVProcess._init
FileNotFoundError: [Errno 2] No such file or directory

So, the above automation and this shell command made it work!

shell_command:
  wu_temp_http: /usr/bin/curl "http://192.168.1.121/control?cmd=event,RealTemp={{state}}"