Syntax in different files, Sensors not shown

Hi,

I am new to HA and I have read a lot of stuff but struggling with some of the stuff.
I organized my configuration.yaml (CY) to have different directories for template, themes, sensors, rest-commands because I believe it is more clearly to have single files for stuff that belongs together each in one directory and I do not want a big file with all e.g. sensors in it.

But now the first thing: I recognized that there is a slightly different syntax between configuration.yaml and a single yaml in the above dirs - why?
You have to look carefully on examples / tutorials, is it for CY or for a single yaml.
It is hard for a newbie.

Than the next big big big thing.

I have some self-made stuff (and in the following pihole) which give me back a result as json.

In case of pihole, you can control some stuff by REST and you can have a status by REST.

So I wrote in CY rest_command: !include_dir_merge_named restcommands

Than in that dir I put a yaml with this

pihole_block_off_10:
  url: "http***/admin/api.php?disable[...]"
  method: post
  content_type: "application/x-www-form-urlencoded"
  verify_ssl: false

ok, I can now have a Button in UI to set this value, it is working.

In a browser I got back: {"status":"disabled"}

How to deal with the result?
How does the sensor has to look like to give this message out in a card?
(Please remember, I will use a single sensor_pihole_disable.yaml for this)

Like above there is another REST comand to get full status:

{
  [...]
  "status": "disabled",
  "gravity_last_updated": {
    "file_exists": true,
    [...]
    "relative": {
      "days": 2,
      "hours": 17,
      "minutes": 36
    }
  }
}

And this is my try to get the field status from status json.

- sensor:
  - name: template_custom_pihole_status
    unique_id: template_custom_pihole_status
    icon: mdi:wall-fire
    state: "{{ state_attr('rest_command.pihole_rest_status', 'status') }}"

And for later, I’d like to integrated a self-made component which gives back e.g. S.0.1.1.0 and how do I split this at the points?

I used OpenHAB before, there was an opportunity to look at the raw-result given back in the log-file. I set logger to debug, but it seemed that I do not see the result itself.
I got this but I can not see the raw result elsewhere:

2023-01-14 09:43:35.143 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [...] Received {'type': 'call_service', 'domain': 'rest_command', 'service': 'pihole_block_off_10', 'target': {}, 'service_data': {}, 'id': 33}
2023-01-14 09:43:35.144 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=rest_command, service=pihole_block_off_10, service_data=>
2023-01-14 09:43:35.149 DEBUG (Recorder) [homeassistant.components.recorder.core] Processing task: EventTask(event=<Event call_service[L]: domain=rest_command, service=pihole_block_off_10, service_data=>)
2023-01-14 09:43:35.308 DEBUG (MainThread) [homeassistant.components.rest_command] Success. Url: http***/admin/api.php?disable[...]. Status code: 200. Payload: None

Amd also a big mystery is the difference between template (template-sensor and sensor-template) and seinsor(s). Can someone explain clearly?

Regards and thanks

I’ll try to help if I can.

It may seem so at first but not really.

all yaml is is just a collection of key:value pairs.

And all of the yaml style configuration starts with configuration.yaml. All the yaml config starts in there. If it’s not included thru the UI then it HAS to be in there for HA to know it exists.

let’s take for example a sensor.

here is a very truncated version of my configuration.yaml with the sensor domain and a few sensors included in it:

homeassistant:
  legacy_templates: false
  name: NUC Docker
  latitude: !secret lat
  longitude: !secret long
  elevation: 866
  packages: !include_dir_named packages
  customize: !include_dir_merge_named customizations/

backup:

# Enables configuration UI
config:

