Templates, Legacy vs Current

Hi,

I’ve started rewriting my template sensors, but can’t get this to work.

configuration.yaml:

template: !include_dir_merge_list includes/templates/

Currently, in the folder /includes/templates/ I only at the moment have a single .yaml file (but will be more individual files)

f711b.yaml:

  - sensor:
    - unique_id: zflip_location_state
      name: Siv ZFlip
      state: >
        {%- if states.person.siv.state == 'home' %}
          Hjemme
        {%- endif %}

    - unique_id: zflip_battery_level_power
      name: Strømlader
      state: >
        {%- set power_status = states.binary_sensor.sm_f711b_is_charging.state %}
        {%- if power_status == 'off' %}
          Frakoplet
        {%- else %}
          Tilkoplet
        {%- endif %}
      device_class: battery
      icon: >
        {%- set power_status = states.binary_sensor.sm_f711b_is_charging.state %}
        {%- if power_status == 'off' %}
          mdi:battery-off-outline
        {%- else %}
          mdi:battery-charging-outline
        {%- endif %}  

Anybody spot what I’m doing wrong?

When I test the template in the Template editor, it give me the correct value, but after loading the configuration, I do not get any new sensor named zflip_battery_level_power or stromlader.

None of the two template sensors above work…

Of course, I’ve checked that binary_sensor.sm_f711b_is_charging report anything, which current value off

First time you use template: , you have to restart HA.

Hehe, done several restarts :slight_smile:

It’s going to be named sensor.stromlader.

Ain’t there… Really confused. That’s why I had to raise this here. I’ve even looked at your git @petro and can’t find anything wrong…

Is includes a sub-directory of the directory containing configuration.yaml?

It must be in order for this to work:

Maybe it doesn’t understand:

ø

and replaces it with an underscore?

try looking for a ‘sensor.str*mlader’

Nop, not related.
Even the sensor above in my example above are not created. No error in the logs.

@petro Pls try to help me out here :slight_smile:

I moved it all directly to configuration.yaml and tested this:

template:
  - sensor:
      - unique_id: sm_s908b_battery_level_charging
        name: Ladestatus Stigh
        state: >
          {% set power_charging = states.sensor.sm_s908b_battery_state.state %}
          {% if power_charging == 'discharging' %}
            Frakoplet
          {% else %}
            Lader
          {% endif %}
        device_class: battery
        icon: >
          {% set power_charging = states.sensor.sm_s908b_battery_state.state %}
          {% if power_charging == 'discharging' %}
            mdi:power-plug-off
          {% else %}
            mdi:power-plug
          { % endif %}

I also tried with

template:
  - sensor:
      - unique_id: sm_s908b_battery_level_charging
        name: Ladestatus Stigh
        state: >
          {-% set power_charging = states.sensor.sm_s908b_battery_state.state %}
          {-% if power_charging == 'discharging' %}
            Frakoplet
          {-% else %}
            Lader
          {-% endif %}
        device_class: battery
        icon: >
          {-% set power_charging = states.sensor.sm_s908b_battery_state.state %}
          {-% if power_charging == 'discharging' %}
            mdi:power-plug-off
          {-% else %}
            mdi:power-plug
          {-% endif %}

Checked sensor.sm_s908b_battery_state and it updates when connecting and disconnecting charger.

Still no sensor called ladestatus_stigh

… and no error in the log, even with debug.
I’m out of options…

Well for 1, you can’t use device_class: battery for text based sensors. The battery device class is for % sensors that represent a batteries life from 0 to 100%. You’d see errors in the logs saying “non numerical blah blah”.

TBH, these should be binary_sensors with a device_class: power.

template:
  - binary_sensor:
      - unique_id: sm_s908b_battery_level_charging
        name: Ladestatus Stigh
        state: "{{ not is_state('sensor.sm_s908b_battery_state', 'discharging') }}"
        device_class: power

secondly, make sure you have 1 template section, not 2.

Hi @petro

Can’t I have the template (as earlier) to return a sensor?

What I’m trying are to make a custom state (text) rather than the original istate.
The device class was never intended to be there, frankly I do not know why it was there :rofl:

Only got 1 template section :slight_smile:

You can, but not as device class battery. So just remove that

@petro

Found it!

I’m on my way to reset myself to factory default - so embarrassed…

Hidden in a galaxy far far away, thousands of years ago, there was another template: in configuration.yaml :grinning_face_with_smiling_eyes:

1 Like