Using HVAC state for limiting power calculations

I want to do power calculations based on the state of my heat pump running mode. I have:

  • binary_sensor.heating
  • binary_sensor.cooling
  • sensor.electrity_consumption

How can I calculate power usage when mode has been cooling and when it has been heating? My goal is to use history_stats for daily/weekly/monthly energy consumption for heating and cooling separately. History_stats I already have set up working for total consumption.

Someone able to point me into right direction? Template sensor is needed but I haven’t so far built any by my self and there is a bit of a learning curve still to overcome :slight_smile:

Wouldn’t “monthly” require recorder to store data for over a month? Unless you modify its purge_keep_days option, its default is 10 days of retention.

Describe the calculations you wish to perform.

As I said, I have history_stats sensor up and running. What I am trying to calculate is something like this:

- platform: template
  sensors:
    heating_power:
      friendly_name: 'Heating power'
      unit_of_measurement: 'W'
      value_template: "{{ states('sensor.electricity_consumption') | float * states('binary_sensor.heating') | float }}"

Of course this doesn’t work with multiplying binary_sensor like that. Anyone with more experience?

ps. I just noticed a mistake here in my channel naming but don’t worry about it. It is my translation mistake, my sensors are in my mother tongue but here in English

I got it working. Here is the code I used just if someone else is strugling with similar issue with template sensor
‘’’

  • platform: template
    sensors:
    heating_power:
    friendly_name: ‘Heating power’
    unit_of_measurement: ‘W’

    value_template: >
    
     {% if is_state('binary_sensor.heating', 'True') | float == True %}
       {{ states('sensor.electricity_consumption') }}
     {% else %}
       {{ 0 }}
     {% endif %}
    

‘’’

I was simplier than I initially thought.

The following statement will always evaluate to false.

 {% if is_state('binary_sensor.heating', 'True') | float == True %}

Here’s why:

  • A binary_sensor’s two valid states are on and off (not True and False). Despite that important fact, the template does this:
 is_state('binary_sensor.heating', 'True')
  • Given that the state of a binary_sensor can never be True, the result of the is_state function will always be false.

  • The template proceeds to do something unusual. It uses the float filter to convert the boolean value false to a floating point number. The result of converting boolean false to a floating point number is always 0.0.

  • Finally, it does another unusual thing by comparing 0.0 to the boolean value True. That comparison will always evaluate to false.

  • The end-result is that the statement always evaluates to false.

Now what’s your interpretation of how it works?


EDIT

Perhaps this is what you were aiming for? It reports the sensor’s value only if heating is on otherwise it reports 0.

value_template: >
  {{ states('sensor.electricity_consumption') if is_state('binary_sensor.heating', 'on') else '0' }}

Hi,

Wow, thanks for the very thorough explanation! I think I learned more about templates from your message than from the official documentation! :slight_smile:

Your explanation makes perfect sense and now I am a bit confused here. You are explaining and argumenting well that my code is not working. For some reason it is! I don’t know why or how but I am getting the correct readings for the heating_power sensor. Must be something to do with me copy-pasting the code, sensor definitions etc…

But a curiosity hit my attention here… How come binary sensor can not be anything else than ‘on’ or ‘off’? If I check binary_sensor.heating data in history: I will see only states ‘True’ and ‘False’. And as said, it triggers the the template sensor if sentence correctly.

Your edited code seems much more efficient than mine so I might try that also. It is 4 rows less code :slight_smile:

EDIT: I tested your code. I does not work with ‘on’ state but works with ‘True’ state if modified

Look at the states reported in Developer Tools > States.

For example:
Screenshot from 2020-11-16 13-21-25

If you see true and false there (instead of on and off) then the entity must be a sensor not a binary_sensor. A sensor has more freedom in how it represents its state.

Good for you but the template you posted above will always evaluate to false.

Simple example where a binary_sensor’s state is on and comparing it to True results in the template reporting false.

Screenshot from 2020-11-16 13-30-33

Here is a screenshot:
binary_sensor

Nevermind the different naming of the sensor. This is what I am using in my own code. The sensor is by the way coming directly from ThermIQ device via MQTT.

So a sensor can be called binary_sensor but still be regular sensor with any ‘on’/‘off’ named states?

In the two years of using Home Assistant, that’s the first time I have ever seen a binary_sensor report its state as True/False as opposed to on/off.

Out of curiosity, which version of Home Assistant are you using? Can you post the binary_sensor’s configuration or is created automatically via MQTT Discovery?

Here is the same code as in your preview:

I am using HA supervised, HassOS 4.15, Core 0.117.2. The sensor is generated with MQTT auto-discovery.

Are you using this custom component?

Do all of your binary_sensors report True/False or just the ones associated with ThermIQ?

Yes, that is the component.

Only the ones from ThermIQ. Didn’t actually even notice that before!

Any idea on why it is like this? Something to be worried about? Conflict if future?

That means the ThermIQ custom component does not comply with Home Assistant’s standard for representing a binary_sensor’s state as on/off.

Obviously you will have to adapt the template I posted to accommodate the non-standard usage of True/False.

value_template: >
  {{ states('sensor.electricity_consumption') if is_state('binary_sensor.thermiq_varmvatten', 'True') else '0' }}

What I suggest you do is contact the author of ThermIQ and report the non-standard binary_sensor states. Should the author ever wish to submit the ThermIQ as an official integration, it will have to comply with Home Assistant’s standards. Even if the author has no interest in making it official, it should comply with such a basic standard as on/off for binary_sensors.

Hi Guys, thanks for the feedback. It will be fixed in the near future.

This is now corrected and ThermIQ binary_sensor now use STATE_ON and STATE_OFF to identify its state. ThermIQ is part of HACS default integrations and can be updated via the HACS interface in ha.

I can also confirm that the new update has changed to binary_sensor to on/off. Great work ThermIQ!