How to set sensor or helper value from template

How does one set a Sensor or helper value to the value of “relHumidity” as defined by this?

{% set temperature = states(‘sensor.mainroomtemperaturec’) | float %}
{% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float %}
{% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float %}
{% set absTemperature = temperature + 273.15 %}
{% set relHumidity = absHumidity %}
{% set relHumidity = relHumidity * absTemperature %}
{% set relHumidity = relHumidity / 2.1674 %}
{% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + temperature)) %}
{% set relHumidity = relHumidity / 6.112 %}
{{ relHumidity | round(2) }}

First, make sure that your float filters, have a (0) after them, so they don’t fail if the sensor goes unavailable.

Second, stick it all in a template sensor:

Ok what’s a float filter, and what is the syntax for making it into a template? Like this?

     sensors:
        enteringhumidity:
          unit_of_measurement: '%'
          value_template: {% set temperature = states(‘sensor.mainroomtemperaturec’) | float %}
{% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float %}
{% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float %}
{% set absTemperature = temperature + 273.15 %}
{% set relHumidity = absHumidity %}
{% set relHumidity = relHumidity * absTemperature %}
{% set relHumidity = relHumidity / 2.1674 %}
{% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + temperature)) %}
{% set relHumidity = relHumidity / 6.112 %}
{{ relHumidity | round(2) }}

something is wrong I think. Also can I have two things in the config that say
Sensors: or do I have to put all things under sensors: under just one sensors: thing.

template:
  - sensor:
      - name: enterting_humidity
        unit_of_measurement: '%'
        state: >
          {% set temperature = states(‘sensor.mainroomtemperaturec’) | float(0) %}
          {% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float(0) %}
          {% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float(0) %}
          {% set absTemperature = temperature + 273.15 %}
          {% set relHumidity = absHumidity %}
          {% set relHumidity = relHumidity * absTemperature %}
          {% set relHumidity = relHumidity / 2.1674 %}
          {% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + temperature)) %}
          {% set relHumidity = relHumidity / 6.112 %}
          {{ relHumidity | round(2) }}

To add another sensor, just add it underneath:

    - name: Another sensor
      # all the other stuff
    - name: yet another sensor

So it looks mathematically like it’s saying the sensor is greater than the math I put? What does the > sign actually mean? Is this all part of how the Jinja thing works?

Yes the > tells the config that the multi-line bit that follows should all be treated as one bit, instead of multiple lines.

Ok. So this creates a sensor called entering_humidity? Or is it a device?

And do you know where I can read about how this works? Like a tutorial or videos explaining Jinja

This is where the majority of the documentation is:

But there is something somewhere that explains the difference between: > - and |, from memory >- means that if there are multiple lines of output in the template, line breaks will be ignored so that the template renders as a single line, instead of multiple lines.

Also, i got this in logs and no entering_humidity sensor?
The entity definition format under template: differs from the platform configuration format. See Template - Home Assistant

Nothing else I have under template has name:

template:
  - sensor:
      - name: "carverplace"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'place') }}"

  - sensor:
      - name: "tomplace"
        state: "{{ state_attr('device_tracker.life360_thomas_margotta', 'place') }}"

  - sensor:
      - name: "tomaddress"
        state: "{{ state_attr('device_tracker.life360_thomas_margotta', 'address') }}"

  - sensor:
      - name: "carveraddress"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'address') }}"

  - sensor:
      - name: "carverlatitude"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'latitude') }}"

  - sensor:
      - name: "carverlongitude"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'longitude') }}"

  - sensor:
      - name: "momaddress"
        state: "{{ state_attr('device_tracker.life360_kim', 'address') }}"

  - sensor:
      - name: "momlatitude"
        state: "{{ state_attr('device_tracker.life360_kim', 'latitude') }}"

  - sensor:
      - name: "momlongitude"
        state: "{{ state_attr('device_tracker.life360_kim', 'longitude') }}"
        
  - sensor:
      - name: "insidedoorclosedvoltage"
        state: "{{ state_attr('sensor.lumi_lumi_sensor_magnet_aq2_power', 'battery_voltage') }}"

  - sensors:
        outdoortemperaturec:
          unit_of_measurement: 'C'
          value_template: '{{ "%.2f"|format((( states("sensor.outdoortemperatureecowittgw") | float ) -32) *5/9) }}'

  - sensors:
        mainroomtemperaturec:
          unit_of_measurement: 'C'
          value_template: '{{ "%.2f"|format((( states("sensor.mainroomtemperature1") | float ) -32) *5/9) }}'

  - sensor:
      - name: entering
        unit_of_measurement: '%'
        state: >
          {% set temperature = states(‘sensor.mainroomtemperaturec’) | float(0) %}
          {% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float(0) %}
          {% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float(0) %}
          {% set absTemperature = temperature + 273.15 %}
          {% set relHumidity = absHumidity %}
          {% set relHumidity = relHumidity * absTemperature %}
          {% set relHumidity = relHumidity / 2.1674 %}
          {% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + temperature)) %}
          {% set relHumidity = relHumidity / 6.112 %}
          {{ relHumidity | round(2) }}

It’s the last entry

Edit* crap I just read the last one and I think that’s separating them and causing a problem.

Where have you tried to put it in your configuration yaml, template: is a top level thing, like sensor:, binary_sensor:, light: and switch: it does not go in under sensor:

