From configuration.yaml to template.yaml file

I want to put my sensors in templmate.yaml file but I get a lot off errors.

# EMHASS integreer Solcast Forcast oost en west samengevoegd   

  - platform: rest
    name: "Solcast Forecast Zonnetje Z/O"
    json_attributes:
      - forecasts
    resource: https://api.solcast.com.au/rooftop_sites/2b55-56d8-7046-a4d8/forecasts?format=json&api_key=jKQ62J688md1XW-nSabOg81SZcVAblIQ&hours=24
    method: GET
    value_template: "{{ (value_json.forecasts[0].pv_estimate)|round(2) }}"
    unit_of_measurement: "kW"
    device_class: power
    scan_interval: 86400
    force_update: true

  - platform: rest
    name: "Solcast Forecast Zonnetje Z/W"
    json_attributes:
      - forecasts
    resource: https://api.solcast.com.au/rooftop_sites/521d-2dc0-0583-a262/forecasts?format=json&api_key=jKQ62J688md1XW-nSabOg81SZcVAblIQ&hours=24
    method: GET
    value_template: "{{ (value_json.forecasts[0].pv_estimate)|round(2) }}"
    unit_of_measurement: "kW"
    device_class: power
    scan_interval: 86400
    force_update: true

  - platform: template
    sensors:
      solcast_24hrs_forecast_zonnetje_z_o:
        value_template: >-
          {%- set power = state_attr('sensor.solcast_forecast_zonnetje_z_o', 'forecasts') | map(attribute='pv_estimate') | list %}
          {%- set values_all = namespace(all=[]) %}
          {% for i in range(power | length) %}
            {%- set v = (power[i] | float |multiply(1000) ) | int(0) %}
            {%- set values_all.all = values_all.all + [ v ] %}
          {%- endfor %} {{ (values_all.all)[:48] }}
          
      solcast_24hrs_forecast_zonnetje_z_w:
        value_template: >-
          {%- set power = state_attr('sensor.solcast_forecast_zonnetje_z_w', 'forecasts') | map(attribute='pv_estimate') | list %}
          {%- set values_all = namespace(all=[]) %}
          {% for i in range(power | length) %}
            {%- set v = (power[i] | float |multiply(1000) ) | int(0) %}
            {%- set values_all.all = values_all.all + [ v ] %}
          {%- endfor %} {{ (values_all.all)[:48] }}

      solcast_24hrs_forecast:
        value_template: >-
          {% set a = states("sensor.solcast_24hrs_forecast_zonnetje_z_o")[1:-1].split(',') | map('int') | list %}
          {% set b = states("sensor.solcast_24hrs_forecast_zonnetje_z_w")[1:-1].split(',') | map('int') | list %}
          {% set ns = namespace(items = []) %}
          {% for i in range(a | length) %}
            {% set ns.items = ns.items + [ a[i]  + b[i]  ] %}
          {% endfor %}
          {{ ns.items }}

