Compare 2 sensors and create new sensor with highest sensor value

How can I compare 2 actual sensors and select the sensor with the highest value and use this value to create a new virtual sensor.

I haven’t seen an example to add to conf.yaml that covers exactly this.

The actual sensors are updated every 5 sec so the new virtual sensor needs to keep up but I guess that is already part of HA :wink:

P.S. thinking about it again … I realise I better compare the sensors and only if one sensor shows a value of zero it will use the value of the other sensor.

For education I will be interested in both scenarios.

Go to the helpers section an create a template helper.
Open your Home Assistant instance and show your helper entities.

Then put this in as the template (of course replace the entity_id’s in the first lines with your acual entity_id’s):

Option 1: highest value of the 2 sensors

{% set s1 = states('sensor.sensor_1') %}
{% set s2 = states('sensor.sensor_2') %}
{{ [s1, s2] | select('is_number') | map('float') | max | default(none) }}

Option 2: sensor 1 is used unless it isn’t available or equals to 0, then sensor 2 is used.

{% set s1 = states('sensor.sensor_1') | float(0) %}
{% set s2 = states('sensor.sensor_2') | float(none) %}
{{ s1 if s1 > 0 else s2 }}

In both cases it will output none if both sensors are not available.

Thanks, it works perfectly :+1: