Best way to run php scripts

What is the recommended way to run php scrips?

I am an a Mac an I could enable an Apache server with php, but there might be an easier way within HA.

Translating to python is not possible for all my scripts.

Can you run the scripts from cli? In that case it’s I would use the command-line integration.

command_line:
  - sensor:
      command: /usr/bin/php /path/to/php_file.php

No need to run a webserver.

1 Like

Yes I can. Good point.

I will check if that works for me. Thanks.

Ok I ran into some issues. Currently I am using a REST sensor like that:

  - resource: url_to/sensor.php
    scan_interval: 60
    timeout: 45
    sensor:
      - name: "Sensor"
        unit_of_measurement: "%"
        value_template: >-
          {% if value_json is defined %}
              {{ value_json.IR }}
            {% else %}
              {{ "unknown" }}
            {% endif %}

How can I do this with the command_line sensor?

The second problem is probably macOS related, but maybe someone can help.

I installed php with brew install php, since macOS had no php preinstalled.
imagecreatefrompng() returns false, indicating there is an issue. The same script worked before on an external webserver, so I assume the code in the script is correct.

gd 2.3.3_6 is already installed and up-to-date.
PHP 8.3.1 (cli)

Like this:

command_line:
  - sensor:
      command: /usr/bin/php /path/to/php_file.php
      value_template: ... (same as before)
      scan_interval: 60
      name: Sensor

Of course first check if the /usr/bin/php /path/to/php_file.php produces the expected results.

For the PHP installation did you load the right modules?

Thanks, this will work.

Not sure by that. I just installed php and ran the script. I don’t load anything in the script. I simply run this:

$image = imagecreatefrompng("url.png");

With this code it works (adding @). It actually ignores any errors.

@$image = imagecreatefrompng("url.png");

One other question. I have multiple sensors binary and value sensors that get the data from the same php. How can I avoid to run the php multiple times but instead use on call to make all sensors?