sensor: 
  - platform: derivative
    source: sensor.smoker_food_temperature
    name: Smoker Food Temperature Derivative
    round: 3
    unit_time: min
    time_window: "00:05:00"
    unit: '°F/Min'
    
  - platform: derivative
    source: sensor.smoker_box_temperature
    name: Smoker Box Temperature Derivative
    round: 3
    unit_time: min
    time_window: "00:05:00"
    unit: '°F/Min'
      
  - platform: template
    sensors:
      grill_propane_remaining:
        friendly_name: Grill Propane Net Weight
        value_template: >
          {% if states('sensor.grill_propane_raw_weight') != 'unavailable' %}
            {% set x = (states('sensor.grill_propane_raw_weight') | float(0) - states('input_number.grill_cylinder_tare') | float(0) - 2.8) | round(1) %}
            {{ ([0, x, 100]|sort)[1] }}
          {% else %}
            {{ states('sensor.grill_propane_remaining') }}
          {% endif %}
        icon_template: mdi:propane-tank-outline
        unit_of_measurement: "lb"
      smoker_propane_remaining:
        friendly_name: Smoker Propane Net weight
        value_template: >
          {% if states('sensor.smoker_propane_raw_weight') != 'unavailable' %}
            {% set x = (states('sensor.smoker_propane_raw_weight') | float(0) - states('input_number.smoker_cylinder_tare') | float(0) - 2.6) | round(1) %}
            {{ ([0, x, 100]|sort)[1] }}
          {% else %}
            {{ states('sensor.smoker_propane_remaining') }}
          {% endif %}
        icon_template: mdi:propane-tank-outline
        unit_of_measurement: "lb"

## only used for the testing of the GUI editors
automation: !include automations.yaml

## All of my regular automations go here
automation yaml: !include_dir_merge_list automations/

switch: !include_dir_merge_list switches/
template: !include_dir_merge_list template_sensors/
group: !include_dir_merge_named groups/
script: !include_dir_merge_named scripts/

But if I wanted to split those sensors out to a separate file then I still need the “sensor:” key in the configuration.yaml to tell it where to look:

sensor: !include_dir_merge_list sensors/

## only used for the testing of the GUI editors
automation: !include automations.yaml

## All of my regular automations go here
automation yaml: !include_dir_merge_list automations/

switch: !include_dir_merge_list switches/
template: !include_dir_merge_list template_sensors/

and then move everything after “sensor:” into it’s own file in the “sensors” directory. For example put that code into a file called “smoker.yaml”:

- platform: derivative
    source: sensor.smoker_food_temperature
    name: Smoker Food Temperature Derivative
    round: 3
    unit_time: min
    time_window: "00:05:00"
    unit: '°F/Min'
    
  - platform: derivative
    source: sensor.smoker_box_temperature
    name: Smoker Box Temperature Derivative
    round: 3
    unit_time: min
    time_window: "00:05:00"
    unit: '°F/Min'

notice I didn’t include the “sensor:” at the top of that file since it’s already in the main configuration.yaml file. if I included it here there would be an error returned.

also notice I kept the indentation the same from configuration.yaml to smoker.yaml. While it’s not technically necessary I personally find it easier.

when looking at examples in the forums a lot of users already have moved their sensor config to a different file so they don’t include the “sensor:” key in the example.

hopefully someone else can help with the other question.

Thank you very much

The problem one so often runs into (at the beginning) is exactly what you described to be a “move”.

You don’t move, moving means keeping the code, not being asked to remove 2 chars from each line of the code block, or insert them if reverting your idea to shift code back to the primary file.
And since this is next to an awful task you need an editor which allows block indentation.

Second hurdle, if the strategy within the YAML and it’s split would be consistent fine. You learned the items within an included yaml do start with a "- ", but then you’re wrong again. And you start asking yourself if you missed something since it worked last time.

template: !include template.yaml
mqtt: !include mqtt.yaml

then the template.yaml and mqtt.yaml themselves start like this:

- sensor:
  - unique_id: ...```

while the mqtt starts like this:

button:
  - unique_id: ...

Once you got it, fine, but till that point it’s really frustrating. I still remember my first month with YAML.

and such things don’t easy things for beginners. Once you have a setup and simply add items to existing included files no problem at more, sure.

Not true.

you already have the indentation from the original configuration.yaml file.

you literally just need to copy the whole code block under the “sensor:” key and move it to the new yaml file. No change in indentation is needed.

But not always.

And it has nothing to do with the code being split away from the configuration.yaml file.

some items under different keys are delineated with dashes and some aren’t.

dashes in the code structure define lists.

no dashes define dictionaries.

some integrations (scripts, input_x entities, etc) are named (no dashes just dictionaries).

others (sensors, binary sensors, etc) are lists of dictionaries and require a dash in the beginning of each sensor.

its exactly the same whether it’s in the configuration.yaml or split out from it.

you can also see the difference in the !include for directories. Some need “!include_dir_merge_list” and some others need “!include_dir_merge_named”.

Why this?

And thanks to all.

I have an answer but it’s not very satisfying…

Because it is? :wink:

But seriously I have no idea. That’s been asked before many times but there’s never been an answer given AFAIK. It’s just always been like that.