Create integer from attribute using template. Tibber

Hi,

I’m using the Tibber integration that returns electricity price. It has an attribute which gives the price levels in five steps: VERY_CHEAP, CHEAP, NORMAL, EXPENSIVE, VERY_EXPENSIVE

Have been trying to read up on templating how to create a template to turn those into an integer (1 to 5) so I could for example show a meter with severity (green, yellow, red…) but I’m stuck.

Any help would be great.

Here is a template that will set to 0-5 0 meaning unavailable

template:
- sensor:
  - name: "Energy Price"
    unique_id: "energy_price"
    state: >-
      {% if states('sensor.your_price') == "VERY_CHEAP" %}
        1
      {% elif if states('sensor.your_price') == "CHEAP" %}
        2
      {% elif if states('sensor.your_price') == "NORMAL" %}
        3
      {% elif if states('sensor.your_price') == "EXPENSIVE" %}
        4
      {%  elif if states('sensor.your_price') == "VERY_EXPENSIVE" %}
        5
      {% else %}
        0
      {% endif %}

Hi,

Thank you very much! However I tried it in the developer tool/template but don’t understand how to add the attribute. Tried with:

{% if states(‘sensor.electricity_price_tibber_se4’, ‘price_level’’) == “VERY_CHEAP” %} but that won’t work. “price_level” is the attribute .

Have tried state_attr as well but that didn’t work

That template checks for an attribute:

is_state_attr('sensor.your_price', 'price_level', 'VERY_CHEAP')

I don’t understand how to use that.

Tried both state_attr and is_state_attr but don’t understand how to get it into the example AllHailJ gave

Tried this but that won’t work:

template:
- sensor:
  - name: "Energy Price"
    unique_id: "energy_price"
    state: >-
      {% if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "VERY_CHEAP" %}
        1
      {% elif if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "CHEAP" %}
        2
      {% elif if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "NORMAL" %}
        3
      {% elif if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "EXPENSIVE" %}
        4
      {%  elif if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "VERY_EXPENSIVE" %}
        5
      {% else %}
        0
      {% endif %}

Try this

template:
- sensor:
  - name: "Energy Price"
    unique_id: "energy_price"
    state: >-
      {% if state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "VERY_CHEAP" %}
        1
      {% elif state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "CHEAP" %}
        2
      {% elif state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "NORMAL" %}
        3
      {% elif state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "EXPENSIVE" %}
        4
      {% elif state_attr('sensor.electricity_price_tibber_se4', 'price_level') == "VERY_EXPENSIVE" %}
        5
      {% else %}
        0
      {% endif %}
1 Like

That was it. Thank you so much!

That’s what cutting and pasting without thorough check gets you. Sorry for the confusion.