Afficher état d'une entité HA sur une page web en PHP

Bonjour,

Voici comment afficher un état d’une entité HA sur une page en PHP :

(En exemple je vais prendre une sonde de température 1)

<?php
$output = shell_exec('
	curl -X GET -H "Authorization: Bearer ***JETON_LONGUE_DUREE_HOMEASSISTANT***" \
	-H "Content-Type: application/json" \
	http://***IP_HOMEASSISTANT***:8123/api/states/sensor.temperature1
');
echo $output;
?>

On retrouve alors toutes les informations de l’état de la sonde de température 1:

{"entity_id": "sensor.temperature1", "state": "21.68", "attributes": {"state_class": "measurement", "unit_of_measurement": "\u00b0C", "friendly_name": "Temp\u00e9rature1", "device_class": "temperature"}, "last_changed": "***DATE_LAST***", "last_updated": "***DATE_LAST***", "context": {"id": "***ID***", "parent_id": null, "user_id": null}}

Comment faire pour décomposer en PHP ce résultat en 21.68°C ?

Merci d’avance

Que donne :

<?php
$json= shell_exec('
	curl -X GET -H "Authorization: Bearer ***JETON_LONGUE_DUREE_HOMEASSISTANT***" \
	-H "Content-Type: application/json" \
	http://***IP_HOMEASSISTANT***:8123/api/states/sensor.temperature1
');
$arr= json_decode($json, true);
echo $arr['state']

?>

:question:

edit:

Pour avoir l’unité de mesure :

echo $arr['state'] . $arr['attributes']['unit_of_measurement']

Source : How to Encode and Decode JSON Data in PHP - Tutorial Republic

1 « J'aime »

Géant ! Merci beaucoup @Clemalex, ça donne bien 21.68°C

1 « J'aime »