HA custom sensor

Hello, I have one problem that I can’t to solve.
I’d like to send data from my device to HA. I have created short php prog that send test messages to HA:
{"state":"43","attributes":{"type":"sensor","value":33.5,"message":"Property norm","friendly_name":"All functions","temperature":"21","voltage":"5.07"}}
I can get and see value of ‘state’ field, but i can’t to find a way to see/to use other fields, such as ‘value’, ‘voltage’.
In my config I have tried:

sensor:  
  - platform: template
    sensors: 
        dsh_sensor_s:
            value_template: "{{ sensor.dsh_sensor.attributes.value }}"
            friendly_name: 'speed'
            unit_of_measurement: 'km/h'
        dsh_sensor_t:
            value_template: "{{ sensor.dsh_sensor.attributes['temperature'] }}"
            friendly_name: 'temperature'
            unit_of_measurement: 'C'
        dsh_sensor_v:
            value_template: "{{ sensor.dsh_sensor.attributes['voltage'] }}"
            friendly_name: 'voltage'
            unit_of_measurement: 'V'

No one sensor wan’t works/show value.
In Dev Panel I can see a list of attributes, that comes from my php:

type: sensor
value: 33.5
message: Property norm
friendly_name: All functions
temperature: '21'
voltage: '5.07'

So, the sending data and format are acceptable by HA, but I can’t to use it.
How I can to show/use Attribute’s value?

I use API to receive message at HA side. (Headers and others details regarding REST API | Home Assistant Developer Docs ) - so it works well.
(To anyone who will repeat my experiment I leave my code below).
My php:

$service_url = "http://local_ip:8123/api/states/sensor.dsh_sensor";
echo"cURL: $service_url<br><br>";

// Initialize the cURL
$ch = curl_init($service_url);

// Set service authentication

$body = array();
$body["state"] = "43";

$attr["type"] = "sensor";
$attr["value"] = 33.5;
$attr["message"] = "Property norm";
$attr['friendly_name'] = "All functions";
$attr["temperature"] = "21";
$attr["voltage"] = "5.07";

$body["attributes"] = $attr;// '{friendly_name: "my function"}';

$data = json_encode($body); 
echo"Send body: $data<br>";

// Composing the HTTP headers
$headers = array();
$headers[] = 'Authorization: Bearer my_long_term_token_here';
$headers[] = 'Content-Type: application/json';

// Set the cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, TRUE);  
//curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// Execute the cURL
$dd = curl_exec($ch);

The value templates are not quite right. Try:

        dsh_sensor_s:
            value_template: "{{ state_attr('sensor.dsh_sensor', 'value') ​}}"
            ...
       ​dsh_sensor_t:
           ​value_template: "{{ state_attr('sensor.dsh_sensor', 'temperature') }}"
           ...
       ​dsh_sensor_v:
           ​value_template: "{{ state_attr('sensor.dsh_sensor', 'voltage') }}"

Great! It works. Thank you.

1 Like

By the way - how I can to reduce a digits 58,5700000000014 → 58,57 ?

I have answer - there need to use - |round(2)

1 Like

Hi,
I’m trying to achieve something similar as You but not really sure how to start :frowning
My solar panels provide me with a web interface where I can see how much power each panel is generating, etc.
I would like to read a specific value from that page and then somehow pull this into HA. I have checked that I can use curl command which produces something like this:

~ $ curl --http0.9 -o - http://192.168.12.210/hometable.xml -s
1.82;12332.78;18.99;14.91;25;01;25;25;00;YC;

where each value is separated by a semicolon.

How would I go about picking the first value from the above output (e.g. 1.82) and making it available as a sensor (power in kW)?