Error creating template do do math on modbus value

I need to do some math on the value returned from a modbus sensor.
I was tryng to follow this: Sensors - template & value math

I know a value-template will not work, so I am trying to use “- platform: template” but when validating the configuration, I am getting the error “Error loading /config/configuration.yaml: mapping values are not allowed here in “/config/configuration.yaml”, line 153, column 12”
How can I do some math on the sensor value?
Image added to show lone numbers.

Thanks,
Mike

    - name: "Battery Current"
      device_class: power
      hub: victron
      unit_of_measurement: "A"
      slave: 247
      register: 261



- platform: template
    sensors:
      sensor.battery_current:
        value_template: > "{{value('sensor.battery_current') | multiply(0.1) }}" 

You can do this (template is on the same line as value_template so it must be delimited with quotation marks):

- platform: template
  sensors:
    modified_battery_current:
      value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}" 

or this (template appears below value_template so a “line-continuation character”, the greater-than symbol >, appears next to value_template):

- platform: template
  sensors:
    modified_battery_current:
      value_template: > 
         {{ states('sensor.battery_current') | float | multiply(0.1) }}

but not the combination of the two formats like you’ve done.

I also corrected the template.


EDIT

What is the other thing, the one with name: "Battery Current", part of? Never mind; it’s part of the modbus integration.

EDIT 2

Fixed more syntax errors …

If this all is in configuration.yaml, it should be:

sensor:
  - platform: template
      sensors:
        sensor.battery_current:
          value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}" 

if you don’t already have a sensor directive.

The Battery Current is under the sensor directive and is what is actually getting the data from modbus. Some of them need to be divided by 10 or 100 to get the proper data.
Is my formatting an issue? I copied and pasted it, so I think it is correct.

I am still getting this error.
Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/configuration.yaml”, line 153, column 12
-platfor: template is on line 152 for reference.

      
- platform: template
    sensors:
      sensor.battery_current:
        value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}"
      

Edited to add the entire file.

I missed that syntax error.

Shift all lines below platform: template to the LEFT by 2 spaces.

- platform: template
  sensors:
    modified_battery_current:
      value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}" 

The words platform and sensors should be vertically aligned.

EDIT

Fixed more syntax errors …

Intend everything after sensor: two spaces more.

sensor:
  - platform: modbus
    scan_interval: 10
    registers:
      - name: "Grid power"
        device_class: power
        .....

  - platform: template
      sensors:
        battery_current_template: # <- this was also wrong
          value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}"

EDIT: The template name was also wrong
EDIT2: 123, as always, was faster! :slightly_smiling_face:
EDIT3: I also missed the indentation like 123 said.

Giving up for now. :upside_down_face:

2 Likes

Ok, I have shifted all of them. The error has updated to
Invalid config for [sensor.template]: invalid slug sensor.battery_current (try sensor_battery_current) for dictionary value @ data[‘sensors’]. Got OrderedDict([(‘sensor.battery_current’, None), (‘value_template’, “{{ states(‘sensor.battery_current’) | float | multiply(0.1) }}”)]). (See ?, line ?).

I pulled sensor.battery_current from the developer tools.

Thanks for all of the help. This is day two for me with HA.

New configuration.yaml:

Take a look at my post above.
You can’t use a sensor for the template sensor name.

  - platform: template
    sensors:
      battery_current_template: # <- this was also wrong
        value_template: "{{ states('sensor.battery_current') | float | multiply(0.1) }}"
1 Like

I just noticed that in the error message. I have updated it to battery_current_template: and am waiting for the validation to finish.

That worked!
A big thanks to the both of you!! I have been banging my head most of the day trying to get the sensors to have a proper value.

1 Like

With time, this will get better. :+1:

2 Likes

I think VDRainer and I also have a few forehead bruises … I can’t believe how many syntax error were packed into so few lines … and I missed two of them on the first try. :man_facepalming:

1 Like

Thank you for finding them. I was trying to use the forum and google to get it to work, so I had a patchwork of ‘solutions.’ I don’t like to make things too easy. :slight_smile:

1 Like