How to retrieve template variables and forward them into a shell_command

Hi all!

I’m trying to forward a saved temperature state to a webhook so that I can do some further processing with it on another process.

I actually import the value from a MQTT topic which works ok:

# configuration.yml
sensor:
  - platform: mqtt
    name: "Workshop Temp."
    state_topic: "telemetry/temperature/workshop"
    unit_of_measurement: "C"

I can see the temperature in graphs within Home Assistant. What I want to do now is forward that piece of information to my webhook, but only when that value actually changes. I’ve come as far as doing the following:

# configuration.yml:
shell_command:
  webhook_workshop_temp: "/usr/bin/curl -X POST -d 'temp={{ temperature }}' {{ url }}"

# (...)
automation: !include automations.yaml

# automations.yml:
- id: send_interior_temp_to_webhook
  alias: Send interior temp. to webhook
  trigger:
    platform: state
    entity_id: sensor.workshop_temp
  action:
  - service: shell_command.webhook_workshop_temp
    data:
      url: 'https://XXX.YYY.ZZZ/'
      temperature: '{% states.sensor.workshop_temp.state %}'

Which triggers successfully, however in my POST data on the server, this is what I receive:

# PHP script which will do some stuff:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $postData = file_get_contents('php://input');
    file_put_contents(__DIR__ . '/post.txt', print_r($postData, true));
    (...)

$ cat post.txt
temp={% states.sensor.workshop_temp.state %}

I’ve tried calling {{ sensor.workshop_temp }} and some other values but no matter what, I can’t seem to retrieve/forward the actual value. Ideally, if the temperature is 20.4°C I would like it to send “20.4”.

FWIW: I’m running Home Assistant v0.107.1 via Docker on a raspberry pi 4. I did read this guide: https://www.home-assistant.io/docs/automation/trigger/ and some other answer on the internet but couldn’t find my answer there, I’m a bit confused however with the differences between data, data_template, payload and payload_template though…

Thanks and greetings.

  - service: shell_command.webhook_workshop_temp
    data_template:
      url: 'https://XXX.YYY.ZZZ/'
      temperature: "{{ states('sensor.workshop_temp') }}"
2 Likes

That did the job, thank you!

I thought that I had to replace the temperature variable name with the data_template, but that didn’t work either so yeah… there was my confusion.

Everything is working ok now, thanks!

1 Like