Help creating custom entity

I want to create custom entity or custom sensor, which would give a numeric output based on the current month value.

From looking around, this would be the code:

        {% if now().strftime("%m") == "01" %}
          {% set plafond = '340'  %}
        {% elif now().strftime("%m") == "02" %}
          {% set plafond = '280' %}
        {% elif now().strftime("%m") == "03" %}
          {% set plafond = '268' %}
        {% elif now().strftime("%m") == "04" %}
          {% set plafond = '204' %}
        {% elif now().strftime("%m") == "05" %}
          {% set plafond = '181' %}
        {% elif now().strftime("%m") == "06" %}
          {% set plafond = '159' %}
        {% elif now().strftime("%m") == "07" %}
          {% set plafond = '161' %}
        {% elif now().strftime("%m") == "08" %}
          {% set plafond = '176' %}
        {% elif now().strftime("%m") == "09" %}
          {% set plafond = '199' %}
        {% elif now().strftime("%m") == "10" %}
          {% set plafond = '267' %}
        {% elif now().strftime("%m") == "11" %}
          {% set plafond = '306' %}
        {% elif now().strftime("%m") == "12" %}
          {% set plafond = '356' %}
        {% endif %}
        {{ plafond }}

How do I include that in the configuration?

I tried to create something like this:

- platform: template
  sensors:
    price_plafond_electricity
      friendly_name: "Electricity Price Plafond"
      device_class: energy
      unit_of_measurement: "kWh"
      value_template: >
        {% if now().strftime("%m") == "01" %}
          {% set plafond = '340'  %}
        {% elif now().strftime("%m") == "02" %}
          {% set plafond = '280' %}
        {% elif now().strftime("%m") == "03" %}
          {% set plafond = '268' %}
        {% elif now().strftime("%m") == "04" %}
          {% set plafond = '204' %}
        {% elif now().strftime("%m") == "05" %}
          {% set plafond = '181' %}
        {% elif now().strftime("%m") == "06" %}
          {% set plafond = '159' %}
        {% elif now().strftime("%m") == "07" %}
          {% set plafond = '161' %}
        {% elif now().strftime("%m") == "08" %}
          {% set plafond = '176' %}
        {% elif now().strftime("%m") == "09" %}
          {% set plafond = '199' %}
        {% elif now().strftime("%m") == "10" %}
          {% set plafond = '267' %}
        {% elif now().strftime("%m") == "11" %}
          {% set plafond = '306' %}
        {% elif now().strftime("%m") == "12" %}
          {% set plafond = '356' %}
        {% endif %}
        {{ plafond }}

But, I have no idea which YAML configuration. Trial and error lead me to nothing, documentation also quite confusing. Please help.

Fixed by addind this line in the configuration.yaml

template: !include templates.yaml

Then created templates.yaml like this:

I wish there is a newbie guide for this basic coding.

So you already found a way to split the configuration…good, not necessary but good as this is more future proof.
There is little stuff for the newbie’s but the documentation is not that bad at all and provides lots of examples.

Template - Home Assistant (home-assistant.io)

Templating - Home Assistant (home-assistant.io)

1 Like

And jinja is challenging unless you have a deep interest and (not to forget) time…
Above could also be redone as a tuple…
Do not want to scare you off at all but just to show some of the ‘magic’

{% set plafonds = { '01':'340', '02':'280', '03':'268', '04' : '204' } %}
{% set keys = plafonds.keys() | list %}
{% set values = plafonds.values() | list %}
{{ values[keys.index(now().strftime("%m"))] }}

Just put it in Developer Tools > Templates and ‘play around’

1 Like

It is month, from what I read he wants to report back a limit (plafond) for each month.
The use of this in that type of sensor escapes me though but that is highly likely due to being ‘new’ :slight_smile:

1 Like

I think “%m” is Month, not Minute. Unless that’s incorrect and I should use something else. NOTE: I took that code from elsewhere: Energieplafond teller Home Assistant - Smarthome - GoT

Thanks, it looks like I need to do some more reading: Jinja | The Pallets Projects

Jinja is beautiful indeed but it is quite vast too in its applications and timeconsuming if you are not fully into it (myself included). Added to that there is a python element too making it even more powerful (and daunting)
People like e.g. @123 … who reported up there are real masters …

1 Like

You’re right! I should avoid commenting before breakfast! :man_facepalming:t3:

1 Like

If you are interested, you can reduce the template to a single line like this:

- platform: template
  sensors:
    price_plafond_electricity:
      friendly_name: "Electricity Price Plafond"
      device_class: energy
      unit_of_measurement: "kWh"
      value_template: >
        {{ [340, 280, 268, 204, 181, 159, 161, 176, 199, 267, 306, 356][now().month - 1] }}

How it works

This is a list of the desired energy values for each month of the year:

 [340, 280, 268, 204, 181, 159, 161, 176, 199, 267, 306, 356]

There are 12 items in the list and they are indexed starting with zero. That means the first item’s index value is 0, the second item’s index is 1, etc (i.e. from 0 to 11).

To get the first (zeroth) list item by its index value you append [0] to the end of the list. For example, this will report 340.

{{ [340, 280, 268, 204, 181, 159, 161, 176, 199, 267, 306, 356][0] }}

For your application, we want to use the current month as the index value.

{{ [340, 280, 268, 204, 181, 159, 161, 176, 199, 267, 306, 356][now().month - 1] }}

We must subtract 1 from the current month because the list is indexed starting with zero.

2 Likes

Thank you, this is brilliant. I only know PowerShell and Delphi 7 :laughing: