Icon Farbwechsel bei einem Stromverbauch >1

Guten Abend.

bin jetzt schon länger am tüfteln, bekomme es aber nicht hin.

Ab einem Verbrauch > 1 Watt soll das Icon von Grün auf Rot wechseln.

Das setze ich mit dem card_mod um, klappt auch sehr gut, nur bei den letzten drei Sensoren beiß ich mir die Zähne aus. es will einfach nicht funktionieren.

Vielleicht hat einer einen Tipp?

type: entities
entities:
  - entity: sensor.boiler_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.boiler_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.shellyplug_eb77f1_waschmaschine_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.shellyplug_eb77f1_waschmaschine_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.trockner_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.trockner_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.kuhlschrank_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.kuhlschrank_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_fernseher_leistung
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_fernseher_leistung.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_media_leistung
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_media_leistung.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_ladestation_leistung
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_ladestation_leistung.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.plusplugs_pi5_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.plusplugs_pi5_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.computer_rpi400_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.computer_rpi400_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.computer_mini_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.computer_mini_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.plusplugs_gateway_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.plusplugs_gateway_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.shelly_plug_s_fur_68_home_assistant_shelly_plug_s_fur_68_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.shelly_plug_s_fur_68_home_assistant_shelly_plug_s_fur_68_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.shellyplug_560005_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.shellyplug_560005_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.tablet_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.tablet_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.computer_usv_neu_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.computer_usv_neu_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: >-
      sensor.shellyplusplugs_08f9e0fcfddc_wohnzimmer_steckleiste_wohnzimmer_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.shellyplusplugs_08f9e0fcfddc_wohnzimmer_steckleiste_wohnzimmer_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_diele_router_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_diele_router_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_esszimmer_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_esszimmer_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_gastezimmer_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_gastezimmer_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.steckdose_schlafzimmer_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.steckdose_schlafzimmer_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.0xa4c138af1da30e9a_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.0xa4c138af1da30e9a_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.0xa4c1388c3bc85219_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.0xa4c1388c3bc85219_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
  - entity: sensor.0xa4c13845a7ae6716_power
    card_mod:
      style: |
        :host {
          {% if ( states.sensor.0xa4c13845a7ae6716_power.state | float == 0 ) %}
          --card-mod-icon-color: red;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}
          }
title: Ernergieverbrauch
state_color: false
show_header_toggle: true

Ist das sicher, dass die drei letzten Entitäten auch wirklich 0,0 W haben? oder sind das gerundete Werte? Offennsichtlich ist die Genauigkeit der Entität auf 0 gestellt… also keine Nachkommastelle. Wird dann vielleicht gerundet und der Wert entspricht nicht wirklich 0,0.

Könnte es möglich sein, dass die Entität nicht so heißt wie du es angegeben hast?

Hast du das mal im Bereich Entwicklerrwerkzeuge überprüft. Ich frage nur, weil gerade bei den letzten die Entity ID so kryptisch ist.

Vielen dank für Deine Antwort.

Bei Entwicklerwerkzeuge kommt folgendes Ergebnis:

Ob jetzt mit Nachkommastelle oder ohne, dann müsste ja wenigstens die Farbe angenommen werden, Grün oder Rot. Und beim letzten Sensor wird ja auch ein Wert angezeigt. Schalten kann ich die Steckdosen ja auch, für mich sehr seltsam.

Gib doch die Abfragen z.B.

          {% if ( states.sensor.0xa4c1388c3bc85219_power.state | float == 0.0 ) %}
          --card-mod-icon-color: green;
          {% else %}
          --card-mod-icon-color: red;
          {% endif %}

mal in den Entwicklerwerkzeugen bei Template ein, was kommt da raus?

Wenn ich das in den Entwicklerwerkzeuge bei Template eingebe, kommt folgende Fehlermeldung:

TemplateSyntaxError: expected token ‘)’, got ‘_power’

Moin, teste mal ob das so funktioniert? Deine letzte Entität hatte übrigens 2x red anstatt 1x green und 1x red eingetragen

