Creating Sensor with API - or multiple sensors from text

I’m currently working on pulling data from a mobile app into Home Assistant.
I’ve managed to get the data I require into the format below before it’s sent to Home Assistant…

£31.55, £40.64

Using the API the data is stored in Home Assistant like the below…

image

The API is very basic… hence why I’m here.

{"state": output} (where ‘output’ is my data source)

These are the ‘Electric’ & ‘Gas’ so I’m wanting to create them into their own sensors with Icons etc… was hoping to use the Template but quite stuck now.

Entity: Electric
State: £31.55 (first numbers from the output data)
icon: mdi:flash

Entity: Gas
State: £40.64 (second set of numbers from the output data)
icon: mdi:fire

I understand it may be two separate sensors to generate these, just curious on the best way to do so.

Can you get the data without the pound symbols?

That would make it easier.

Hi Tom,
potentially I could if I change the API call? If it’s possible to template there?

But before that, unfortunately not.

You can extract the data like:
{{ (states('sensor.utilita').replace("'", '"')|from_json)[0] }}

And ofcourse [1] for the second.

You can create a template sensor like this (for Electric)

template:
  - sensor:
    - name: 'Electric'
      icon: mdi:flash
      unit_of_measurement: '£'
      state: >
        {{ (states("sensor.utilita_test") | from_json) [0] [1:] }}

image

1 Like

Thanks, but I get invalid config ?
image

That has to go:

  • Or as-is in configuration.yaml
  • Or in a templates.yaml if you did something like template: !include templates.yaml; then remove the template:
1 Like

Really sorry, I feel dumb now…
I’m using the !include templates.yaml so I’m trying to store it in there but still can’t get it to work.

I added directly into my config.yaml but obviously that killed my templates.yaml

Details, please

Apologies, I’m getting confused.
I’m trying the sensor.yaml not template.yaml

I don’t have a template.yaml so adding into the config.
I added the below, but instantly get this error.

image

Saw that nice invitation to check the logs? :wink:
I’m assuming you use a recent version of HA, here…

Sorry :frowning: my mind is all-over at the moment

I’m a couple of versions behind
image

Yep, too old.
Update, or translate the template to the old, deprecated, platform: template way.

A 4 months old HA is like using Windows Vista: You can still use it, but don’t expect meaningful support :grin:

1 Like

Really appreciate your help, and really sorry, should have highlighted the version at the start.

I’ll convert over and give it another go.
(on another note, will this break my current sensors? :stuck_out_tongue: )

Here’s what I’m putting in my sensor.yaml

- platform: template
  sensors:
    utilita_electric:
       friendly_name: "Electric"
       value_template: >-
         {{ (states("sensor.utilita_test") | from_json) [0] [1:] }}

image

Or utilita_electric is not yet populated, and you have to wait.
Or it will never work on that version because the template does not recover from the initial error.

I think I’ll leave it for another day, or until I update to the latest.

Just found that it’s showing ‘unavailable’ as the utilita_test entity disappears when I add this template in… Weird.

Would there be an easier way when making the API call to write the data?
Could I make two API calls to write both them separately using a template?

Yes. See

But, once again, I don’t know if that works on your version.

1 Like

Thanks again.
When I get time, i’ll try study the formatting a little more to get it to send via the restapi