Heyo. I’m struggling to find a solution for my problem here.
I have 4 monthly cost brackets for electricity. They are:
0 to 100kWh - R2.7033 per kWh
101 to 400kWh - R3.1637 per kWh
401 to 600kWh - R3.4468 per kWh
600+kWh - R3.7158 per kWh
I have a Sonoff DB board meter monitoring the total power coming into the DB board. It gives like energy readings. It also does daily readings but that sensor doesn’t seem to be available in the helpers section. Basically I need the following:
Track monthly kWh coming through the sonoff.
if ((sonoff < 100), then cost = 2.7033)
elif (sonoff > 100 and sonoff < 400, then cost = 3.1637))
etc etc. Where “cost” is a variable I will feed to the energy dashboard to keep track of costs live.
Thanks for the reply. However I’m getting stuck here. The guide you linked says to create a template sensor that exports a string. There are two issues there:
ANOTHER EDIT: I got it working. Will post how when I’ve got a bit more energy lol.
EDIT: I am learning that I don’t really understand how this language works and what I’m actually trying to achieve
The example yaml is not accepted as a template. I changed that to jinja though so that’s all good.
The template sensor option does not allow me to export a string as far as I can tell. Here’s the error: Sensor None has device class 'energy', state class 'total' unit 'kWh' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'blockA' (<class 'str'>)
And here’s my current template block:
{% set energy = states('sensor.sonoff_1001d984af_energy') | float %}
{% if energy <= 100 %}
{{ 'blockA' }}
{% elif energy <= 400 %}
{{ 'blockB' }}
{% elif energy <= 600 %}
{{ 'blockC' }}
{% else %}
{{ 'blockD' }}
{% endif %}
Alternative things I’ve tried:
{% set blockA = input_number.eskom_rates_0_to_100() %}
{% set blockB = input_number.eskom_rates_101_to_400() %}
{% set blockC = input_number.eskom_rates_401_to_650() %}
{% set blockD = input_number.eskom_rates_600() %}
{% set tarrif = 0 %}
{% set energy = states('sensor.sonoff_1001d984af_energy') | float %}
{% if energy <= 100 %}
{{ tarrif == 'blockA' }}
{% elif energy <= 400 %}
{{ tarrif == 'blockB' }}
{% elif energy <= 600 %}
{{ tarrif =='blockC' }}
{% else %}
{{ tarrif =='blockD' }}
{% endif %}
Ayy this one at least gives me a reading. But the reading is “False kWh” lol
{% set tarrif = 0 %}
{% set energy = states('sensor.sonoff_1001d984af_energy') | float %}
{% if energy <= 100 %}
{{ tarrif == 'blockA' }}
{% elif energy > 100 and energy <= 400 %}
{{ tarrif == 'blockB' }}
{% elif energy > 400 and energy <= 600 %}
{{ tarrif =='blockC' }}
{% elif energy > 600 %}
{{ tarrif =='blockD' }}
{% else %}
{{ tarif == 'testing'}}
{% endif %}