- entity: sensor.0xa4c138af1da30e9a_power
  card_mod:
    style: |
      :host {
        {% if states("sensor.0xa4c138af1da30e9a_power") | float == 0.0 %}
        --card-mod-icon-color: green;
        {% else %}
        --card-mod-icon-color: red;
        {% endif %}
      }

- entity: sensor.0xa4c1388c3bc85219_power
  card_mod:
    style: |
      :host {
        {% if states("sensor.0xa4c1388c3bc85219_power") | float == 0.0 %}
        --card-mod-icon-color: green;
        {% else %}
        --card-mod-icon-color: red;
        {% endif %}
      }

- entity: sensor.0xa4c13845a7ae6716_power
  card_mod:
    style: |
      :host {
        {% if states("sensor.0xa4c13845a7ae6716_power") | float == 0.0 %}
        --card-mod-icon-color: green;
        {% else %}
        --card-mod-icon-color: red;
        {% endif %}
      }

Wahnsinn, so funktioniert es.

Vielen Dank für Deine Hilfe.

Zweimal red hatte ich extra eingetragen, um den Wert als Fehler auszuschliessen. Egal welcher Wert, es hätte auf jeden Fall red gezeigt werden.

Aber, warum funktioniert es jetzt. Sind die anderen Einträge von mir jetzt fehlerhaft und sollten geändert werden?

Gruß Wilfried

Die anderen Sensoren von dir haben wahrscheinlich das “state” Atribut. Wenn es funktioniert nichts ändern.

Moin,

Nein, deine bisherigen Einträge sind nicht falsch und müssen nicht geändert werden.
Der Sensor selbst ist völlig in Ordnung.

Das Problem liegt nur daran, dass der Sensorname mit einer Zahl beginnt.
So ein Name kann nicht richtig ausgelesen werden.

Im neuen Code wird der Sensorname als Text übergeben.
Dadurch kann Home Assistant den Sensor korrekt finden und auswerten.

Deshalb funktioniert der neue Code jetzt.

Vielen Dank für die Erklärung.

Vielleicht hast Du noch Zeit um mir bei einem Problem zu helfen.

Bei dieser Übersicht gibt es eine AVM Steckdose die etliche Stunden nicht verfügbar ist, da stromlos.

Dadurch habe ich im Protokoll von Home Assistant eine Fehlermeldung:

Logger: homeassistant.helpers.event
Quelle: helpers/template/__init__.py:463
Erstmals aufgetreten: 09:05:19 (1 Vorkommnis)
Zuletzt protokolliert: 09:05:19

Error while processing template: Template<template=(:host { {% if ( states.sensor.steckdose_fernseher_leistung.state | float == 0.0 ) %} --card-mod-icon-color: green; {% else %} --card-mod-icon-color: red; {% endif %} }) renders=34>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template/__init__.py", line 1551, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unavailable'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template/__init__.py", line 461, in async_render
    render_result = render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template/context.py", line 45, in render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template/__init__.py", line 1554, in forgiving_float_filter
    raise_no_default("float", value)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template/helpers.py", line 24, in raise_no_default
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Template error: float got invalid input 'unavailable' when rendering template ':host {
  {% if ( states.sensor.steckdose_fernseher_leistung.state | float == 0.0 ) %}
  --card-mod-icon-color: green;
  {% else %}
  --card-mod-icon-color: red;
  {% endif %}
  }' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template/__init__.py", line 579, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        variables, strict=strict, log_fn=log_fn, **kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/template/__init__.py", line 463, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unavailable' when rendering template ':host {
  {% if ( states.sensor.steckdose_fernseher_leistung.state | float == 0.0 ) %}
  --card-mod-icon-color: green;
  {% else %}
  --card-mod-icon-color: red;
  {% endif %}
  }' but no default was specified

Ändere einfach

| float == 0.0

in

| float (0) == 0.0

dadurch wird die Leistung falls ‘unavailable‘ als Standardwert ‘0‘ ausgegeben und dein Icon wird grün.

1 „Gefällt mir“