Help cleaning the configuration.yaml file

I am still new to HA and my knowledge of the yaml code is still limited. I would be thankful if anyone can help clean the the configuration file by creating yaml files for binary sensors and for other sensors. I tried creating them and included them in the configuration but keeps getting errors.
Here my code in the configuration.yaml:


# Freezer Monitoring
input_boolean:  
  freezer_power_supply_monitor_ib:
    name: Freezer Power Supply Monitor (IB)
    icon: mdi:power-plug-outline
  freezer_unit_monitor_ib:
    name: Freezer Unit Monitor (IB)
    icon: mdi:thermometer-low

# Freezer sensor
template:
  - binary_sensor:
    - unique_id: freezer_running_monitor_bst
      name: Freezer Running Monitor (BST)
      icon: mdi:power
      state: "{{states('sensor.zooz_zen15_power_cord_freezer_electric_consumption_v')|float > 50}}"


 
  - binary_sensor:
      - name: raub_home_wifi_tracker
        state: >
          {% if (states('sensor.raub_ssid') == "Casa") %} or ((states('sensor.raub_ssid') == "Games")) %}
            home
          {% else %}
            not_home
          {% endif %}


  - binary_sensor:
      - name: zak_wifigps
        state: >
          {% if (states('device_tracker.zak') == "Casa") %} or ((states('device_tracker.zak') == "Games")) %}
            home
          {% else %}
            not_home
          {% endif %}

 
  - sensor:
     - name: "Destination address"
       state: >-
          {%- if is_state("input_select.destination", "Home")  -%}
            725 5th Ave, New York, NY 10022, USA
          {%- elif is_state("input_select.destination", "Work")  -%}
            767 5th Ave, New York, NY 10153, USA
          {%- elif is_state("input_select.destination", "Parents")  -%}
            178 Broadway, Brooklyn, NY 11211, USA
          {%- else -%}
            Unknown
          {%- endif %}



sensor:
  - platform: template
    sensors:
      hvac_filter_days_remaining:
        friendly_name: 'HVAC Filter Days remaining'
        value_template: >
          {{  states('input_number.hvac_filter_threshold') | int - (now() - 
          states( 'input_datetime.hvac_filter_last_replaced') 
          | as_datetime | as_local ).days }} 
        icon_template: mdi:clock-end
        unit_of_measurement: 'Days'
  - platform: template
    sensors:
      hvac_filter_days_since_replacement:
        friendly_name: 'HVAC Filter Days Since Replaced'
        value_template: >
          {{ ((as_timestamp(now()) - state_attr('input_datetime.hvac_filter_last_replaced','timestamp')) | int / 86400)  | round(0) -1 }}
        icon_template: mdi:clock-end
        unit_of_measurement: 'Days'






Like I said, I created a binary_sensor.yaml and copied the code posted in it. I included it in configuration using :
binary_sensor: !include binary_sensor.yaml
Thank you in advance!

See here: Can not put "modern style template sensor" in sensors.yaml - #2 by pedolsky

Take care of how your sensors codes are starting with. If the code starts with

binary_sensor:
  - platform: template


or


sensor:
  - platform: template

the code goes into a corresponding file, e.g.

binary_sensor: !include binary_sensor.yaml
sensor: !include my_sensors.yaml

If the code is starting with


template:
  - binary_sensor:

or


template:
  - sensor:

or


template:
  - trigger:
    - … 
    binary_sensor:

the code is included with


template: !include my_template_sensors.yaml (for example)

1 Like

If you would like to split up your configuration file into separate ones you should read this:

There are several directives available for doing so, not just !include. Can use them as you see fit to achieve whatever folder/file system you want.

Alternatively, you can take a look at packages which is the configuration splitting tool of choice for a lot of advanced users on here:

Thank you very much @pedolsky for taking time to explain this to me. Is there a way for me to recode the sensors in my current configuration yaml file so they can be all the same when I create their separate files. Instead of starting with template for example I would rename them binary_sensor?

You already have legacy sensors:


