Templates in different files

HI,
I have a templates.yaml file that is being included in the configuration file.
The code in the templates file is being used to trigger temperature. sensor updates, see below.
How do I add more sensors. The following is not working. If I remove the last 6 lines of code, the temperature sensors work.
thanks

trigger:
  - platform: state
    entity_id: sensor.serial_sensor

sensor:
  - name: Home_1_Temperature
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[5] | float if d | count > 2 and d[1] == '65' and d[2] == '1' else this.state }}
    unit_of_measurement: "°C"
  - name: Home_1_Signal
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[3] | float if d | count > 2 and d[1] == '65' and d[2] == '1'  else this.state }}
    unit_of_measurement: "dB"
  - name: Home_1_Battery
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[4] | float if d | count > 2 and d[1] == '65' and d[2] == '1'  else this.state }}
    unit_of_measurement: "mV"
  - name: Home_2_Temperature
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[5] | float if d | count > 2 and d[1] == '65' and d[2] == '2'  else this.state }}
    unit_of_measurement: "°C"
  - name: Home_2_Signal
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[3] | float if d | count > 2 and d[1] == '65' and d[2] == '2'  else this.state }}
    unit_of_measurement: "dB"
  - name: Home_2_Battery
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[4] | float if d | count > 2 and d[1] == '65' and d[2] == '2'  else this.state }}
    unit_of_measurement: "mV"
  - name: Home_3_Temperature
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[5] | float if d | count > 2 and d[1] == '65' and d[2] == '3'  else this.state }}
    unit_of_measurement: "°C"
  - name: Home_3_Signal
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[3] | float if d | count > 2 and d[1] == '65' and d[2] == '3'  else this.state }}
    unit_of_measurement: "dB"
  - name: Home_3_Battery
    state: >-
      {% set d = trigger.to_state.state.split(':') %}
      {{ d[4] | float if d | count > 2 and d[1] == '65' and d[2] == '3'  else this.state }}
    unit_of_measurement: "mV"
  
sensor
  - name: 'Night State'
    state: "{% if is_state('sun.sun', 'below_horizon') %}1{% else %}0{% endif %}"
      
  - name: 'Sun Angle'
    unit_of_measurement: "°"
    state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"

Use hyphens to separate list items…

- trigger:
    - platform: state
      entity_id: sensor.serial_sensor
  sensor:
    - name: Home_1_Temperature
      ...

- sensor:
    - name: 'Night State'
      ...

Though, I would set up the “Night State” sensor up as a Binary sensor… in the long run it will likely be easier if you use the provided entity types so that you have uniform state values instead of having different sensors represent binary values as “1/0”, “True/False”, “true/false”, “On/Off”, or “on/off”. By using Binary sensors you can be assured that the possible state values will always be “on/off”.

Also note that Digeridrew’s example fixed the indentation of the trigger based template sensor. that whole section needs to be indented more.

The last six lines are outcome-determinative because of a syntax error.

First, you’re missing a : after sensor. Second, you don’t need the sensor: line at all, and when you add a colon (in your version) you’ll end up with an error about duplicate dictionary keys. (That won’t happen with Didgeridrew’s version.). Just keep adding to the list using the same indentation pattern.

I would not use a template sensor or template binary sensor at all. That particular type of sensor is what this integration is for:

Can be set up from the UI or yaml, supports sunrise/sunset with offsets if needed.

2 Likes

Thanks to all replies. They were all very helpful. In the end it was my indentation and misunderstanding of lists. I also like the UI integration from [tom_l].

1 Like