Error rendering state template for sensor.sg_ready_mode: TypeError: '>=' not supported between instances of 'str' and 'float'

I’ve recurring log errors but I have no idea how to fix.

Logger: homeassistant.helpers.sensor
Source: helpers/trigger_template_entity.py:209
First occurred: 13:00:00 (60 occurrences)
Last logged: 13:59:00

Error rendering state template for sensor.sg_ready_mode: TypeError: '>=' not supported between instances of 'str' and 'float'

  - trigger:
      - platform: time_pattern
        minutes: "/1"
    action:
      - variables:
          t: "{{ now().isoformat() }}"
          var: >-
            {% from '032-emhass.jinja' import globalVariables %}
            {{ globalVariables()|from_json }}
          epexspot_quantile_forecast: "{{ states('sensor.epex_spot_data_net_quantile_forcast')|float(0) }}"
          grid_power: "{{ states('sensor.power_meter_active_power')|float(0) }}"
          EVU: "{{ states('switch.406_wp_sg_ready_evu') }}"
          PV: "{{ states('switch.406_wp_sg_ready_pv_ma') }}"
    sensor:
      - name: "sg-ready-mode"
        unique_id: sg-ready-mode
        state: >-
          {% if EVU == 'on' and PV == 'off' %}
            {{ 'EVU lock' }}
          {% elif EVU == 'off' and PV == 'on' %}
            {{ 'Recommendation' }}
          {% elif EVU == 'on' and PV == 'on' %}
            {{ 'Turn On' }}
          {% else %}
            {{ 'Normal' }}
          {% endif %}
        attributes:
          mode: >-
            {% if epexspot_quantile_forecast >= var.epexspot_quantile_setpoint_high
                and grid_power <= 1000 %}
              {{ 'EVU lock' }}
            {% else %}
              {{ 'Normal' }}
            {% endif %}
          evu_lock: "{{ EVU }}"
          pv_ready: "{{ PV }}"
          last_updated: "{{ t }}"
        availability: >-
          {{ states('switch.406_wp_sg_ready_evu')
              and states('switch.406_wp_sg_ready_pv_ma') }}

In template editor I can’t reproduce the error:

{% from '032-emhass.jinja' import globalVariables %}
{% set var = globalVariables()|from_json %}
{% set t = now().isoformat() %}
{% set epexspot_quantile_forecast = states('sensor.epex_spot_data_net_quantile_forcast')|float(0) %}
{% set grid_power = states('sensor.power_meter_active_power')|float(0) %}
{% set EVU = states('switch.406_wp_sg_ready_evu') %}
{% set PV = states('switch.406_wp_sg_ready_pv_ma') %}

{% if EVU == 'on' and PV == 'off' %}
  {{ 'EVU lock' }}
{% elif EVU == 'off' and PV == 'on' %}
  {{ 'Recommendation' }}
{% elif EVU == 'on' and PV == 'on' %}
  {{ 'Turn On' }}
{% else %}
  {{ 'Normal' }}
{% endif %}

{% if epexspot_quantile_forecast >= var.epexspot_quantile_setpoint_high
      and grid_power <= 1000 %}
  {{ 'EVU lock' }}
{% else %}
  {{ 'Normal' }}
{% endif %}

{{ var }}
{{ var.epexspot_quantile_setpoint_high }}
{{ epexspot_quantile_forecast }}
{{ grid_power }}
{{ EVU }}
{{ PV }}
{{ t }}
Normal



  Normal


{'inverter_rated_power': 6000.0, 'inverter_maximum_active_power': 6600.0, 'batteries_rated_capacity': 15.0, 'batteries_rated_charge_energy': 17.25, 'batteries_rated_discharge_power': 6600.0, 'batteries_rated_charge_power': 7500.0, 'batteries_charge_end_time_expected': '14:00', 'batteries_soc_setpoint_low': 15.0, 'batteries_soc_setpoint_neutral': 45.0, 'batteries_soc_setpoint_high': 75.0, 'epexspot_quantile_setpoint_low': 0.15, 'epexspot_quantile_setpoint_neutral': 0.33, 'epexspot_quantile_setpoint_high': 0.9}
0.9
0.0
4199.0
off
off
2024-07-30T14:19:37.170981+02:00

