Home Assistant Templates / Statistics and Sensors

 

In addition to the sensors of specific devices, it is possible to create your own sensors in Home Assistant: for example, via the statistics integration or via templates.

Template sensor

Templates allow you to create your own sensors using Jinja2, a full-featured template engine for Python. In a template, data from a wide variety of sensors can be converted, combined, or custom formulas can be added.

In addition to the configuration.yaml file, template sensors can also be created in the management interface, under Settings, Devices & Services / Helpers:

Als Beispiel gibt es in Home Assistant als Status das Datum und die Uhrzeit für den nächsten Sonnenaufgang, in der Form von:  2023-09-12T07:40:21.796007+00:00. Um die Uhrzeit über ein Template zu extrahiert, kann folgender Templatesensor erstellt werden:

{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }}

The wizard already shows a preview with the result.

As an alternative to the administration interface, templates can also be created in the configuraton.yaml file:

sensor:
  - platform: template
    sensors:
      nextsunrise:
        friendly_name: 'Sunrise'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        friendly_name: 'Sunset'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-down
      nextsunsetsunrise:
        friendly_name: 'Sun'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }} - {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up   

New templates can be tested in advance in the GUI with a template editor: "Developer tools" / "Template":

Template examples

what Template
Sunrise
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
Sunset
 {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
Sun
 {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }} - {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
Power (Watt) from current (I) and voltage (U)
{% set V = states('sensor.meter01_voltage_l1') | float %}
{% set A = states('sensor.meter01_current_l1') | float %}
{{ ( 0.9 * A * V) | round(0)}}
Using the current consumption of the heating control - measured with a Zigbee socket - I can determine which action the heating is currently performing.
{% set value = states('sensor.heating_active_power') | float %}
          {{ "off" if value < 10 else 
          "pump" if value < 58 else 
          "water" if value < 70 else 
          "pump" if value < 125 else 
          "heat" }}

Platform Integration

To determine the consumption from an instantaneous value, a so-called platform integration can be used. As an example, from certain consumption values: Watts the complete consumption can be summed up:

sensor:
  - platform: integration
    source: sensor.washingmachine_power
    name: washingmachine_energy_sum
    unit_prefix: k
    unit_time: h
    round: 2  

Consumption is summed:

To relate the summed consumption back to a specific time period, the "Platform Integration" can be used as "utility_meter":

Utility_Meter

The utility meter uses the total consumption and divides it into certain time periods: e.g. hourly or daily:

utility_meter:
  washingmachine_hour:
    source: sensor.washingmachine_energy_sum
    name: washing machine hour
    cycle: hourly  
  washingmachine_daily:
    source: sensor.trockner_energy_sum
    name: washing machine hour
    cycle: daily

Statistics Platform

The Statistics Platform can be used to represent certain historical values, for example the sum of certain values:

  - name: rain_weather_24
    platform: statistics
    entity_id: sensor.rain_weather
    state_characteristic: sum
    max_age:
      hours: 24

sum_differences uses the differences between the values in a continuous counter:

  - name: powerplug_24
    platform: statistics
    entity_id: sensor.powerplug_summation_delivered
    state_characteristic: sum_differences	
    max_age:
      hours: 24
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details