Configure own sensors

Hi,

New to Home Assistant but I’m trying to get my home battery in the Energy dashboard.

Modified the configuration.yaml as following:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml

Made a sensors.yaml file with the following:

template:
  - sensor:
      - name: Battery Charge Power
        state_class: measurement 
        device_class: power
        unit_of_measurement: W
        state: >
         {% if states('sensor.battery_power') | float(0) >= 0 %}
         {% set gridout = (states('sensor.battery_power') |float |abs) %}
         {% else %}
         {% set gridout = 0 %}
         {% endif %}
         {{ gridout }}
        availability: "{{ has_value('sensor.battery_power') }}"
        
      - name: Battery Discharge Power
        state_class: measurement 
        device_class: power
        unit_of_measurement: W
        state: >
         {% if states('sensor.battery_power') | float(0) <= 0 %}
         {% set gridout = (states('sensor.battery_power') |float |abs) %}
         {% else %}
         {% set gridout = 0 %}
         {% endif %}
         {{ gridout }}
        availability: "{{ has_value('sensor.battery_power') }}"

When I try the code above in the developer tools → template it works.
If I restart HA I always get this message, and the sensors aren’t created:

ERROR (MainThread) [homeassistant.config] Invalid config for ‘sensor’ at configuration.yaml, line 12: required key ‘platform’ not provided, please check the docs at Sensor - Home Assistant

Help would be appreciated :slight_smile:

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

1 Like

In configuration.yaml, add

template: !include template.yaml

then in template.yaml :


  - sensor:
      - name: Battery Charge Power
        state_class: measurement 
        device_class: power
        unit_of_measurement: W
        state: >
         {% if states('sensor.battery_power') | float(0) >= 0 %}
         {% set gridout = (states('sensor.battery_power') |float |abs) %}
         {% else %}
         {% set gridout = 0 %}
         {% endif %}
         {{ gridout }}
        availability: "{{ has_value('sensor.battery_power') }}"
        
      - name: Battery Discharge Power
        state_class: measurement 
        device_class: power
        unit_of_measurement: W
        state: >
         {% if states('sensor.battery_power') | float(0) <= 0 %}
         {% set gridout = (states('sensor.battery_power') |float |abs) %}
         {% else %}
         {% set gridout = 0 %}
         {% endif %}
         {{ gridout }}
        availability: "{{ has_value('sensor.battery_power') }}"

and remove the old ones from sensors.yaml

Thanks Francis!
Is there a guide I can follow before I feel the need to post any more stupid questions? :slight_smile:

Hello WouterMelon,

This is a guide to a lot of things, maybe it will help.