Help with attribute_templates on a Custom Sensor

Hello guys,

I need your help regarding the creation of a sensor.
First of all, I want to explain what I need to do:

I need to create a Graph with some manually inserted data.
Ok, this isn’t a very smart usage of HomeAssistant, I know that. I already have a lot of atomation and so on, but now I’m starting a project that I want to track inside an HA Dashboard.

That said, I found a cool Custom Card (ApexCharts card) which can give the possibility to create a graph from an array inside an attribute of a sensor.

So, I tryied creating mine own sensor, like this one:

sensor:
  - platform: template
    sensors:
      test:
        friendly_name: Test
        unit_of_measurement: 'X'
        value_template: '1'
        attribute_templates:
         data: >
            - date: '2021-05-22T05:30:00.000Z' 
              value: 0.00499446
            - date: '2021-05-23T05:30:00.000Z'
              value: 0.01417467

But it doesn’t work due to the fact I’m creating a string inside the “data” attribute and not an array. This is the attribute shown on Dev tools:

data: |-
  - date: '2021-05-22T11:05:30+00:00' 
    value: 0.00499446
  - date: '2021-05-22T11:05:30+00:00'
    value: 0.01417467

The “problem” is that “|-

So, now I have to questions for you guys:

  • Is there a way to create an array inside the attribute of a sensor template?
  • Any other ideas on how to use some “graph card” with manually inserted data is welcome.

Thank you!

Change this from YAML to a Jinja2 template.

May I ask you some help on this?

Use an online YAML to JSON converter then add double-braces to make it a template. Here’s what you will put data: > but you will need to indent it properly (sorry, I am on mobile and it’s challenging to format the text).

{{ [
    {
        "date": "2021-05-22T05:30:00.000Z",
        "value": 0.00499446
    },
    {
        "date": "2021-05-23T05:30:00.000Z",
        "value": 0.01417467
    }
] }}
1 Like

Thank you so much Mate! It worked perfectly!

2 Likes