Struggling with adding more sensors to configuration

Okay, so HomeAssistant is growing with the addition of solar panels and its integration. Problem I run into is that the integration tosses consumption and production together so it requires adding sensors to the configuration file -and- I already have a template. The template I have is great from help on this forum in re holiday lights

Current yaml template with sensors:

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: livingroom_tree_off
        unique_id: livingroom_tree_off
        state: "{{ (today_at('22:10') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: familyroom_tree_off
        unique_id: familyroom_tree_off
        state: "{{ (today_at('21:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: xmas_front_door_off
        unique_id: xmas_front_door_off
        state: "{{ (today_at('22:20') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: upstairs_wrap_lights
        unique_id: upstairs_wrap_lights
        state: "{{ (today_at('20:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp

I added the Envoy sensors to workaround this problem with the consumption/production miscount:

template:
  - sensor:
      # Generation  to grid
      - name: "Current Power Exporting"
        unique_id: envoy_current_power_exporting
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_production') | int(0) - states('sensor.envoy_current_power_consumption') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"
      # Grid to house
      - name: "Current Power Importing"
        unique_id: envoy_current_power_importing
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_consumption') | int(0) - states('sensor.envoy_current_power_production') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"

I get the following error:

duplicated mapping key (87:1)

84 | state: "{{ (today_at('2 …
85 | device_class: timestamp
86 |
87 | template:
------^
88 | - sensor:

This corresponds to the start of the new template. So I thought well maybe I have to combine them together to be one template. But doing that would mean adding sensors that run on this trigger for holiday lights.

I’m newb-challenged here and just lost on what I should do. All I know is that it seems I need to reproduce this Envoy integration- reversed sign of grid power - #5 by exxamalte into my setup in order to get the proper sensors for the energy dashboard.

Help to resolve greatly appreciated.

As the error message shows, you have duplicated the top-level template key.
Remove the second template:…

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: livingroom_tree_off
        unique_id: livingroom_tree_off
        state: "{{ (today_at('22:10') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: familyroom_tree_off
        unique_id: familyroom_tree_off
        state: "{{ (today_at('21:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: xmas_front_door_off
        unique_id: xmas_front_door_off
        state: "{{ (today_at('22:20') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: upstairs_wrap_lights
        unique_id: upstairs_wrap_lights
        state: "{{ (today_at('20:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp

  - sensor:
      # Generation  to grid
      - name: "Current Power Exporting"
        unique_id: envoy_current_power_exporting
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_production') | int(0) - states('sensor.envoy_current_power_consumption') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"
      # Grid to house
      - name: "Current Power Importing"
        unique_id: envoy_current_power_importing
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_consumption') | int(0) - states('sensor.envoy_current_power_production') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"

Uggggggh I tried that must I must’ve had something else off because the red error notice never went green. Thank you. Now to figure out where to put these last two sensor codes.

Are you asking about additional template sensors…? You just add them under the top-level template: configuration key according to their type. All state-based sensors can go under the same sensor: sub-key. Any trigger-based sensors that use the same set of triggers can be under the same trigger: sub-key.

Example Template Config
template:
  - sensor:
      - name: 
        state: "{{ }}"
      - name: 
        state: "{{ }}"

  # Overnight, Restart, Reload
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: 
        state: "{{ }}"
      - name: 
        state: "{{ }}" 

  # Heating activates
  - trigger:
      - platform: state
        entity_id: climate.hvac
        attribute: hvac_action
        to: 'heating'
    sensor:
      - name: 
        state: "{{ }}"

Non-template or legacy format template sensors need to be placed under a top-level sensor: key, not the template: key shown above. Most of the time this will be either in the configuration.yaml or sensors.yaml file.

I don’t believe these are templated sensors, but the next step in the patchwork solution for Enphase solar panel data is to add this somewhere:

sensor:
  # Aggregating data
  - platform: integration
    method: left
    source: sensor.current_power_importing
    name: Lifetime Imported Energy
    unit_prefix: k
    round: 2
  - platform: integration
    method: left
    source: sensor.current_power_exporting
    name: Lifetime Exported Energy
    unit_prefix: k
    round: 2

“These last two sensors are the one you add to your energy dashboard.” So I don’t know that they would go in the configuration.yaml like that or what. It doesn’t prevent an error but upon reloading I didn’t see it in the energy dashboard to select.

You are right, those are not template sensors. If you have already added other manual sensors to your configuration.yaml file, you will find a top-level sensor key… and you can copy/paste the two integration sensors directly under that. If you haven’t set up other non-template sensor entities in the past all you need to do is copy what you posted above directly into your configuration, making sure that sensor: is all the way to the left. Assuming you put the template sensors in configuration.yaml too it will look something like:

...
# Template Entities
template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: livingroom_tree_off
        unique_id: livingroom_tree_off
        state: "{{ (today_at('22:10') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: familyroom_tree_off
        unique_id: familyroom_tree_off
        state: "{{ (today_at('21:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: xmas_front_door_off
        unique_id: xmas_front_door_off
        state: "{{ (today_at('22:20') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: upstairs_wrap_lights
        unique_id: upstairs_wrap_lights
        state: "{{ (today_at('20:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp

  - sensor:
      # Generation  to grid
      - name: "Current Power Exporting"
        unique_id: envoy_current_power_exporting
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_production') | int(0) - states('sensor.envoy_current_power_consumption') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"
      # Grid to house
      - name: "Current Power Importing"
        unique_id: envoy_current_power_importing
        unit_of_measurement: 'W'
        state: "{{ [0, (states('sensor.envoy_current_power_consumption') | int(0) - states('sensor.envoy_current_power_production') | int(0))] | max }}"
        state_class: "measurement"
        device_class: "power"
        availability: "{{ not is_state('sensor.envoy_current_power_production', 'unavailable') and not is_state('sensor.envoy_current_power_consumption', 'unavailable') }}"



# Sensor Entities (Non-Template)

sensor:
  # Aggregating data
  - platform: integration
    method: left
    source: sensor.current_power_importing
    name: Lifetime Imported Energy
    unit_prefix: k
    round: 2
  - platform: integration
    method: left
    source: sensor.current_power_exporting
    name: Lifetime Exported Energy
    unit_prefix: k
    round: 2
1 Like