Escape degree sign in PHP cURL to HASS virtual sensor?

This seems like a very dumb question, but i cannot figure out how to get it solved.

I have a PHP-script that updates a virtual sensor in my HASS setup:

<?php
$temp = '19';
$tempfloat = floatval($temp);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.4.13:8123/api/states/sensor.buitentemperatuur");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"state\": \"$tempfloat\", \"attributes\": {\"unit_of_measurement\": \"°C\", \"friendly_name\": \"Buitentemperatuur\"}}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "X-Ha-Access:";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
```
It updates the temperature nicely, but the degree sign is messed up, as it displays as '19 �C'.

I tried with '&deg;' in the PHP-code, but it didn't work. 

Can someone please enlighten me how to get a correct degree sign (°) in HASS by using this PHP-script?
I'm also not sure if the value is being put in HASS as a float, even though i put in the `floatval()` part. How can i check if it is? If it is put in as a string, that would make it unuseable to use in a automation :stuck_out_tongue:
1 Like

I don’t profess to know what HASS does on the front end but have you tried: http://uk.php.net/htmlentities

or urlencode() the value?

1 Like

As for your float issue you need to remove the quotes around that in your json that is transfered :slight_smile:

i think its probably better to just update the value from the sensor.
the unit of measurement is then set just 1 time in the config as a customize from the sensor.buitentemperatuur

1 Like

That seems like a good idea, but how would i put it into my HASS config?
I tried this:

- platform: template
  sensors:
    buitentemperatuur:
      unit_of_measurement: '°C'

But then HASS won’t even start: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 1737: invalid start byte

I also tried with double quotes around the value like here, but that didn’t work either.

You need to make sure you have the right encoding in your text editor…those symbols are always kind of tricky

1 Like

i think it must be something like this.
youre PHP must make a sensor buitentemperatuur and the template connects to that.
and it must have a different name

sensor:
  platform: template
  sensors:
    buitentemp:
      friendly_name: buiten temperatuur
      value_template: '{{ states.sensor.buitentemperatuur.state }}'
      unit_of_measurement: '°C'   
1 Like

SOLVED!

It was indeed a problem with encoding. I always edit my .yaml’s with the built-in editor of WinSCP, so i can edit the files directly on the VM that HASS is running in.
In WinSCP i opened the file with an external editor (Sublime Text 3, nice syntax marking), copied and pasted the Celsius markup part from here and clicked ‘Save with encoding’ and chose ‘UTF-8’. Then HASS would boot without problems and displayed a nice degree symbol :smiley:

Further i setup the sensor like ReneTode mentioned:

- platform: template
  sensors:
    buitentemperatuur:
      value_template: '{{ states.sensor.buitentemperatuur.state }}'
      unit_of_measurement: '°C'
      friendly_name: 'Buitentemperatuur'

and just update only the state of it with this:

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"state\": $tempfloat}");

I think i need to adjust my workflow a bit by using Sublime Text as my default editor always :stuck_out_tongue:
Thanks everyone!

2 Likes