leiweibau / Pi.Alert

Scan the devices connected to your WIFI / LAN and alert you the connection of unknown devices. It also warns if a "always connected" device disconnects. In addition, it is possible to check web services for availability. For this purpose HTTP status codes and the response time of the service are evaluated.
https://leiweibau.net
GNU General Public License v3.0
342 stars 25 forks source link

Use API-Call for Home Assistant - Template variable error: 'value_json' is undefined when rendering '{{ value_json.Last_Scan }}' #223

Closed gericho closed 6 months ago

gericho commented 6 months ago

After adding the sensor via the command line platform as the notes found here

I realized running Home Assistant 2023.12.4, the platform must be configured differently to match the new format in particular from this:

sensor:
  - platform: command_line
    name: "PiAlert - Last Scan"
    command: curl -k -X POST -F 'api-key=yourApi-Key' -F 'get=system-status' https://[URL]/api/
    scan_interval: 200
    unique_id: pialert.status.lastscan
    value_template: '{{ value_json.Last_Scan }}'

to this (so maybe the manual needs some changes)

command line:
  - sensor:
      name: "PiAlert - Last Scan"
      command: "curl -k -X POST -F 'api-key=yourApi-Key' -F 'get=system-status' https://[URL]/api/"
      scan_interval: 200
      unique_id: pialert.status.lastscan
      value_template: '{{ value_json.Last_Scan }}'

BTW now all the sensors report UNKNOWN and this is what I found in the HA core log:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   666  100   341  100   325   330k   315k --:--:-- --:--:-- --:--:--  650k
2024-01-03 19:15:59.359 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.Last_Scan }}'

pialert

and the /api/ file reposts 404

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>404 Not Found</title>
 </head>
 <body>
  <h1>404 Not Found</h1>
 </body>
</html>

Besides generating and including the current API key and server local HTTP IP address in the sensor definition, I really don't know how to check if the API is rendering the correct JSON values. May I have some help? Thank you!

leiweibau commented 6 months ago

If you look at the current documentation, you will see that it has already been updated. 😉

https://github.com/leiweibau/Pi.Alert/blob/main/docs/API-USAGE.md

Depending on the system configuration, it may be necessary to specify the path "/pialert" (e.g. http://192.168.0.10 or http://192.168.0.10/pialert/) in the URL in addition to the IP or host name.

Whether "http" or "https" is used depends on the web server configuration.

The documentary you linked to is over a year old.

gericho commented 6 months ago

Thank you for linking the new version, I'm sure I wasted your time but I hope it will help someone

pialert

YES, it works!

So now the API resides in /pialert/api/ and all the sensor must be updated to:

command: "curl -k -X POST -F 'api-key=yourApi-Key' -F 'get=system-status' https://[URL]/pialert/api/"

Do you plan to build a Home Assistant Addon/integration to include Pihole and PiAlert? I'm sure it will help spread this wonderful tool! THANK YOU

leiweibau commented 6 months ago

I'm sure I wasted your

No

Do you plan to build a Home Assistant Addon/integration

I have thought about this, but am not able to implement it at the moment (lack of knowledge).