# Deviation of multiple sensors

Hi,

I have 3 illumination sensors pointed in different celestial directions.
Ive noticed on cloudy days they’ll return a very similar value and on sunny days the sensor pointing in direction of the sun will clearly peak above the other two sensors.

I was hoping to uses this to create a “is it sunny or cloudy” sensor by simply looking at the std. deviation of the three values.
I thought to do this with the statistics platform but its entity_id only supports a single entity and compares its values over time. What i need is comparing different sensors at a single point in time.

How do i do that?

You’ll have to use a template and you’ll have to calculate it by hand as there’s no templating function for stdev.

This is a macro in templates for stdev

``````{%- macro stdev(values) %}
{%- set ns = namespace(values=[] ) %}
{%- set mean = values | average %}
{%- for value in values %}
{% set ns.values = ns.values + [ (value - mean)**2 ] %}
{%- endfor %}
{{ sqrt(ns.values | sum / (ns.values | length)) }}
{%- endmacro %}
``````

to use it

``````{% set entities = 'sensor.a', 'sensor.b', 'sensor.c' %}
{% set values = expand(entities) | map(attribute='state') | select('is_number') | map('float') | list %}
{{ stdev(values) }}
``````

putting it all together in a sensor…

``````template:
- sensor:
- name: A B C Standard dev
state: >
{%- macro stdev(values) %}
{%- set ns = namespace(values=[] ) %}
{%- set mean = values | average %}
{%- for value in values %}
{% set ns.values = ns.values + [ (value - mean)**2 ] %}
{%- endfor %}
{{ sqrt(ns.values | sum / (ns.values | length)) }}
{%- endmacro %}

{% set entities = 'sensor.a', 'sensor.b', 'sensor.c' %}
{% set values = expand(entities) | map(attribute='state') | select('is_number') | map('float') | list %}
{{ stdev(values) }}
``````
2 Likes