sensor:
  - platform: template
    sensors:
      hvac_filter_days_remaining:
        friendly_name: 'HVAC Filter Days remaining'
        value_template: >
          {{  states('input_number.hvac_filter_threshold') | int - (now() - 
          states( 'input_datetime.hvac_filter_last_replaced') 
          | as_datetime | as_local ).days }} 
        icon_template: mdi:clock-end
        unit_of_measurement: 'Days'
  - platform: template
    sensors:
      hvac_filter_days_since_replacement:
        friendly_name: 'HVAC Filter Days Since Replaced'
        value_template: >
          {{ ((as_timestamp(now()) - state_attr('input_datetime.hvac_filter_last_replaced','timestamp')) | int / 86400)  | round(0) -1 }}
        icon_template: mdi:clock-end
        unit_of_measurement: 'Days'

Feel free to refactor your new-style (=template) sensors.

@pedolsky . I successfully move the template sensors from the configuration file. However, I could not move the legacy sensors and keep getting error missing property “platform” in front sensor key. Could you please assist me in addressing this issue even with rewriting the sensor to meet the modern criteria? Thank you again.

Just my two cents but intentionally switching to something clearly marked legacy in the documentation isn’t really a good idea. Legacy means there are no new features or improvements coming (it’s already missing several key features such as trigger entity support, delay_on/delay_off, etc). It also means at some point in the future it will most likely no longer be supported at all and get removed although there’s no timeline for that date currently.

It seems like a particularly bad idea if you don’t know how to do it yourself as legacy things typically have reduced support. On encountering an issue with a legacy thing usually the first fix suggested is to not use the legacy thing. Legacy things are generally considered advanced for this reason alone.

If your whole goal is simply to put all sensors in one file why not just make a package called sensors and follow the instructions for packages I linked above? You can put config from any number of integrations in a package, doesn’t have to all be under sensor in configiration.yaml.

EDIT: actually just wrote out an example here for this if you are interested

Difficulty to answer as I don’t know how your legacy sensors file looks like and how you included it. What is the error message?

I like @CentralCommand s suggestion. Sure - man’s mind is his kingdom, but I can also only warmly recommend that you take a look at the links.

@CentralCommand . Thank you Mike for taking time to reply to my concern. I agree with you that legacy sensors might not be supported in the future and for this reason, I want to replace the two mentioned here with the modern ones. I tried to modify the example shown at the template page but I got stuck at how at the command state:

# Example configuration entry
template:
  - trigger:
      - platform: time_pattern
        # This will update every night
        hours: 0
        minutes: 0
    sensor:
      # Keep track how many days have past since a date
      - name: "Not smoking"
        state: '{{ ( ( as_timestamp(now()) - as_timestamp(strptime("06.07.2018", "%d.%m.%Y")) ) / 86400 ) | round(default=0) }}'
        unit_of_measurement: "Days"

Thank you again.

It’s everything behind value_template:

state: |-
   {{  states('input_number.hvac_filter_threshold') | int - (now() - states('input_datetime.hvac_filter_last_replaced') | as_datetime | as_local ).days }} 

EDIT: Code not tested

Thank you again @pedolsky . I appreciate that. I will try that some time this weekend.

We will be waiting here. :grinning:

Success! After a few hours of testing and correcting mistakes, I finally converted the two sensors:

  - sensor:
# AC filter days since replaced
      - name: "AC Filter Days Since Replaced"
        unique_id: ac_filter_days_since_replacement
        state: "{{ ((as_timestamp(now()) - state_attr('input_datetime.ac_filter_last_replaced','timestamp')) | int / 86400)  | round(0) -1 }}"
        icon: mdi:clock-end
        unit_of_measurement: "Days"

# AC filter days remaining to change the filter
      - unique_id: ac_filter_days_remaining
        name: AC Filter Days remaining
        icon: mdi:clock-end
        state: "{{  states('input_number.ac_filter_threshold') | int - (now() - states('input_datetime.ac_filter_last_replaced') | as_datetime | as_local ).days }}"
        unit_of_measurement: "Days"  

Thank you guys for your assistance!

1 Like