Set current energy cost to X

Hi,

I have a smart power meter and I have set the price per KW/h in energy setup.

My problem is, that the calculated cost begins at zero. I want to set it my own value (in developer tools-> state->set state), but this is not possible (get overwritten asap). I also tested to delete all values from home-assistant_v2.db and insert them how I want them, with the fellow SQL commands (ha was stopped before):

DELETE FROM events WHERE event_id IN (SELECT event_id FROM states WHERE entity_id = 'sensor.pm1_kwh_cost');

DELETE FROM states WHERE entity_id = 'sensor.pm1_kwh_cost';

INSERT INTO "main"."states" ("state_id", "domain", "entity_id", "state", "attributes", "event_id", "last_changed", "last_updated", "created", "old_state_id")
VALUES ('3104525', 'sensor', 'sensor.pm1_kwh_cost', '0', '{"state_class":"total","last_reset":"2021-12-21T12:26:00.000000+00:00","unit_of_measurement":"EUR","device_class":"monetary","friendly_name":"Stromz\u00e4hler Z\u00e4hlerstand Cost"}', '3129327', '2021-12-21 12:26:00.000000', '2021-12-21 12:26:00.000000', '2021-12-21 12:26:00.000000', '');

INSERT INTO "main"."states" ("state_id", "domain", "entity_id", "state", "attributes", "event_id", "last_changed", "last_updated", "created", "old_state_id")
VALUES ('3104528', 'sensor', 'sensor.pm1_kwh_cost', '125.3439', '{"state_class":"total","last_reset":"2021-12-21T12:26:00.000000+00:00","unit_of_measurement":"EUR","device_class":"monetary","friendly_name":"Stromz\u00e4hler Z\u00e4hlerstand Cost"}', '3129330', '2022-01-11 13:25:34.322148', '2022-01-15 17:50:34.322148', '2022-01-15 17:50:34.322148', '3104525');

INSERT INTO "main"."events" ("event_id", "event_type", "event_data", "origin", "time_fired", "created", "context_id", "context_user_id", "context_parent_id")
VALUES ('3129327', 'state_changed', '{}', 'LOCAL', '2021-12-21 12:26:00.000000', '2021-12-21 12:26:00.000000', 'bdb89f1c7c9aa7feeaa32f9ffb982a55', '', '');

INSERT INTO "main"."events" ("event_id", "event_type", "event_data", "origin", "time_fired", "created", "context_id", "context_user_id", "context_parent_id")
VALUES ('3129330', 'state_changed', '{}', 'LOCAL', '2022-01-15 17:50:34.322148', '2022-01-15 17:50:34.322148', '2437ef543a68073be6fe454a3b4301f9', '', '');

I tested with one state/event and also with two… it is always the same. The two state_ids/event_ids are the one I deleted before. I calculated my cost to the date and counter number, which I submitted to my energy provider.

This cost is the only one which is also really interesting, because it is what I will pay at the end.

Has anybody an idea how to do it?

Thanks in Advanced.

Make the current price sensor something you can update, like a webhook or MQTT sensor. Then you can easily update the price. You can’t use developer tools, set state, it will always be overwritten.

I tested to do this:

template:
    sensor:
      - name: "Stromzähler Kosten"
        unique_id: "pm1_kwh_cost"
        state: '{{ ( states("sensor.pm1_kwh") | float - 16893 | float ) * 0.3267 | round(4, default=0) }}'
        unit_of_measurement: "EUR"
        device_class: "monetary"
        state_class: "total_increasing"

But also after a restart from HA the entity is not created - no warning/error in logfile…
Other sensor templates (following the same definition down under this one) are working…

Oh and I removed the setting in energy setup before.
Also after change unique_id to something else and reload templates I also dont get the entity.

any idea?

:thinking:

  - platform: template
    sensors:
      stromzähler_kosten:
        friendly_name: Stromzähler Kosten
        unit_of_measurement: 'EUR'
        state: '{{ ( states("sensor.pm1_kwh") | float - 16893 | float ) * 0.3267 | round(4, default=0) }}'     
        


