Trying to make http put automation

I currently have the following automation that does an mqtt “broadcast” and I need to convert it to a targeted http put. Really not sure how to pull this off. Looks like the rest api is what I need?

This is used with ESPEasy devices and they are limited to 4 mqtt import tasks and so far the software has worked with 8, but not so going forward.

- id: "Broadcast Real Temp MQTT"
  alias: 'Broadcast Real Temp MQTT'
  hide_entity: True
  trigger:
    - platform: time
      minutes: '/3'
      seconds: '0'
  action:
    service: mqtt.publish
    data_template:
      payload_template: '{{ states.sensor.pws_temp_f.state }}'
      topic: 'Real'

I need to send something like the following:

http://192.168.1.121/control?cmd=event,Real=65

Can you explain why you want to use “PUT” and what you want to accomplish??
The request to /control?cmd… looks like a simple “GET”!?

/Tonk.in

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.