Converting Sensor Measurement Units

I have a Airthings Wave Radon Detector, which is reporting Radon values in units Bq/M^3 via MQTT to HA. Unfortunately, in the US, Radon levels are read in units of PCi/L. Converting from Bq/M^3 to PCi/L is quite easy, just divide by 37. How can I add this conversion in HA so that PCi/L is displayed?

I’ve tried the following via templating, but continue to encounter errors:

sensor.radon_24hrsUS would be the new entity that I am trying to create

sensor.radon_24hrs is the current entity that is reporting units in Bq/M^3

sensor:
  platform: template
  sensors:
    sensor.radon_24hrsUS:
    unit_of_measurement: 'pCi/L'
    value_template: {{ (float(states('sensor.radon_24hrs')) * 1 / 37) }}
1 Like

Always those dreadful value_templates :slight_smile:

Try the following:
value_template: “{{(states.sensor.radon.state | float * 1/37)}}”

You don’t say what the errors are, but you might have a couple problems unrelated to the value template (it looks fine to me): 1) the yaml formatting has incorrect indentation, 2) entity names can only be lowercase but you have uppercase.

Try this:

sensor:
  - platform: template
    sensors:
      sensor.radon_24hrs_us:
        unit_of_measurement: 'pCi/L'
        value_template: {{ float(states('sensor.radon_24hrs')) / 37 }}

As for the value template, these all seem to be the same, so take your pick. In templates I find the filter versions easier to read since it separates the logic to get the sensor state and the conversion.

{{ float(states('sensor.radon_24hrs')) / 37) }}
{{ float(states('sensor.radon_24hrs')) * 1 / 37 }}
{{ states('sensor.radon_24hrs') | float / 37) }}
{{ states('sensor.radon_24hrs') | float * 1 / 37) }}

Thanks for the help fversteegen & freshcoast!

BTW - Completely forgot about the capital letters!

I tried the entry below:

sensor:
  platform: template
  sensors:
    sensor.radon_24hrs_us:
      unit_of_measurement: 'pCi/L'
      value_template: {{(states.sensor.radon_24hrs.state | float * 1/37)}}

But receive the error;

Error loading /home/homeassistant/.homeassistant/configuration.yaml: invalid key: “OrderedDict([(’(states.sensor.radon_24hrs.state | float * 1/37)’, None)])”
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 323, column 0

You had a number of things wrong.

  1. platforms under sensor require a list. So you need list indicator.
  2. You made the sensor entity_id invalid by adding a period in it. Not sure why you named it sensor.radon_24hrs_us. Just name it radon_24hrs_us and it will be created as sensor.radon_24hrs_us.
  3. Value template requires quotes when it’s a single line
  4. You should be using the states method so that it properly gives you a result at all times (even during startup).

Just a few pointers:

  1. Order of operation matters, because of this filters don’t always need parenthesis.
  2. 1/37 is eqivalent to dividing by 37. No reason to make the parser work harder to get to your answer.
sensor:
  - platform: template
    sensors:
      radon_24hrs_us:
        unit_of_measurement: 'pCi/L'
        value_template: "{{ states('sensor.radon_24hrs') | float / 37 }}" 
1 Like

Whoops, copy and paste errors on my part. Thanks for the correction @petro. :+1:

Ah yeah, I didn’t even notice your post. I think you just had an extra ), no biggie. EDIT: Did i reply to you with that post? Meant to reply to @curt7000

Thanks for the tips @petro, its now working! I added some rounding, the confirmed working config is below. I broke up my sensors to individual components a long way back as listing them was causing a bunch of errors. I need to merge them.

sensor 15:
  platform: template
  sensors:
    radon_24hrs_us:
      unit_of_measurement: 'pCi/L'
      value_template: "{{ (states('sensor.radon_24hrs') | float / 37) | round(2) }}"

sensor 16:
  platform: template
  sensors:
    radon_lifetime_us:
      unit_of_measurement: 'pCi/L'
      value_template: "{{ (states('sensor.radon_lifetime') | float / 37) | round(2) }}"

Yes … Radon mitigation is in my future.

1 Like

I also have some 2nd Gen Airthings sensors but connected through their modern cloud API… it’s great by the way… I highly recommend their View Plus hardware.

Anyway, wanted to update the template syntax to help out the other Home Assistant Newbs out there since the above syntax seems to be outdated and doesn’t work as of 2022.

template:
  - sensor:
      - name: "Airthings Radon US"
        unit_of_measurement: "pCi/L"
        state: "{{ (states('sensor.airthings_radon') | float / 37) | round(2) }}"

Also for the newbs, this text gets saved into your configuration.yaml file. After a system restart, you should have a new sensor show up.

You can also test these template outputs in real-time without restarting by using the ‘Template Editor’ in the ‘Developer Tools’ sidebar, then copy the test to your configuration.yaml file after you’ve confirmed it works in the Template Editor.

Cheers

9 Likes

Thank you qwertyphile!!! This was awesome and worked like a charm. The last note about where to put this entry has solved a long standing issue for me. I haven’t been able to figure out template entries.

Thank you so much for thinking of us newbies!

This worked perfectly for me as well. Thank you!

This is great, thank you!

So glad I found this as an Airthings user in the US… but new to HA… Where do I drop this ? IN configuration.yaml?

did you ever figure this out? I’ve tried adding it to the configuration.yaml as well as creating a jinja file in the custom_templates folder, but neither seem to work.

It doesn’t go in custom_templates, it goes in configuration.yaml.

If that didn’t work, post your errors in your logs.

1 Like

like this?

1 Like

Yep, then restart. It’ll be in your entity list only, no device.

1 Like