"majority result" Sensor which decides for "True" when

Hi

Using three different (also remote) weather stations, I constructed virtual sensors that are supposed to predict the probability of fog. They look something like this:

- platform: template
  sensors:
    home_fog_probability_fixed_tolerance_and_fixed_maximum_difference:
      friendly_name: "Nebelwahrscheinlichkeit (Feste Toleranz und feste maximale Differenz)"
      value_template: >
        {% set temperatur = states('sensor.home_temperature') | float %}
        {% set taupunkt = states('sensor.home_dew_point') | float %}
        {% set toleranz_in_grad_celsius = 3.7 %} 
        {{ (temperatur - taupunkt) | abs <= toleranz_in_grad_celsius }}

Now I would like to add a “majority result” which decides for “True” whether at least 2 of the three sensors output “True”. The previous code looks like this:

- platform: template
  sensors:
    majority_result:
      friendly_name: "Mehrheitsergebnis"
      unique_id: majority_result
      value_template: >
        {% set true_count = states('sensor.home_fog_probability_fixed_tolerance_and_fixed_maximum_difference') == 'True' | int + states('sensor.erlinsbach_so_fog_probability_fixed_tolerance_and_fixed_maximum_difference') == 'True' | int + states('sensor.openweathermap_fog_probability_fixed_tolerance_and_fixed_maximum_difference') == 'True' | int %}
        {% set false_count = 3 - true_count %}
        {{ true_count > false_count }}

My problem is that this sensor is never available.

The way you have used int will always cause the template to fail. Filters have precedence over comparisons so int is only being applied to “True”. Since you have not supplied a default for int the function renders an invalid input error.

{% set a_list = ['sensor.home_fog_probability_fixed_tolerance_and_fixed_maximum_difference',
'sensor.erlinsbach_so_fog_probability_fixed_tolerance_and_fixed_maximum_difference',
'sensor.openweathermap_fog_probability_fixed_tolerance_and_fixed_maximum_difference']%}

{% set true_count = a_list | select('is_state', 'True') | list | count %}
{% set false_count = (a_list|count) - true_count %}
{{ true_count > false_count }}
1 Like

Also as these are true/false sensors they should be made as template binary sensors. It will make your life a lot easier. Also you should be using the new template sensor format for new sensors.

configuration.yaml (not sensors.yaml)

template:
  - binary_sensor:
      - name: "Home Nebelwahrscheinlichkeit"
        device_class: smoke
        state: >
          {% set temperatur = states('sensor.home_temperature') | float %}
          {% set taupunkt = states('sensor.home_dew_point') | float %}
          {% set toleranz_in_grad_celsius = 3.7 %} 
          {{ (temperatur - taupunkt) | abs <= toleranz_in_grad_celsius }}
        availability: >
          {{ has_value('sensor.home_temperature') and has_value('sensor.home_dew_point') }}

      - name: "Mehrheitsergebnis"
        device_class: smoke
        state: >
          {% set true_count = states('binary_sensor.home_nebelwahrscheinlichkeit')|bool + states('binary_sensor.erlinsbach_so_nebelwahrscheinlichkeit')|bool + states('sensor.openweathermap_nebelwahrscheinlichkeit')|bool %}
          {{ true_count >= 2 }}
        availability: >
          {{ has_value('binary_sensor.home_nebelwahrscheinlichkeit') and 
          has_value('binary_sensor.erlinsbach_so_nebelwahrscheinlichkeit') and 
          has_value('sensor.openweathermap_nebelwahrscheinlichkeit') }}
1 Like

Great, that works perfectly. Actually, they both work very well, and I could have just pinned yours as a solution. But Tom is right, binary is easier.

1 Like

That makes sense, thank you very much.
Can I create a binary_sensors.yaml with?

binary_sensor: !include binary_sensors.yaml

Yes but not like that. Have a read of this, Splitting config for template: - #44 by tom_l

There are other methods in that topic.

1 Like