Logger: homeassistant.config
Bron: config.py:609
First occurred: 09:19:42 (35 gebeurtenissen)
Laatst gelogd: 16:45:47

  • Invalid config for ā€˜templateā€™ at templates.yaml, line 63: ā€˜platformā€™ is an invalid option for ā€˜templateā€™, check: platform Invalid config for ā€˜templateā€™ at templates.yaml, line 64: ā€˜nameā€™ is an invalid option for ā€˜templateā€™, check: name Invalid config for ā€˜templateā€™ at templates.yaml, line 65: ā€˜json_attributesā€™ is an invalid option for ā€˜templateā€™, check: json_attributes Invalid config for ā€˜templateā€™ at templates.yaml, line 67: ā€˜resourceā€™ is an invalid option for ā€˜templateā€™, check: resource Invalid config for ā€˜templateā€™ at templates.yaml, line 68: ā€˜methodā€™ is an invalid option for ā€˜templateā€™, check: method Invalid config for ā€˜templateā€™ at templates.yaml, line 69: ā€˜value_templateā€™ is an invalid option for ā€˜templateā€™, check: value_template Invalid config for ā€˜templateā€™ at templates.yaml, line 70: ā€˜unit_of_measurementā€™ is an invalid option for ā€˜templateā€™, check: unit_of_measurement Invalid config for ā€˜templateā€™ at templates.yaml, line 71: ā€˜device_classā€™ is an invalid option for ā€˜templateā€™, check: device_class Invalid config for ā€˜templateā€™ at templates.yaml, line 72: ā€˜scan_intervalā€™ is an invalid option for ā€˜templateā€™, check: scan_interval Invalid config for ā€˜templateā€™ at templates.yaml, line 73: ā€˜force_updateā€™ is an invalid option for ā€˜templateā€™, check: force_update
  • Invalid config for ā€˜templateā€™ at templates.yaml, line 75: ā€˜platformā€™ is an invalid option for ā€˜templateā€™, check: platform Invalid config for ā€˜templateā€™ at templates.yaml, line 76: ā€˜nameā€™ is an invalid option for ā€˜templateā€™, check: name Invalid config for ā€˜templateā€™ at templates.yaml, line 77: ā€˜json_attributesā€™ is an invalid option for ā€˜templateā€™, check: json_attributes Invalid config for ā€˜templateā€™ at templates.yaml, line 79: ā€˜resourceā€™ is an invalid option for ā€˜templateā€™, check: resource Invalid config for ā€˜templateā€™ at templates.yaml, line 80: ā€˜methodā€™ is an invalid option for ā€˜templateā€™, check: method Invalid config for ā€˜templateā€™ at templates.yaml, line 81: ā€˜value_templateā€™ is an invalid option for ā€˜templateā€™, check: value_template Invalid config for ā€˜templateā€™ at templates.yaml, line 82: ā€˜unit_of_measurementā€™ is an invalid option for ā€˜templateā€™, check: unit_of_measurement Invalid config for ā€˜templateā€™ at templates.yaml, line 83: ā€˜device_classā€™ is an invalid option for ā€˜templateā€™, check: device_class Invalid config for ā€˜templateā€™ at templates.yaml, line 84: ā€˜scan_intervalā€™ is an invalid option for ā€˜templateā€™, check: scan_interval Invalid config for ā€˜templateā€™ at templates.yaml, line 85: ā€˜force_updateā€™ is an invalid option for ā€˜templateā€™, check: force_update
  • Invalid config for ā€˜templateā€™ at templates.yaml, line 87: ā€˜platformā€™ is an invalid option for ā€˜templateā€™, check: platform
  • Invalid config for ā€˜templateā€™ at templates.yaml, line 122: invalid template (TemplateSyntaxError: expected token ā€˜end of print statementā€™, got ā€˜HKZ681ā€™) for dictionary value ā€˜sensors->plate_recognizer->value_templateā€™, got ā€œ{{ state_attr(ā€˜image_processing.platerecognizer_q3538_lve_0ā€™, ā€˜watched_platesā€™).1HKZ681 }}ā€
  • Invalid config for ā€˜templateā€™ at templates.yaml, line 127: invalid template (TemplateSyntaxError: expected token ā€˜end of print statementā€™, got ā€˜EHS016ā€™) for dictionary value ā€˜sensors->plate_recognizer->value_templateā€™, got ā€œ{{ state_attr(ā€˜image_processing.platerecognizer_q3538_lve_0ā€™, ā€˜watched_platesā€™).2EHS016 }}ā€

You can put template sensors in template.yaml, but not your rest sensors.

So I have to put all my sensors back in configuration.yaml file?

Because of : template: !include templates.yaml in the configuration.yaml.

These can go in templates.yaml

These need to stay in configuration.yaml

(or go in rest.yaml )

1 Like

I have a somewhat related question. Has anyone succeeded in putting trigger-based template sensors into an !include file? I already have this:

template:
  - binary_sensor: !include template_binary_sensors.yaml
  - sensor: !include template_sensors.yaml
  - trigger:
      - platform: state
        entity_id: 
     ...

Iā€™ve tried putting the trigger-based sensors in another include file like the other two, but always get errors.

There are a number of options in this topic. Splitting config for template: a lot of false leads too.

I use:

configuration.yaml

template: !include templates.yaml

templates.yaml

- binary_sensor: !include template_binary_sensors.yaml

- image: !include images.yaml

- sensor: !include template_sensors.yaml

#################################################################
# Triggered  Sensors
#################################################################

- trigger:
    - platform: sun
      event: sunrise
      offset: "-00:10:00"
etc...

So triggered template sensors go in templates.yaml but template binary_sensors, sensors and images each get their own files. They start with - name: like this:

template_sensors.yaml

