Calculation of the value

Hi community, I’m new here and I’d like to ask for help to solve my current problem.

I’m playing with Tasmota and the MQ-2 sensor. I flashed the Wemos D1 Mini with the Tasmota firmware 13.0 and connected the MQ-2 to the Analog A0 pin and configured it with MQ template. At this point I receive the value about 3.55 ppm which is 100x lower than the value. I tried to adjust the pullt up resistor with various value where I received not quite expected values. I believe that the optimal way is to adjust the incoming value and multiply it directly in the Home Assistant.

image

What I did:
I was to able to calcute the value under Developer Tools/Template

{{ states(‘sensor.mq_2’) | float(0) * 100 }}

but I’m not able (cannot understand) how to put this information that is visible in the sensor value

I’m not a developer and this is more than pain for me as I’m trying to solve it few weeks but without any progress. YAML is for me a bit difficult. I found a lot of information about this topic but nothing helped.

Version of my Home Assistant is 2023.6.3, Core on RPI3 (Ubuntu 22.04.3 LTS)

Thanks for help,

Patrik

Hey there, welcome! :wave:

I’m sorry, but I don’t understand, what you’re trying to do. :open_mouth:

Do you want to show this sensors value in the frontend? Or do you need for other things?

Either way, if you get a sensor value from an integration (like you do), you can always setup a so called template sensor, do modifications to it, and use that one to work with.

If that’s what you’re looking for, let me know, I’ll provide an example. :slight_smile: Nonetheless, just let us know, what you want to achieve, and we’ll go from there. :slight_smile:

Hi Paddy,

thanks for reply. At this point I can see the value of CO sensor on the level 3.43ppm but I need that it shows 343ppm. Have to multiply the value 100x. I don’t know how to do it :smile:

image

Here is the current value but I need that it shows 343 instead 3.43 :slight_smile: I know that when I use this {{ states(‘sensor.mq_2’) | float(0) * 100 }} then it calculates the vale but I don’t know how to replace.

Similar like here: How do i calculate values from sensor data

but for me this procedure didn’t worked.

Patrik

That doesn’t answer, where you want to use it. :wink: :slight_smile: But yes, the linked topic is exactly what you’re looking for - a template sensor! :slight_smile:

The idea behind this is as follows: sensor values are sent from an integration to HA, that is the raw data for HA to work with. If you need to change something, as in your case multiply it or any other change, you need to do this afterwards. That’s where template sensors come into play. You take the raw value of one or more sensors and combine these in one template sensor, so you can use the changed value eg. to display or whatever you like. The raw data is still untouched and can be used by the system.

This goes in your configuration.yaml or if you have split up your configuration in the corresponding file:

template:
  - sensor:
      - name: YOUR_NEW_SENSOR_NAME
        unit_of_measurement: ppm
        device_class: carbon_monoxide
        state: >
           {{ states(‘sensor.mq_2’) | float(0) * 100 }}

:slight_smile:

Thanks for reply. I’ve added exactly the same you wrote and got error:

  • Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 11) for dictionary value @ data[‘sensor’][0][‘state’]. Got “’{{ states(‘sensor.mq_2’) | float(0) * 100 }}’\n”. (See /var/snap/home-assistant-snap/571/configuration.yaml, line 59).

  • Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 10) for dictionary value @ data[‘sensor’][0][‘state’]. Got ‘{{ states(‘sensor.mq_2’) | float(0) * 100 }}\n’. (See /var/snap/home-assistant-snap/571/configuration.yaml, line 59).

I created a customize.yaml file, included it to the configuration. What is working is this:

sensor.mq_2:
unit_of_measurement: “ppm”
device_class: carbon_monoxide
----------above commands are working, below commands are not-------
state: >
{{ states(‘sensor.mq_2’) | float(0) * 100 }}

I tried several posts from forum but nothing helped and also the example from the link you’ve sent me.

I’m so sorry, this is the second time this happens this week… I overlooked the wrong apostrophes…

Take this one instead:

template:
  - sensor:
      - name: YOUR_NEW_SENSOR_NAME
        unit_of_measurement: ppm
        device_class: carbon_monoxide
        state: >
           {{ states('sensor.mq_2') | float(0) * 100 }}

What’s changed are the apostrophes around the sensor name… They need to be " or '.

And btw. if you have time later, please read the forum rules regarding code formatting, it really is important. Not in this case, but if there are errors with indentation it is only findable with the correct formatting. :slight_smile:

Well, the formating was the first I checked and I all the time using the configuration check under the developer tools. Unfortunately the problem still persists and I don’t know where could be the issue.

@patrik.zuffa you haven’t copied the correctly

You have added ‘ ‘ to the last line.

Hi Julian,

I didn’t this is exactly the problem I have. I tried it with quotes, without, various combinations, with syntax check, config reload, HA restart, RPI restart, but I’m not able to change the value. The sensor is connected to Tasmota Wemos D1 Mini via A0 sensor. I tried to adjust the value also directly in the Tasmota by add of the rule but somehow it does not work. Maybe it helps that the information is sent via MQTT.

sensor.mq_2:
  unit_of_measurement: "ppm"
  device_class: carbon_monoxide
  state: >
    {{ states(‘sensor.mq_2’) | float(0) * 100 }}

Here is the MQTT value from MQTT Explorer

{“sn”:{“Time”:“2023-08-10T11:36:06”,“ANALOG”:{“MQ2_0”:4.95}},“ver”:1}

This is what I’m using since the last reboot and on the image below is the value I get. I spent couple of weeks by searching but nothing helps.

image

Patrik

You can’t add sensor definitions to this file — only customizations.

This is what I have, it is the whole customize.yaml file. It is not is the configuration.yaml file. From above mentioned config works the “ppm” unit which is added after the measured value, but not the calculation of the value.

You cannot use templates in the customize.yaml file.

That’s why this works:

unit_of_measurement: "ppm"

but this doesn’t:

state: >
    {{ states(‘sensor.mq_2’) | float(0) * 100 }}
1 Like

Now I get it. And where shall I put the calculation?

You must create a Template Sensor, as was advised by paddy0174.

1 Like

I think I have it. There were a lot of thing to change on my side.

  • I’ve renamed in the configuration.yaml
sensor: !include sensors.yaml

to

template: !include template.yaml

then I put this config into the file

- sensor:
    name: "MQ2"
    unit_of_measurement: "ppm"
    device_class: carbon_monoxide
    state: >
      {{ states('sensor.mq_2') | float(0) * 100 }}

which created a new entity sensor.mq2

image

where is the expected value.

By this ended my pain with the adjustment of the values using templates and I’m a bit closed to better understand how great is the Home Assistant. Thank you guys for the contribution.

Patrik

It’s the custom of this community forum to assign the Solution tag to the first (or best) post that provides the answer/solution to the original question/problem. In this topic, that would be paddy0174’s post.

If everyone marked their own post with the Solution tag, regardless of the assistance they received from others, it would give the impression that everyone ultimately solves their own problem.

For more information, please refer to guideline 21 in the FAQ.

2 Likes

While that will work for this sensor as it is a template sensor, there’s also good use for a sensors.yaml for sensors that are not template sensors but sensors for other integrations.

@patrik.zuffa since you’re still new, I would suggest to look into YAML packages already now. I wish I did a lot sooner than I started to migrate config. Packages keep things a lot neater. Save yourself some future admin. :slight_smile:

1 Like