Help with passing a state variable to an external python script

I’m trying to do a very simple external python email script and pass a temperature variable to it to be included within the email. I’ve been reading the documentation and searched the forums but not getting very far.

The external python script works as it should. It is simple and grabs the first argument sent to it via the command line and sends it along as part of the body of the email. I’ve tested it and it sends email and include the argument. But how do I do that calling it from Home Assistant?

So if I run the script:

python3 send_email_heat_low.py temperature

The word “temperature” (or whatever argument I use) is successfully picked up and sent in the email.

I believe that I have the correct entity state I’m looking for. I looked at: Developer Tools -. States to find it. sensor.zse40_4_in_1_sensor_air_temperature (which is a Z Wave sensor).

In configuration.yaml I’m using notify: to call the external python script. I’ve tried a bunch of different things to call the script with an argument included (as well as not included). The script only works if I at least include 1 argument. In the below example, the only one that will actually call the script and successfully send email is the the command: not commented out.

configuration.yaml

# name: send_heat_low_email
notify:

  - name: send_heat_low_email
    platform: command_line
    command: "python3 send_email_heat_low.py temperature"
#    command: "python3 send_email_heat_low.py"
#   command: "python3 send_email_heat_low.py {{ states('sensor.zse40_4_in_1_sensor_air_temperature') }}"

In scripts.yaml I have:

send_email_if_heat_to_low:
  alias: Send Email if Heat to low
  sequence:
  - service: notify.send_heat_low_email
    data:
      message: ''
  mode: single

(I have also tried many different things in message but nothing has worked, yet.)

I can include the python script but as stated it works as it should when running it by itself so I don’t think it’s the issue.

I feel like I missing something simple but nothing has worked. Has anyone found a good tutorial on how to do something like this? I’ve been reading the documentation but it hasn’t helped me enough to get this working yet.

So I discovered the use of templating in command line sensors. This almost gets me a step closer but not enough to solve my issue.

Adding the following sensor to my configuration.yaml file actually passes the correct value to my python script, which then in turn emails it to me… unfortunately it does so every minute as there is no logic to determine “when” to send the email. (I only want to send the email if the temperature falls below some value.)

sensor:

  - platform: command_line
    name: low heat email
    command: "python3 send_email_heat_low.py {{ states('sensor.zse40_4_in_1_sensor_air_temperature') }}"
    unit_of_measurement: "temperature"

It’s also a bit unfortunate that you can’t do a similar thing within a notify: in configuration.yaml.

notify:

  - name: send_heat_low_email
    platform: command_line
    command: "python3 send_email_heat_low.py {{ states('sensor.zse40_4_in_1_sensor_air_temperature') }}"

Does NOT result in the same variable being passed to the external python script.

So, if this approach isn’t the right/best way to approach notifications from Home Assistant…

What do people suggest?

I’d like to avoid external cloud services to do this, which is why I went down the path of a Python script. I currently use this method to send the shopping list, as well as access tracking, and it works well. I have it working to send an email if the heat falls below a certain level, but it would be nice to have it include the temp variable from the sensor. I’m just not able to figure out just how to do that.

How do others do this type of thing?

If anyone is interested… I found a solution to this. Though not really ideal.

Because I could pass the entity state via a sensor, I altered the Python script to do all of the logic. Not really ideal but itt does work. I’d prefer to have the logic remain in Home Assistant but at least I found a solution.

So the sensor calls the command line script with the argument being the temperature state. I then check the temperature and if its below a certain value, send the email. All the logic inside the python script.

Again, this works but not really ideal. If anyone has any idea how to do this via notify I’d love to know how to do this. Or if this is the “correct” way to do this, Id love to see feedback telling me that as well.