- name: 'Calculated Light Brightness No Late Dim'
  unique_id: ec93bfc9-9b35-4281-a1da-7907469766e7
  unit_of_measurement: "Int"
  state: >
    {{ ( [3, (-0.1785 * states('sensor.outside_light_level')|float(0) + 298.35 )|int(0), 255]|sort )[1] }}
  availability: "{{ states('sensor.outside_light_level')|is_number }}"

- name: 'Calculated Light Brightness'
  unique_id: 844d8510-6871-4449-bead-cad680964594
  icon: "mdi:brightness-auto"
  unit_of_measurement: "Int"
  state: > etc...
3 Likes

Very elegantā€¦ Totally going to steal this when I have time to split config.

Thanks!! This seems like the best approach.

I agree with you about the false leads. Iā€™m pretty sure I followed all of them.

  • platform: rest stays in configuration.yaml ?

You can do

sensor: !include sensor.yaml

and put them in sensor.yaml (and i was wrong, you use old style template sensors, these have to go there too)

Another alternative is to just shove them all into a package.

I have Uber Eats setup similarly where itā€™s just a bunch of rest and template sensors:

homeassistant:
  packages: !include_dir_named packages

Then, just create a folder under in the HA config directory called ā€œpackages.ā€ Drop a YAML in there with all your sensors.

image

Summary
sensor:
  - platform: rest
    name: Uber Eats Orders Count
    resource: https://www.ubereats.com/api/getActiveOrdersV1
    method: POST
    headers:
      Content-Type: application/json
      X-CSRF-Token: x
      Cookie: 'XXX'
    payload: '{"timezone":"America/New_York"}'
    force_update: true
    value_template: >
      {{ value_json.data.orders | count }}
    unit_of_measurement: 'orders'
    scan_interval: 60
  - platform: rest
    name: Uber Eats Orders JSON
    unique_id: uber_eats_orders_json
    json_attributes:
      - feedCards
      - activeOrderOverview
      - contacts
      - backgroundFeedCards
    json_attributes_path: "$.data.orders[0]"
    resource: https://www.ubereats.com/api/getActiveOrdersV1
    method: POST
    headers:
      Content-Type: application/json
      X-CSRF-Token: x
      Cookie: 'XXX'
    payload: '{"timezone":"America/New_York"}'
    force_update: true
    value_template: >-
      {{ now() }}
    scan_interval: 86400
  - platform: template
    sensors:
      uber_eats_order:
        value_template: >-
          {{ now() }}
        attribute_templates:
          eta: >-
            {{ state_attr("sensor.uber_eats_orders_json", "feedCards")[0].status.title if not is_state("sensor.uber_eats_orders_json",'N/A') else "N/A"}}
          order_status_description: >-
            {{ state_attr("sensor.uber_eats_orders_json", "feedCards")[0].status.titleSummary.summary.text if not is_state("sensor.uber_eats_orders_json",'N/A') else "N/A" }}
          order_status: >-
            {{ state_attr("sensor.uber_eats_orders_json", "feedCards")[0].status.currentProgress if not is_state("sensor.uber_eats_orders_json",'N/A') else "N/A" }}  
          restaurant_name: >-
            {{ state_attr("sensor.uber_eats_orders_json", "activeOrderOverview").title if not is_state("sensor.uber_eats_orders_json",'N/A') else "N/A" }}
          latitude: >-
            {{ state_attr("sensor.uber_eats_orders_json", "backgroundFeedCards")[0].mapEntity[0].latitude|float if not is_state("sensor.uber_eats_orders_json", 'N/A') else "N/A" }}
          longitude: >-
            {{ state_attr("sensor.uber_eats_orders_json", "backgroundFeedCards")[0].mapEntity[0].longitude|float if not is_state("sensor.uber_eats_orders_json", 'N/A') else "N/A" }}
      uber_eats_readable_eta:
        friendly_name: 'Uber Eats Order Readable ETA'
        value_template: >
          {% set eta = state_attr("sensor.uber_eats_order","Eta") %}
      uber_eats_status_desc:
        friendly_name: 'Uber Eats Order Status Description'
        value_template: '{{ state_attr("sensor.uber_eats_order","order_status_description") }}'

binary_sensor:
  - platform: template
    sensors:
      uber_eats_has_new_order:
        friendly_name: "Uber Eats has a new order"
        value_template: >
          {% set orders_count = states("sensor.uber_eats_orders_count") | int(0) %}
          {{ orders_count != 'unknown' and orders_count > 0 }}