Energy sensor/template

Hi there,

I have a smart plug coming, im planning to connect it to zigbee2mqtt.

I want to create a template which shows the used energy consumed in Euros. Reading a lot of topics…I should be able to get it done, but I’m not sure of a couple things.

-1ste I make a integration called utility_meter, on the entity of the smart plug, but which one? In the documentation I see two options.

  • Power, measurement in W
  • Energy, measurement in kWh

Think I need the"energy"?

2th the integration makes a new sensor, this is the sensor that I need for the template I guess?

3th I have to created a whole new “sensor template” whit the entity of the sensor that the utility_maker created?

The code that I need to change the output from W or kWh to euro… it’s all abba kadabra for me :roll_eyes:.

Do I miss something? Any feedback is appreciated :sweat_smile:
Link for documentation

1 Like

Correct. The one in units of kWh.

Also correct. You can make more than one. So you could have energy per day and energy per month.

Correct. Three in a row! :star:

If you need help with the template sensor just ask.

1 Like

Tom, thank you for the confirmation and the :star: LoL.

So when I make a daily and a monthly, I will get 2 sensors if I’m correct, and need to make 2 templates to display then separately?

The only thing I won’t get is the code… It’s JSON or something :roll_eyes: could you point me in the right direction?

I want to manipulate/change the output from kWh to euro.

Yep all correct again, except it’s Jinja not JSON.

You’ll want something like this for each:

sensor:
  - platform: template
    sensors:
      daily_energy_cost:
        friendly_name: "Daily Energy Cost"
        unit_of_measurement: '$'
        value_template: "{{ states('sensor.your_daily_utiliy_energy_here')|float * states('input_number.cost_per_kwh')|float}}"

The use of an input number allows you to easily adjust the cost per kWh if/when it changes:

cost_per_kwh:
  name: Cost per kWh
  mode: box
  min: 0.00000001
  max: 100.00000001
  unit_of_measurement: "$/kWh"
  icon: mdi:currency-usd

The stupid precision is because of this issue:

1 Like

Thank you again, that Jinja code is what I need , I saw something about “round 2” inside the Jinja code, as far I understand… That is for the decimals?

Is it still needed for the “mode box”? I have a fixed energy plan for years so I guess I won’t need to update it a lot.

For the template, when I add more… For example monthly, I don’t need to start on a new line right? I can start monthly_energy_cost: under the last line?

When the template is done, I can select it as a sensor in UI as a sensor card to show up

Sorry for all this questions, but I want to be prepared… Because of my work, I only have the weekends to short and try this things out.

It is, see updated template below to round to 2 decimal places.

Then you can just replace it with the number (e.g. $0.29 per kWh):

sensor:
  - platform: template
    sensors:
      daily_energy_cost:
        friendly_name: "Daily Energy Cost"
        unit_of_measurement: '$'
        value_template: "{{ ( states('sensor.your_daily_utiliy_energy_here')|float * 0.29 )|round(2) }}"
      monthly_energy_cost:
        friendly_name: "Monthly Energy Cost"
        unit_of_measurement: '$'
        value_template: "{{ ( states('sensor.your_monthly_utiliy_energy_here')|float * 0.29 )|round(2) }}"

See above.

Yes you can.

Wooh great, thank you. . Much appreciated.
Tomorrow I will be home, I dive straight into it!

Thank you.

1 Like

All great information, thank you both!! I want to do something like this one of these days, now I don’t have to figure it out myself.

as promised I’ve dived in to it, everything works fine… but on the template a get a error.

property unit_of_measurment is not allowed.

thus is the code that I have add the moment:

  - platform: template
    sensors:
      daily_energy_cost:
        friendly_name: tv wand daily
        unit_of_measurement: '€'
        value_template: "{{ ( states('sensor.daily_energy')|float * 0.21632 )|round(2) }}"
      monthly_energy_cost:
        friendly_name: tv wand monthly
        unit_of_measurement: '€'
        value_template: "{{ ( states('sensor.monthly_energy')|float * 0.21632 )|round(2) }}"

edit:

I saw I forget the " sensor" on top, I added it. but now I get a error: " missing property “platform”
this is the code right know.

sensor:
  - platform: template
    sensors:
      daily_energy_cost:
        friendly_name: tv wand daily
        unit_of_measurement: 'kWh'
        value_template: "{{ ( states('sensor.daily_energy')|float * 0.21632 )|round(2) }}"
      monthly_energy_cost:
        friendly_name: tv wand monthly
        unit_of_measurement: 'kWh'
        value_template: "{{ ( states('sensor.monthly_energy')|float * 0.21632 )|round(2) }}"

also I changed the unit of measurement to ‘kWh’ I’m not sure what it as to be? € or kWh?

post the full error, not an abridged version

the error is only showing in RED letters in visual studio code, when I copy and paste the code the RED error is not showing her?

is your visual studio code setup to detect errors properly? Personally, I wouldn’t rely on it. That’s what config checker is for. Check your config and see if there’s errors.

Also, when you reply to a person, use the reply on the persons post, not the blue button at the bottom of the topic. Thanks.

@petro

thx for you’re feedback, when I check the config I get the error to. how can I show that error line from the config check?

just copy/paste it

@petro

I hope you mean it like this:

Controleer je configuratie als je onlangs wijzigingen hebt aangebracht en zeker wilt weten dat ze geldig zijn
Ongeldige configuratie
CONTROLEER CONFIGURATIE
Invalid config for [binary_sensor.template]: [unit_of_measurement] is an invalid option for [binary_sensor.template]. Check: binary_sensor.template->sensors->daily_energy_cost->unit_of_measurement. (See ?, line ?).

Yes, it’s telling you that unit_of_measurement is not valid for the binary_sensor section.

@petro

oke, i understand that. my English understanding is better than I can wight it LoL…sorry.
but what’s wrong, I have copied the code that I get from Tom, and replace the stuff that I needed.

in some other topics I saw the same type of measurements put in?

binary_sensor can only be on or off. unit_of_measurement is only valid for sensors, not binary_sensors.

@petro

so do I need to make a separate sensors.yaml !include? and put the template under that one then? will that short it out?