How to read HA temperature sensor from PHP

Hi!

I’m totally new to Home assistant and trying to figure out how to publish my temperature sensors to my website. I used to do this from Domotiz using a PHP script and parse the JSON values easly.
However, i don’t understand the HA API …
Searched the forum but everything is about geting values INTO Home assistant.

Any examples would be highly appreciated!

//Regards…

How do you need to send the data to your website?
Can you please show an example data set that should be sent to your website?

Hi,
if you can make a REST call from php, you can use something like this, the response is in JSON format:
http://<home-assistant-ip>:8123/api/states/<entity-id>

Besides you need an Access token which you can create at profile page from HA. Here is an example call from Firefox REST Plugin:

1 Like

Ahaaaa… Got it… Token… created and getting the state !
That’s easy…

Thanks!

Made a quick PHP get:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.1.121:8123/api/states/sensor.entre_ute_temp');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ....'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
echo '<br>';

$info = json_decode($result,true);

$entity_id = $info['entity_id'];
$state = $info['state'];
$last_changed = $info['last_changed'];
echo $info.'  '.$state.'  '.$last_changed;
?>

Works!
Thanks again!

hi, I want to ask, is the PHP get code above used to get the sensor value?, I tried it through local visual studio. But, I still can’t get the value out, I’ve changed the URL path, but it still doesn’t work. Is there a solution?, thanks in advance

Did you create the access token and used that in the php code?

thanks for the response. yes, i’ve created a token and using php, i made it like this, is there something wrong?

What do you get if you var_dump $result?

In that case the curl has failed. You need to have a look at the URL or the auth code.

i think for url and my token is correct, even for token i have regenerated in long-lived access token.

I happen to use sonoff pow R3 which has sensors for power, energy, and kwh are there other provisions for sonoff pow R3?

You are querying HA not the device.
And you are doing that inside your local LAN so your masking makes no difference. It only makes it harder to help you.
Are you sure you have SSL set up in your local LAN? Shouldn’t the URL be http?

hi sorry for late response, my home assistant uses https because it uses duckdns so the url should be correct using https and I’ve added it to configuration.yaml on home assistant visual studio not local (which is on ha) ssl_certificate and ssl_key

so if i use http can’t open the home assistant.

is there a different way if i use https?