First Sensor Tutorial

Hi,
I’m new to HA and wish to create a sensor that will use a json file as it’s input.
I’ve read articles about creating the sensor (new directory ‘sensor’, add a yaml file, etc) and have done that.
I restart HA but there is no sign of the new sensor anywhere, nor can I see any errors but tbh I’m probably not looking in the right place.

Is there a detailed guide, head to toe, of how to write a sensor, add it to a dashboard, test it via a console, etc please?

There are lots of snippets of info within this community but when starting out I really need a guide to help get me started.

Thanks

Check the Log for related errors and post the sensor configuration you created.

The documentation shows how to compose a Template Sensor’s YAML configuration.

Entities are automatically added to the auto-generated dashboard. If you have a created a custom dashboard, you must manually add the entity using whichever card you prefer.

The only part of a Template Sensor’s configuration you can test in the Template Editor is the template.

Thanks. The Logbook simply shows the state of my mobile phone by the looks of it. There’s no mention of the new sensor.

Share your sensor config.

The Log, not the Logbook.

To configuration.yml I’ve added:

sensor: !include_dir_merge_list sensor/

In sensor/oil_price.yml:

  - platform: file
    name: "Oil Price price per litre"
    file_path: /share/oil_price.json
    value_template: '{{ value_json.pence_per_litre }}'

share/oil_price.json:

{
  "pence_per_litre": 76
}

I’ve found the Log and there are no errors in there relating to this new sensor

Did you add /share to the allowlist_external_dirs option?

No I hadn’t done that - thanks.

homeassistant:
  allowlist_external_dirs:
    - "/share"

I’ve now added it, checked the config, reloaded the config, and checked Log but still no mention of the sensor. It also doesn’t show in the dropdown list when I edit a Dashboard.

Just for good measure, restart Home Assistant after adding the allowlist_external_dirs option.

After it restarts, go to Developer Tools > States and look for sensor.oil_price_price_per_litre.

Thanks Taras, restarted the entire box but still no mention in the Log or the sensor showing in the dropdown.
I’m suspecting that it’s not loading the new config files - is there any way of checking what its loading?

Found the issue: It’s ignoring the yaml file because I come from a Ruby world and had used a .yml suffix.
It’s now loading the file and failing with a config error.

Thanks for your patience - in the words of Arnie “I’ll be back” I’m sure lol

I had considered asking you if that was the actual extension or just a typo during the transcription from your config to the forum post.

What is the error message?

The error was to do with json. Ive changed the type to .txt (data file) for now and can read the sensor.

The next issue is on the sensor card in my trial Dashboard the lines plots upside down - as the sensor returns a higher value the line descends, lower values and it rises.

I’m going to see if I need to set the device_class to monatory next to fix it.

Thanks again.

Json sorted but the chart is still inverted - except when you click it to see the history. That popup chart is just what I would like in the card but there doesn’t seem to be one available built in.

Is it possible to have the history style of chart on the card please?

The chart is showing an initial price of 95 followed by a change to 69.

(as a new user I can only upload one image)

Screenshot 2023-09-20 at 19.52.23

All sorted - changed to a ‘history’ chart and it’s just what I was after. :slight_smile: