Template based sensor with a trigger

Hi all,

I’m trying to set uyp a trigger based sensor, ideally with some additional attributes. When my HW kicks in HWrun goes from 0 to 1. So I use this as the trigger.

At this point I want to take the starting HW electrical consumption which is stored in sensor.hw_round and ideally some other data stored as attributes with different units of measurement - recording the values of outdoor temperature and HW tank temperature would be desirable in the same sensor rather than creating another one.

I have the following for recording the inital electrical consumption but the HWStart sensor is not being created.

template:
  - trigger:
    - trigger: state
      entity_id: 'counter.hwrun'
      to: 1
    sensor:
        - name: "HWStart"
          state: >-
            "{{ states('sensor.hw_round')}}"            
          unit_of_measurement: kWh

Any pointers in my mistake would be greatly appreciated. Thanks

Did you check the documentation for correct structure? The example they give is:

# Example configuration entry
template:
  - trigger:
      - trigger: 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"

Compare that with your own structure and go from there, provided all your entity_ids are correct?

I tried to! Albeit I was flicking between browser tabs.

I note an extra double space indentation on the second trigger, the sensor/counter ids are correct and render values in the template checker.

I’ve updated to the below, done a yaml restart and still the sensor HWStart is not available as an entity.

template:
  - trigger:
      - trigger: state
        entity_id: 'counter.hwrun'
        to: 1
    sensor:
        - name: "HWStart"
          state: >-
            "{{ states('sensor.hw_round')}}"
          unit_of_measurement: kWh

Check the log file? Probably something in home-assistant.log which normally lives in the same location as your configuration.yaml

Common mistakes when entities don’t appear:

  1. You are pressing “Reload yaml” when creating your first template entity. In order to use the reload button, you need to have at least 1 already configured template entity. After that, you can reload. If it’s your first template entity, you must restart.

  2. You have more than 1 template section, the first is overwriting the second. You’ll have an error in your logs for this.

1 Like

I wonder if the >- after state: might be the issue?

I’ve often found a template needs state: |- or perhaps put the jinja statement on the same line ie

state: "{{ states('sensor.hw_round')}}"

worth a try :man_shrugging:

Further thought, I wonder if it’s the state of the trigger that’s confusing it?

The state is a numeric value so perhaps put it in quotes:

- trigger: state
  entity_id: 'counter.hwrun'
  to: "1"
1 Like

That will be an issue. Good catch. The entity still should shwo up in the system with that error, however it will be unavailable.

Remove the quotes


          state: >-
            {{ states('sensor.hw_round') }}
1 Like

Thanks all. Succes with this.

Is it possible to add attributes with differnet units of measurement?

template:
  - trigger:
      - trigger: state
        entity_id: 'counter.hwrun'
        to: "1"
    sensor:
      - name: "HWStart"
        state: >-
          {{ states('sensor.hw_round')}}
        unit_of_measurement: kWh
1 Like

Just make other sensors, don’t bother with attributes

template:
  - trigger:
      - trigger: state
        entity_id: 'counter.hwrun'
        to: "1"
    sensor:
      - name: "HWStart"
        state: >-
          {{ states('sensor.hw_round')}}
        unit_of_measurement: kWh
      - name: "HWStart2"
        state: >-
          {{ states('sensor.hw_round') }}
        unit_of_measurement: foo
1 Like