The automation on top of the sensor:

alias: Heating Pump - SG-Ready controller
description: "Heating Pump: SG-Ready controller."
trigger:
  - platform: time_pattern
    minutes: /1
    enabled: true
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.sg_ready_mode
            attribute: mode
            state: EVU lock
        sequence:
          - if:
              - condition: state
                entity_id: switch.406_wp_sg_ready_evu
                state: "off"
            then:
              - service: switch.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: switch.406_wp_sg_ready_evu
          - if:
              - condition: state
                entity_id: switch.406_wp_sg_ready_pv_ma
                state: "on"
            then:
              - service: switch.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: switch.406_wp_sg_ready_pv_ma
          - if:
              - condition: state
                entity_id: switch.heizung_domestic_hot_water_0_boost
                state: "on"
            then:
              - service: switch.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: switch.heizung_domestic_hot_water_0_boost
    default:
      - if:
          - condition: state
            entity_id: switch.406_wp_sg_ready_evu
            state: "on"
        then:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.406_wp_sg_ready_evu
      - if:
          - condition: state
            entity_id: switch.406_wp_sg_ready_pv_ma
            state: "on"
        then:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.406_wp_sg_ready_pv_ma
      - if:
          - condition: state
            entity_id: switch.heizung_domestic_hot_water_0_boost
            state: "on"
        then:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.heizung_domestic_hot_water_0_boost
mode: single

This is probably the issue.
Later you use:

which is the only place I can find >=.
Make sure this value does not become unavailable or unknown in your macro

Content of custom jinja template:

