I'll pay someone to help me with this

I have a device sending data to home assistant over serial port:

sensor:
  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 9600
    stopbits: 1
    name: veeder

Under “Developer Tools - Events” I’m listening to “state-changed”, when I request data from my device, I get this returned to me:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "1   TEST TANK                8178   71.35     1.2   70.4     1550",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.369028+00:00",
            "last_updated": "2021-01-30T03:23:00.369028+00:00",
            "context": {
                "id": "5f2237a3f3ce6e21564edc58c83edbb0",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.529468+00:00",
            "last_updated": "2021-01-30T03:23:00.529468+00:00",
            "context": {
                "id": "316eb114345fe625d8f9f9ddbd4942ac",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.529468+00:00",
    "context": {
        "id": "316eb114345fe625d8f9f9ddbd4942ac",
        "parent_id": null,
        "user_id": null
    }
}

Event 8 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.289744+00:00",
            "last_updated": "2021-01-30T03:23:00.289744+00:00",
            "context": {
                "id": "4d7340ffb8aaf08fcd1fbd6b2fc0df85",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "1   TEST TANK                8178   71.35     1.2   70.4     1550",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.369028+00:00",
            "last_updated": "2021-01-30T03:23:00.369028+00:00",
            "context": {
                "id": "5f2237a3f3ce6e21564edc58c83edbb0",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.369028+00:00",
    "context": {
        "id": "5f2237a3f3ce6e21564edc58c83edbb0",
        "parent_id": null,
        "user_id": null
    }
}

Event 7 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "TANK  PRODUCT               GALLONS  INCHES   WATER  DEG F   ULLAGE",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.288668+00:00",
            "last_updated": "2021-01-30T03:23:00.288668+00:00",
            "context": {
                "id": "bf4f936081e0122ba010991ff6465624",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.289744+00:00",
            "last_updated": "2021-01-30T03:23:00.289744+00:00",
            "context": {
                "id": "4d7340ffb8aaf08fcd1fbd6b2fc0df85",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.289744+00:00",
    "context": {
        "id": "4d7340ffb8aaf08fcd1fbd6b2fc0df85",
        "parent_id": null,
        "user_id": null
    }
}

Event 6 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.225561+00:00",
            "last_updated": "2021-01-30T03:23:00.225561+00:00",
            "context": {
                "id": "2bf95ef3a3b22dc46ead9ae26b437aa9",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "TANK  PRODUCT               GALLONS  INCHES   WATER  DEG F   ULLAGE",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.288668+00:00",
            "last_updated": "2021-01-30T03:23:00.288668+00:00",
            "context": {
                "id": "bf4f936081e0122ba010991ff6465624",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.288668+00:00",
    "context": {
        "id": "bf4f936081e0122ba010991ff6465624",
        "parent_id": null,
        "user_id": null
    }
}

Event 5 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "01-27-21 20:23",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.209392+00:00",
            "last_updated": "2021-01-30T03:23:00.209392+00:00",
            "context": {
                "id": "65eee56f21a91c0ba8e1fd774258c35f",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.225561+00:00",
            "last_updated": "2021-01-30T03:23:00.225561+00:00",
            "context": {
                "id": "2bf95ef3a3b22dc46ead9ae26b437aa9",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.225561+00:00",
    "context": {
        "id": "2bf95ef3a3b22dc46ead9ae26b437aa9",
        "parent_id": null,
        "user_id": null
    }
}

Event 4 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.193635+00:00",
            "last_updated": "2021-01-30T03:23:00.193635+00:00",
            "context": {
                "id": "eb55b1945872178d918ac5a557d4ec12",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "01-27-21 20:23",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.209392+00:00",
            "last_updated": "2021-01-30T03:23:00.209392+00:00",
            "context": {
                "id": "65eee56f21a91c0ba8e1fd774258c35f",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.209392+00:00",
    "context": {
        "id": "65eee56f21a91c0ba8e1fd774258c35f",
        "parent_id": null,
        "user_id": null
    }
}

Event 3 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "TEST STATION",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.192664+00:00",
            "last_updated": "2021-01-30T03:23:00.192664+00:00",
            "context": {
                "id": "cf83bb09697d0a8c1d33b3745386d66a",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.193635+00:00",
            "last_updated": "2021-01-30T03:23:00.193635+00:00",
            "context": {
                "id": "eb55b1945872178d918ac5a557d4ec12",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.193635+00:00",
    "context": {
        "id": "eb55b1945872178d918ac5a557d4ec12",
        "parent_id": null,
        "user_id": null
    }
}

Event 2 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "201",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.081910+00:00",
            "last_updated": "2021-01-30T03:23:00.081910+00:00",
            "context": {
                "id": "eb42ca2403b820551adc613bc3ec0681",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "TEST STATION",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.192664+00:00",
            "last_updated": "2021-01-30T03:23:00.192664+00:00",
            "context": {
                "id": "cf83bb09697d0a8c1d33b3745386d66a",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.192664+00:00",
    "context": {
        "id": "cf83bb09697d0a8c1d33b3745386d66a",
        "parent_id": null,
        "user_id": null
    }
}

Event 1 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "\u0001",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.081002+00:00",
            "last_updated": "2021-01-30T03:23:00.081002+00:00",
            "context": {
                "id": "5bf668102035c92a64a52910a545a086",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "201",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.081910+00:00",
            "last_updated": "2021-01-30T03:23:00.081910+00:00",
            "context": {
                "id": "eb42ca2403b820551adc613bc3ec0681",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.081910+00:00",
    "context": {
        "id": "eb42ca2403b820551adc613bc3ec0681",
        "parent_id": null,
        "user_id": null
    }
}

Event 0 fired 8:23 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.veeder",
        "old_state": {
            "entity_id": "sensor.veeder",
            "state": "unknown",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:22:14.331274+00:00",
            "last_updated": "2021-01-30T03:22:14.331274+00:00",
            "context": {
                "id": "43cfe14f3419d72b354d6496142706ed",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.veeder",
            "state": "\u0001",
            "attributes": {
                "friendly_name": "veeder"
            },
            "last_changed": "2021-01-30T03:23:00.081002+00:00",
            "last_updated": "2021-01-30T03:23:00.081002+00:00",
            "context": {
                "id": "5bf668102035c92a64a52910a545a086",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-30T03:23:00.081002+00:00",
    "context": {
        "id": "5bf668102035c92a64a52910a545a086",
        "parent_id": null,
        "user_id": null
    }
}

When I look at the sensor on “overview” lovelace, I see the states changing rapidly and I see my data there for a split second. I have been trying to figure out how to make a template to parse this part in particular and make 5 different sensors with it.

 "state": "1   TEST TANK                8178   71.35     1.2   70.4     1550",

I have spent weeks trying so many different value_templates things, I can’t get anything to work!

This is the closest I have gotten to anything.

sensor:
  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 9600
    stopbits: 1
    name: veeder
    value_template: >
        {{ states('sensor.veeder').split(' ')[3] }}
- platform: template
    sensors:
      time_1_mode:
        friendly_name: Timer 1 Mode
        value_template: >
          {% if states("sensor.veeder") == "TEST" %}
            {{ states("sensor.veeder") }}
          {% endif %}

You should tell us the error(s) you’re getting.

I’m fairly sure you need to pass your JSON data through a from_json filter first. See the templating docs for more info.

HA doesn’t understand JSON data directly. It needs to be a (Python) object/dictionary. Only then can you do lookups on the data.

sensor: must be followed by platform: and not the other way around.

Thank you for your reply,
I’ve made some progress, in my config.yaml I have:

sensor:
    - platform: serial
      serial_port: /dev/ttyUSB0
      baudrate: 9600
      stopbits: 1
      name: veeder

    - platform: template
      sensors:
        t1_test:
          friendly_name: T1
          value_template: "{{ states('sensor.veeder').split(' ') }}"

which outputs the data to t1_test like:

['1', '', '', 'TEST', 'TANK', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '8178', '', '', '71.35', '', '', '', '', '1.2', '', '', '69.8', '', '', '', '', '1550']

Now I just need to figure out how to catch that data as it’s being sent and output the 5 numbers into their own sensor. I’ve tried the regex find all approach, but in the template editor I can’t figure out how to get that one to work.

Based on a quick examination of the event data you posted, the format of the received data varies a great deal. It can be nothing, a single number, two words, several words, an encoded character, and finally the ‘combination’, of words an numbers, you are interest in.

Whatever is created to extract the values from the desired ‘combination’ must also be able to ignore the many undesired ones.

I assume the columns of data are structured like this?

TANK  PRODUCT               GALLONS  INCHES   WATER  DEG F   ULLAGE
1     TEST TANK             8178     71.35    1.2    70.4    1550

Yes, you are correct that is the data I am trying to extract and the columns are structured like that.

Is it always “TEST TANK” or can it be something else?

If it can be something else, will its name also be two words? Or is there no pattern and the name can be one or more words?

There will be 3 or sometimes 4 different ones
The leading # is the tank number that is programmed

1 UNLEADED
2 PREMIUM
3 DIESEL

or (not common)
1 UNLEADED
2 MID-GRADE
3 PREMIUM
4 DIESEL

Is it safe to assume that the name will never contain a space like “TEST TANK”? Or is there a possibility that it might?

No it will never contain spaces, that was just how I set it up to test.

Try this just make sure you don’t use “TEST TANK” (two words) otherwise it won’t work. If you change the name to a single word, like “TEST_TANK” then it’ll be fine.

    - platform: template
      sensors:
        t1_test:
          friendly_name: T1
          value_template: >
            {% set x = states('sensor.veeder').replace('    ', ' ').replace('   ', ' ').replace('  ', ' ') %}
            {{ x if x.split() | count == 7 else states('sensor.t1_test') }}

The template’s first line preps the received data. It replaces multiple spaces with single spaces. The goal is to transform this:

1   DIESEL                8178   71.35     1.2   70.4     1550

into this:

1 DIESEL 8178 71.35 1.2 70.4 1550

The last line determines whether to accept or reject the received data:

  • If there are 7 items in that string then it’s considered to be good data and will become the Template Sensor’s new state value.
  • If there are more or less than 7 items, that doesn’t correspond to what we are looking for so we reject it. However, the Template Sensor has to report something so it simply reports its existing state value.

Depending on the data it receives, the template may need some more adjustments. We’ll only know that after you test it.

Once you’re convinced it’s working correctly, the next step depends on how you want to present the data. For example, you can create many Template Sensors, one for each parameter (diesel-gallons, diesel-temperature, premium-gallons, premium-temperature, etc) or you can create a Template Sensor for each type of fuel (diesel, premium, etc) and then store its parameters as attributes.

Okay so I applied the changed in my config.yaml and restarted HA. Now it says T1 is Unavailable even when I request the data from my device. And thank you for your help!

Maybe I did it wrong? This is how I have it configured:

sensor:
    - platform: serial
      serial_port: /dev/ttyUSB0
      baudrate: 9600
      stopbits: 1
      name: veeder

    - platform: template
      sensors:
        t1_test:
          friendly_name: T1
          value_template: >
            {% set x = t.replace('    ', ' ').replace('   ', ' ').replace('  ', ' ') %}
            {{ x if x.split() | count == 7 else states('sensor.t1_test') }}

Sorry, that was my mistake. I pasted my test code without adjusting it to use your sensor.veeder. :man_facepalming:

I modified my example (above). You’ll notice that the first line now refers to the veeder sensor (as opposed to an undefined variable called t as was used in my test code).

NOTE
Because it’s a Template Sensor, you should be able to modify it and then execute Configuration > Server Controls > Reload Template Entities (instead of having to restart Home Assistant).

Haha no worries, IT WORKED!! and thank you for the tip on reloading that would have saved me a lot of time in the past. To answer your question earlier, do you think it would it be easier for me referencing the parameters at attributes? or just creating a template sensor for each parameter?
EDIT
My end goal is to display a picture on lovelace of each of the fuel tanks and depending on the level it will show a different picture

If your goal is to graph the values then you’ll need each parameter to be a separate Template Sensor.

This should help get you going. The first sensor is called sensor.tanks and is basically the one you currently have called sensor.t1_test. The “tanks” sensor is used by all the other sensors as their source of data.

    - platform: template
      sensors:
        tanks:
          friendly_name: All Tanks
          value_template: >
            {% set x = t.replace('    ', ' ').replace('   ', ' ').replace('  ', ' ') %}
            {{ x if x.split() | count == 7 else states('sensor.tanks') }}

        tank_diesel_quantity:
          friendly_name: Diesel Tank Quantity
          unit_of_measurement: gallons
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[2] if x | count == 7 and x[1] == 'DIESEL' else states('sensor.tank_diesel_quantity') }}

        tank_premium_quantity:
          friendly_name: Premium Tank Quantity
          unit_of_measurement: gallons
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[2] if x | count == 7 and x[1] == 'PREMIUM' else states('sensor.tank_premium_quantity') }}

        tank_diesel_level:
          friendly_name: Diesel Tank Level
          unit_of_measurement: 'inch'
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[3] if x | count == 7 and x[1] == 'DIESEL' else states('sensor.tank_diesel_level') }}

        tank_premium_level:
          friendly_name: Premium Tank Level
          unit_of_measurement: 'inch'
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[3] if x | count == 7 and x[1] == 'PREMIUM' else states('sensor.tank_premium_level') }}
 
        tank_diesel_temperature:
          friendly_name: Diesel Tank Temperature
          unit_of_measurement: '°F'
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[5] if x | count == 7 and x[1] == 'DIESEL' else states('sensor.tank_diesel_temperature') }}

        tank_premium_temperature:
          friendly_name: Premium Tank Temperature
          unit_of_measurement: '°F'
          value_template: >
            {% set x = states('sensor.tanks').split() %}
            {{ x[5] if x | count == 7 and x[1] == 'PREMIUM' else states('sensor.tank_premium_temperature') }}

Let me know if you need help to modify these Template Sensors.

1 Like

THANK YOU SO MUCH!! :smiley:
Bought some coffee from ya haha
thanks again!
I’ll do some testing and let you know how it works out!

1 Like

Thank you for the coffee!