Populate a custom HA string entry from powershell script execution

Hi all,
I’m trying to understand if I can populate a string that I will use as an entity in a dashboard from the result of the execution of a powershell script.
I will try to give you more details of my need :

  • Want to build a dashboard around aviation weather and need to request a dedicated website and its API to do that.
  • I’ve already build a powershell to gather the required information
  • I want now to have HA executing this script each 30 min and update a custom entity in order to reflect modification in my dashboard.

I’m a little lost with the last part of my description : populate the entity with the results of the execution of the powershell script.

Could you please, tell me how I need to implement this ?

without seeing your code i’m admittedly guessing a bit. however one way that i think has high likelihood to do what you want is to use curl to call a webhook that you expose from homeassistant. you can set it the webhook to restrict to local ip’s to help protect security.

if you look here, you’ll see both an example of how to create the webhook as well as how to invoke it from a curl that you can put in your powershell script

Hello,
Sorry for my delayed answer, I was working on my HA challenge :slight_smile:
And I will now post the full answer in case someone want to do a similar thing.

So my initial goal was to build a dashboard showing some meteo messages (METAR, TAF) for airplane pilot needs.

Parts circled in red are dynamics messages updated each 30 or 60 minutes (in the real life) and are available (for example) through the API of this website : https://www.checkwxapi.com

What I have done :

  1. Create a powershell script to execute a request to these API for a specific airport (LFLV or LFLC for examples) and test it outside of HA

  1. Have then to install Powershell in my HA instance (and I lost a lot of time on this because I only found some old post around this, not updated, not compatible with the last version of Alpine distro on HA, …).

a. Use this post to install and configure AddOn “SSH & Web Terminal” on HA : Install PowerShell on Home Assistant – Josh's IT-Blog
(don’t use it to install powershell as it no longer relevant with the last version of HA)

b. From the SSH & Web Terminal execute the following command lines :

sudo su
docker exec -it homeassistant /bin/bash

and then check the distro of your HA instance :

cat /etc/os-release

and finally, create a folder on your HA instance to store your powershell scripts :

mkdir /config/pwsh

c. Install Powershell into the HA docker instance (and not only into the terminal session in order to be persistent over time) :

apk add powershell

(PowerShell 7.4.2-0 has been installed for me as I write this post, for your information)

d. Finally, check that powershell has been well installed into your HA docker instance, lauching powershell :

pwsh

  1. Modify YAML files of your HA in order to references and create your sensors

a. Modify the configuration.yaml file in order to reference the command_line sensors that we will create, adding the following line in the configuration.yaml :

b. Create a new yaml file, command_line.yaml in your HA repository and add your sensor definition, like the following example (share a screenshot in order to maintain indentation) :

You can see that my command call my powershell script, stored in the pwsh folder created previously and with specific parameters to be passed to the script.

(Personal API Key have been modified in the screenshot but you can have one for you from the checkwxapi website)

  1. Make some vérifications in order to check that everything works as it is supposed to

a. You can check that the command line works fine from the “Advanced SSH and Web Terminal”

b. Check HA configuration and then restart your HA instance

Check that there is no error in your yaml files that will prevent HA to restart in the good way and then restart the HA instance so that new sensors and configurations done through yaml files are injected in your HA configuration.

c. Check HA log from the Log Viewer section and you must see something similar to the following screenshot (actually I’m not sure at 100% that it is a good thing that these lines appears in the log file :slight_smile: but seems to be better than an error)

d. Check that the state of the new sensor has been well updated with the expected information

  1. Modify the dashboard in order to include the new sensor and check that this is well updated from the end user perspective.

a. Modification of the dashboard

Add the following yaml code to any HA card so that the sensor value will be integrated in the dashboard

{{ states(‘sensor.aero_meteo_metar_lflc’) }}

b. Final result :

Hope this step by step procedure could help you to build something similar for another personal project. Do not hesitate to share your advices or any suggestions in order to optimize my work, I’m pretty new with HA so I won’t be offended.

Aurélien

[Update]
In order to avoid warning messages in the previous procedure, chapter 4.c, you must adapt the command_line.yaml content like the following one, as we don’t have an answer in a json format when we send the request to the website.

Remove the two commented lines in green for each sensor definition and restart HA. These warning messages will dissapear.