/
Double name :thinking:
“Stromzähler Kosten” in HA configuration configuration.yaml , sensor.yaml…
/
sensor.licznik_1_energy = My sensor
/

round(4, default=0) }}'  

change to

round(1, default=0) }}'

funny… but I dont wanna change my definition again - this is my complete template.yaml, which works only the pm1_kwh_costs wont:

  sensor:
    - name: "Stromzähler Kosten"
      unique_id: "pm1_kwh_costs"
      state: '{{ (( states("sensor.pm1_kwh") | float - 16893 | float ) * 0.3267) | round(4, default=0) }}'
      unit_of_measurement: "EUR"
      device_class: "monetary"
      state_class: "total_increasing"

    - name: "Licht Eingang Strom"
      unique_id: "licht_eingang_watt"
      state: '{{ states("sensor.licht_eingang_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Eingang Strom"
      unique_id: "steckdose_eingang_watt"
      state: '{{ states("sensor.steckdose_eingang_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Terasse Strom"
      unique_id: "licht_terasse_watt"
      state: '{{ states("sensor.licht_terasse_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Terasse Strom"
      unique_id: "steckdose_terasse_watt"
      state: '{{ states("sensor.steckdose_terasse_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Flur 1 Strom"
      unique_id: "licht_flur_1_watt"
      state: '{{ states("sensor.licht_flur_1_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Flur 2 Strom"
      unique_id: "licht_flur_2_watt"
      state: '{{ states("sensor.licht_flur_2_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Treppenaufgang Strom"
      unique_id: "licht_treppenaufgang_watt"
      state: '{{ states("sensor.licht_treppenaufgang_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Esszimmer 1 Strom"
      unique_id: "licht_esszimmer_1_watt"
      state: '{{ states("sensor.licht_esszimmer_1_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Wohnzimmer Ecke Strom"
      unique_id: "steckdose_wohnzimmer_ecke_watt"
      state: '{{ states("sensor.steckdose_wohnzimmer_ecke_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Wohnzimmer Fenster 1 Strom"
      unique_id: "steckdose_wohnzimmer_fenster_1_watt"
      state: '{{ states("sensor.steckdose_wohnzimmer_fenster_1_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Wohnzimmer Fenster 2 Strom"
      unique_id: "steckdose_wohnzimmer_fenster_2_watt"
      state: '{{ states("sensor.steckdose_wohnzimmer_fenster_2_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Schlafzimmer Strom"
      unique_id: "licht_schlafzimmer_watt"
      state: '{{ states("sensor.licht_schlafzimmer_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Kinderzimmer Strom"
      unique_id: "licht_kinderzimmer_watt"
      state: '{{ states("sensor.licht_kinderzimmer_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Badezimmer Strom"
      unique_id: "licht_badezimmer_watt"
      state: '{{ states("sensor.licht_badezimmer_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht WC Strom"
      unique_id: "licht_wc_watt"
      state: '{{ states("sensor.licht_wc_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Wohnzimmer Oben Strom"
      unique_id: "steckdose_wohnzimmer_oben_watt"
      state: '{{ states("sensor.steckdose_wohnzimmer_oben_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Badezimmer Oben Strom"
      unique_id: "licht_badezimmer_oben_watt"
      state: '{{ states("sensor.licht_badezimmer_oben_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Jake Unten Strom"
      unique_id: "steckdose_jake_unten_watt"
      state: '{{ states("sensor.steckdose_jake_unten_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Jake Oben Strom"
      unique_id: "steckdose_jake_oben_watt"
      state: '{{ states("sensor.steckdose_jake_oben_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Elvin Unten Strom"
      unique_id: "steckdose_elvin_unten_watt"
      state: '{{ states("sensor.steckdose_elvin_unten_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Steckdose Elvin Oben Strom"
      unique_id: "steckdose_elvin_oben_watt"
      state: '{{ states("sensor.steckdose_elvin_oben_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: "Licht Balkon Strom"
      unique_id: "licht_balkon_watt"
      state: '{{ states("sensor.licht_balkon_strom") | float / 1000 | float * 230 * 100 | float / 100 | round(2, default=0) }}'
      unit_of_measurement: "W"

    - name: 'Badezimmerfenster Oben'
      unique_id: "badezimmerfenster_oben"
      state: >-
        {% set open = is_state('binary_sensor.badezimmerfenster_oben_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.badezimmerfenster_oben_gekippt', 'on') %}

        {% if open and tilt %}
          Geöffnet
        {% elif tilt and not open %}
          Gekippt
        {% elif not open and not tilt %}
          Geschlossen
        {% else %}
          Fehler
        {% endif %}      
      icon: >-
        {% set open = is_state('binary_sensor.badezimmerfenster_oben_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.badezimmerfenster_oben_gekippt', 'on') %}

          {% if open and tilt %}
            mdi:window-open
          {% elif tilt and not open %}
            mdi:window-open
          {% elif not open and not tilt %}
            mdi:window-closed
          {% else %}
            mdi:alert-circle-outline
          {% endif %}

    - name: 'Balkontür Jake'
      unique_id: "balkontur_jake"
      state: >-
        {% set open = is_state('binary_sensor.balkontur_jake_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.balkontur_jake_gekippt', 'on') %}

        {% if open and tilt %}
          Geöffnet
        {% elif tilt and not open %}
          Gekippt
        {% elif not open and not tilt %}
          Geschlossen
        {% else %}
          Fehler
        {% endif %}      
      icon: >-
        {% set open = is_state('binary_sensor.balkontur_jake_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.balkontur_jake_gekippt', 'on') %}

          {% if open and tilt %}
            mdi:door-open
          {% elif tilt and not open %}
            mdi:door-open
          {% elif not open and not tilt %}
            mdi:door-closed
          {% else %}
            mdi:alert-circle-outline
          {% endif %}

    - name: 'Balkontür Elvin'
      unique_id: "balkontur_elvin"
      state: >-
        {% set open = is_state('binary_sensor.balkontur_elvin_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.balkontur_elvin_gekippt', 'on') %}

        {% if open and tilt %}
          Geöffnet
        {% elif tilt and not open %}
          Gekippt
        {% elif not open and not tilt %}
          Geschlossen
        {% else %}
          Fehler
        {% endif %}      
      icon: >-
        {% set open = is_state('binary_sensor.balkontur_elvin_offen', 'on') %}
        {% set tilt = is_state('binary_sensor.balkontur_elvin_gekippt', 'on') %}

          {% if open and tilt %}
            mdi:door-open
          {% elif tilt and not open %}
            mdi:door-open
          {% elif not open and not tilt %}
            mdi:door-closed
          {% else %}
            mdi:alert-circle-outline
          {% endif %}

and the calculation can I fix, when I also get an entity… maybe just swapping the from and to counters

Edit: ok, my last counter value 16893 does not match your value… it is the value I submitted to my power service provider…

Thats why u get a crazy result.

I also tested to remove device_class and state_class from the sensor definition… but it not changed anything…

Help :no_mouth:

I am silly :weary:

the unique id is not the entity id… i was searching for pm1 but it is listed as sensor.stromzahler_kosten

I really forgot about that…

1 Like

Back to topic:

Now, I could select the Entity as the cost item, but I get this error:

Translation Error: The intl string context variable “currency” was not provided to the string “Die folgenden Einheiten haben nicht die erwarteten Maßeinheiten ‘’ {currency}/kWh’’, ‘’ {currency}/Wh’’ oder ‘’ {currency}/MWh’’:”

edit: I have selected the wrong option. I set the entity as entity to control current price…

But to select the entity as total cost, it is not listed… maybe I need to wait some time…