Sunday, 2 February 2025

Home Assistant: Multi-Entity Sensor

This post is part of a group of posts all associated with the acquisition of information from external resources and making it available in Home Assistant.

Simple Sensor

In order to set up Home Assistant to collect data at regular intervals from web sites / devices and make the information available, sensors can be used.

This sensor uses a rest mechanism to extract multiple parameters from a single request / call.

Step 1: Include File for Sensor Settings

Update 'configuration.yaml' to include a separate file which will include all of the sensor configurations:

$ sudo vi configuration.yaml

...

rest: !include rest.yaml

 Step 2: Add a Sensor

Create a 'rest.yaml' file alongside the 'configuration.yaml' file

$ vi rest.yaml

- method: GET

  scan_interval: 900
  resource: "https://api.openweathermap.org/data/2.5/weather?lat=51.1789&lon=-1.8262&appid=MySecret"

  sensor: 

  - name: "Test Weather - Deprecated"
    value_template: "{{ value_json['weather'][0]['description'] | default('unknown') }}"
    json_attributes_path: "$.['main']"
    json_attributes:
      - temp
      - feels_like
      - pressure
      - humidity 

  - name: "Test Weather: Description"
    value_template: "{{ value_json['weather'][0]['description'] | default('unknown') }}" 


  - name: "Test Weather: Feels Like"
    value_template: "{{ value_json['main']['feels_like'] - 273.15 }}"
    unit_of_measurement: "°C" 


  - name: "Test Weather: Temperature"
    value_template: "{{ value_json['main']['temp'] - 273.15 }}"
    unit_of_measurement: "°C"

Note: In order to get your own "MySecret", head on over to openweathermap.org and set up a free account.

Hint: The indentation is critical, so in case the indentation above is unclear, see the clarification below:

- method: ...

␠␠scan_interval: ...

␠␠resource: ...

␠␠sensor: 

␠␠␠␠- name: ...

␠␠␠␠value_template: ...

␠␠␠␠json_attributes:

␠␠␠␠␠␠- attribute_1

Step 3: Check Things

Visit your Home Assistant / Developer Tools / Yaml page, and select "check configuration", and if everything is OK, select "Restart".

Once restarted, visit Home Assistant / Settings / Developer Tools / States, and search for "Test Weather" in the list and you should see something similar to the following.


What have we Just Done

We have changed the configuration of Home Assistant to use a separate rest.yaml file - this allows us to add sensors with multiple variables / values.

Every 15 minutes (900 seconds) in this example, a JSON output will be requested from the given resource, and the contents parsed.

We have added variables / values from a single web call in the rest.yaml using two different methods.

The first entry uses a mechanism which is considered deprecated.  This returns the 'description' as the state using the path: {{ value_json['weather'][0]['description'] | default('unknown') }}, and then adds the tempfeels_like, etc. as attributes. 

The next three entries return the same information, but this time, each of the fields is returned as the state. Here, it is possible to process the data before returning it as the state (in this case, the temperature is converted from degrees Kelvin to degrees centigrade), and also possible to return a default value in the event that the connection is not working.

In order to find the correct paths to use with the value_json syntax, it is recommended that you paste your 'resource' url into a web browser, and copy the resulting json into the form at the JSON Path Finder, this will parse the JSON, and allow you to click on a field within the data and extract the corresponding path.



In the above example, the JSON has come from the openweathermap website, and the path for the description is x.weather[0].description.  

This would be translated to value_json["weather"][0]['description'] in the Home Assistant settings.







Saturday, 1 February 2025

Home Assistant: Simple Sensor

This post is part of a group of posts all associated with the acquisition of information from external resources and making it available in Home Assistant.

Simple Sensor

In order to set up Home Assistant to collect data at regular intervals from web sites / devices and make the information available, sensors can be used.

This is a really simple example.

Step 1: Include File for Sensor Settings

Update 'configuration.yaml' to include a separate file which will include all of the sensor configurations:

$ sudo vi configuration.yaml

...

sensor: !include sensors.yaml

Note: This is 'sensor:' and not 'sensors:'

 Step 2: Add a Sensor

Create a 'sensors.yaml' file alongside the 'configuration.yaml' file

$ vi sensors.yaml

- platform: rest
 name: "Json Test - Random Number"
 resource: "http://www.randomnumberapi.com/api/v1.0/randomnumber"
 method: GET
 value_template: "{{ value_json[0] }}"
 scan_interval: 900
Note: The lines are not indented at all

Step 3: Check Things

Visit your Home Assistant / Developer Tools / Yaml page, and select "check configuration", and if everything is OK, select "Restart".

Once restarted, visit Home Assistant / Settings / Device and Services / Entities, and search for "Json Test - Random Number" in the list - select it, and you should see the results (in this case, the random number is 48).


What have we Just Done

We have changed the configuration of Home Assistant to use a separate sensors.yaml file - this allows us to keep all of our sensors together.

We have added a sensor to the sensors.yaml file.

Every 15 minutes (900 seconds) in this example, a JSON file will be extracted from the given resource, and the contents parsed.

The first entry will be pulled out of the json file, and will be used for the "Json Test - Random Number".

Note that this is a really simple example, and the returned JSON is just a list of just one number.

It is likely that you will have a more complex result, and you will need to replace the 'value_json[0]' with a more complex search pattern.

In this case, it is recommended that you paste your 'resource' url into a web browser, and copy the resulting json into the form at the JSON Path Finder, this will parse the JSON, and allow you to click on a field within the data and extract the corresponding path.


In the above example, the JSON has come from the openweathermap website, and the path for the description is x.weather[0].description.  

This would be translated to value_json["weather"][0]['description'] in the Home Assistant settings.