{%- macro globalVariables() %}
    {# 1 x Huawei SUN2000-6KTL-M1 (High Current Version) #}
    {# 1 x Huawei LUNA2000-10-S0 #}
    {# 1 x Huawei LUNA2000-5-S0 #}
    {{- {
            'inverter_rated_power': 6000.0,
            'inverter_maximum_active_power': 6600.0,
            'batteries_rated_capacity': 15.0,
            'batteries_rated_charge_energy': 17.250,
            'batteries_rated_discharge_power': 6600.0,
            'batteries_rated_charge_power': 7500.0,
            'batteries_charge_end_time_expected': '14:00',
            'batteries_soc_setpoint_low': 15.0,
            'batteries_soc_setpoint_neutral': 45.0,
            'batteries_soc_setpoint_high': 75.0,
            'epexspot_quantile_setpoint_low': 0.15,
            'epexspot_quantile_setpoint_neutral': 0.33,
            'epexspot_quantile_setpoint_high': 0.9
        }|to_json -}}
{%- endmacro %}

String… You can’t have a string there

I don’t understand:

{% from '032-emhass.jinja' import globalVariables %}
{% set var = globalVariables()|from_json %}
{{ var }}
{
  "inverter_rated_power": 6000,
  "inverter_maximum_active_power": 6600,
  "batteries_rated_capacity": 15,
  "batteries_rated_charge_energy": 17.25,
  "batteries_rated_discharge_power": 6600,
  "batteries_rated_charge_power": 7500,
  "batteries_charge_end_time_expected": "14:00",
  "batteries_soc_setpoint_low": 15,
  "batteries_soc_setpoint_neutral": 45,
  "batteries_soc_setpoint_high": 75,
  "epexspot_quantile_setpoint_low": 0.15,
  "epexspot_quantile_setpoint_neutral": 0.33,
  "epexspot_quantile_setpoint_high": 0.9
}
{% from '032-emhass.jinja' import globalVariables %}
{% set var = globalVariables()|from_json %}
var.batteries_charge_end_time_expected: {{ var.batteries_charge_end_time_expected }}
var.epexspot_quantile_setpoint_high:  {{ var.epexspot_quantile_setpoint_high }}
var.batteries_charge_end_time_expected: 14:00
var.epexspot_quantile_setpoint_high:  0.9

The template in general is working as expected - but recurring errors - but the strange thing only on at some point of time

I doubt this is the cause of the problem but let’s officially eliminate this unlikely possibility …

Do you have the following custom integration installed?

It’s used to create entities whose domain is var and serve as global variables. For example, var.color, var.intensity, etc.

Now this is going to be quite a stretch but perhaps Home Assistant occasionally confuses the meaning of var.epexspot_quantile_setpoint_high as being an entity belonging to that custom integration as opposed to a Jinja2 variable.

(Grasping at straws)

1 Like

I’ve NOT installed this custom integration.

1 Like

OK, good. The unlikely scenario is decisively eliminated.


FWIW, the order of the types reported in the error message is significant.

'>=' not supported between instances of 'str' and 'float'

It means it encountered a numeric comparison between a string and float. As already mentioned, the only comparison involving >= is this one:

epexspot_quantile_forecast >= var.epexspot_quantile_setpoint_high

Given the order of the reported types, string and float, means that epexspot_quantile_forecast had a string value.

Which seems impossible because this template ensures the value is float.

{% set epexspot_quantile_forecast = states('sensor.epex_spot_data_net_quantile_forcast')|float(0) %}

Something has happened this noon:

Which, of course, doesn’t explain why the error message complains the lefthand value in the numeric comparison is a string.

Very strange.

I am looking this over, and wanted to remind everyone that everything returned from a
custom macro is a string. Everything.

That is likely a string.

1 Like

I’m hoping you can help me understand one particular aspect of this Trigger-based Template Sensor’s operation.

It’s regarding this availability template:

        availability: >-
          {{ states('switch.406_wp_sg_ready_evu')
              and states('switch.406_wp_sg_ready_pv_ma') }}
  • If both switches are on then the template’s result is on, the sensor is available and its state template will be evaluated.

  • If either switch is off then the template’s result is off, the sensor is unavailable and its state template will not be evaluated.

So both switches must be on in order for the state template to be evaluated. However, the state template is making decisions based on whether those same switches are either on or off.

How is it able to check if a switch is off when the availability template only allows evaluation when both switches are on?

True but the from_json filter is being used to convert it to a dictionary.

Potentially the availability template doesn’t make sense any more and I should remove this old stuff.

But as you can see above, there was no change on both of the switches during the issue …

Sure but my question was about when things are operating normally.

The Template Sensor’s availability template only allows the state template to be evaluated when both switches are on yet the state template contains code that checks if they are on or off. It will never get an off state because the availability template (as its currently written) will not allow it.

I agree … or make the availability template confirm that both switches have nominal values (on or off) and not unavailable or unknown.

        availability: >-
          {{ has_value('switch.406_wp_sg_ready_evu')
              and has_value('switch.406_wp_sg_ready_pv_ma') }}

{{ states('switch.406_wp_sg_ready_evu') and states('switch.406_wp_sg_ready_pv_ma') }}

off

As you are writing above I was also on the way to understand the documentation properly …

Conclusion: My old availibility template needs to be removed!
Thanks for chaising me into the right direction!

I will adapt my configs and monitor and report.

I suggest you use the template I posted above (the one that employs has_value).

If you simply remove the availability option and one of the two switches is unavailable, it will cause your state template to produce an incorrect misleading result.

Assume the first switch’s state is neither on or off but unavailable or unknown.

          EVU: "{{ states('switch.406_wp_sg_ready_evu') }}"
          PV: "{{ states('switch.406_wp_sg_ready_pv_ma') }}"

It will cause the state template to report Normal

        state: >-
          {% if EVU == 'on' and PV == 'off' %}
            {{ 'EVU lock' }}
          {% elif EVU == 'off' and PV == 'on' %}
            {{ 'Recommendation' }}
          {% elif EVU == 'on' and PV == 'on' %}
            {{ 'Turn On' }}
          {% else %}
            {{ 'Normal' }}
          {% endif %}

So your choices are either to use an appropriate availability template (like what I suggested above) or to enhance the state template to handle non-nominal switch values.