91JJ
(91JJ)
August 13, 2021, 3:12pm
1
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…
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.
tom_l
August 17, 2021, 8:20am
2
Can you get the data without the pound symbols?
That would make it easier.
91JJ
(91JJ)
August 17, 2021, 8:22am
3
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.
koying
(Chris B)
August 17, 2021, 8:40am
5
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:] }}
1 Like
91JJ
(91JJ)
August 17, 2021, 9:11am
6
Thanks, but I get invalid config ?
91JJ
(91JJ)
August 17, 2021, 9:29am
8
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
91JJ
(91JJ)
August 17, 2021, 9:49am
10
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.
koying
(Chris B)
August 17, 2021, 9:55am
11
Saw that nice invitation to check the logs?
I’m assuming you use a recent version of HA, here…
91JJ
(91JJ)
August 17, 2021, 10:03am
12
Sorry my mind is all-over at the moment
I’m a couple of versions behind
koying
(Chris B)
August 17, 2021, 10:08am
13
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
1 Like
91JJ
(91JJ)
August 17, 2021, 10:10am
14
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? )
91JJ
(91JJ)
August 17, 2021, 10:19am
15
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:] }}
koying
(Chris B)
August 17, 2021, 10:25am
16
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.
91JJ
(91JJ)
August 17, 2021, 10:30am
17
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.
91JJ
(91JJ)
August 17, 2021, 10:38am
18
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?
koying
(Chris B)
August 17, 2021, 10:47am
19
Yes. See
But, once again, I don’t know if that works on your version.
1 Like
91JJ
(91JJ)
August 17, 2021, 11:18am
20
Thanks again.
When I get time, i’ll try study the formatting a little more to get it to send via the restapi