OK you have 2 sensors in there:

  - sensors:
        outdoortemperaturec:
          unit_of_measurement: 'C'
          value_template: '{{ "%.2f"|format((( states("sensor.outdoortemperatureecowittgw") | float ) -32) *5/9) }}'

  - sensors:
        mainroomtemperaturec:
          unit_of_measurement: 'C'
          value_template: '{{ "%.2f"|format((( states("sensor.mainroomtemperature1") | float ) -32) *5/9) }}'

Which should not be in there, or at least they should be updated to the new template format. They are using the old platform: template format under sensors.

Simply update them to:

  - sensor:
        - name: outdoortemperaturec
          unit_of_measurement: 'C'
          state: '{{ "%.2f"|format((( states("sensor.outdoortemperatureecowittgw") | float(0) ) -32) *5/9) }}'

  - sensor:
        - name: mainroomtemperaturec
          unit_of_measurement: 'C'
          state: '{{ "%.2f"|format((( states("sensor.mainroomtemperature1") | float(0) ) -32) *5/9) }}'

I don’t have time to edit them all now for you, but you should also be aware, that there is no need to have multiple - sensor: lines, taking your first 2 sensors, this would be the format:

template:
  - sensor:
      - name: "carverplace"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'place') }}"

      - name: "tomplace"
        state: "{{ state_attr('device_tracker.life360_thomas_margotta', 'place') }}"

Because they are all of the same type ( sensor)

So I updated the format of those two sensors that still do work but entering_humidity doesn’t appear to be an entity.

This is supposed to make a sensor called entering_humidity with the value of relHumidity after the calculations right?

Ok so I removed the sensor: thing for all of them except the top one but it gave me a message that onto is specifically regarding this thing I added because it references the stuff in it and says it’s related to template

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 28) for dictionary value @ data[‘sensor’][12][‘state’]. Got '{% set temperature = states(‘sensor.mainroomtemperaturec’) | float(0) %} {% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float(0) %} {% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float(0) %} {% set absTemperature = temperature + 273.15 %} {% set relHumidity = absHumidity %} {% set relHumidity = relHumidity * absTemperature %} {% set relHumidity = relHumidity / 2.1674 %} {% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + tempe… (See /config/configuration.yaml, line 18).

I tried with and without the - directly after the > and it said the same thing

template:
  - sensor:
      - name: "carverplace"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'place') }}"

      - name: "tomplace"
        state: "{{ state_attr('device_tracker.life360_thomas_margotta', 'place') }}"

      - name: "tomaddress"
        state: "{{ state_attr('device_tracker.life360_thomas_margotta', 'address') }}"

      - name: "carveraddress"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'address') }}"

      - name: "carverlatitude"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'latitude') }}"

      - name: "carverlongitude"
        state: "{{ state_attr('device_tracker.life360_carver_fell', 'longitude') }}"

      - name: "momaddress"
        state: "{{ state_attr('device_tracker.life360_kim', 'address') }}"

      - name: "momlatitude"
        state: "{{ state_attr('device_tracker.life360_kim', 'latitude') }}"

      - name: "momlongitude"
        state: "{{ state_attr('device_tracker.life360_kim', 'longitude') }}"
        
      - name: "insidedoorclosedvoltage"
        state: "{{ state_attr('sensor.lumi_lumi_sensor_magnet_aq2_power', 'battery_voltage') }}"

      - name: outdoortemperaturec
        unit_of_measurement: 'C'
        state: '{{ "%.2f"|format((( states("sensor.outdoortemperatureecowittgw") | float(0) ) -32) *5/9) }}'

      - name: mainroomtemperaturec
        unit_of_measurement: 'C'
        state: '{{ "%.2f"|format((( states("sensor.mainroomtemperature1") | float(0) ) -32) *5/9) }}'

      - name: entering_humidity
        unit_of_measurement: '%'
        state: >-
          {% set temperature = states(‘sensor.mainroomtemperaturec’) | float(0) %}
          {% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float(0) %}
          {% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float(0) %}
          {% set absTemperature = temperature + 273.15 %}
          {% set relHumidity = absHumidity %}
          {% set relHumidity = relHumidity * absTemperature %}
          {% set relHumidity = relHumidity / 2.1674 %}
          {% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + temperature)) %}
          {% set relHumidity = relHumidity / 6.112 %}
          {{ relHumidity | round(2) }}

So I’m getting this error and I’m not sure what to do to get this to work

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected char '‘' at 28) for dictionary value @ data['sensor'][12]['state']. Got '{% set temperature = states(‘sensor.mainroomtemperaturec’) | float(0) %} {% set relHumidity = states(‘sensor.outdoorhumidityecowittgw’) | float(0) %} {% set absHumidity = states(‘sensor.outdoor_absolutehumidity’) | float(0) %} {% set absTemperature = temperature + 273.15 %} {% set relHumidity = absHumidity %} {% set relHumidity = relHumidity * absTemperature %} {% set relHumidity = relHumidity / 2.1674 %} {% set relHumidity = relHumidity / relHumidity**((17.67 * temperature) / (243.5 + tempe.... (See /config/configuration.yaml, line 18).

It tells you what the issue is:

And where (13th sensor)

You are using fancy quotes, like this:

states(‘sensor.mainroomtemperaturec’)

Change them all to this:

states('sensor.mainroomtemperaturec')

I don’t even think my phone has that lol. I’ll just do it on my laptop when I get home I’m not copy pasting it that many times. I wouldn’t have figured it out lol

The >- vs |- is not really important in a template sensor, since its not shown, but if the code is used in a Lovalace card, then it can have a meaning.
If you use it in a Lovelace card, then you can also add the - to the {%, like {%- and -%}. to remove line breaks.

What is the purpose of defining relHumidity if you just overwrite it with absHumidity before using it in any calculations…