Creating Sensor with API - or multiple sensors from text

Saw that nice invitation to check the logs? :wink:
I’m assuming you use a recent version of HA, here…

Sorry :frowning: my mind is all-over at the moment

I’m a couple of versions behind
image

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 :grin:

1 Like

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? :stuck_out_tongue: )

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:] }}

image

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.

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.

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?

Yes. See

But, once again, I don’t know if that works on your version.

1 Like

Thanks again.
When I get time, i’ll try study the formatting a little more to get it to send via the restapi

I would love to see that as a forum text.
Example:


So green for latest release, orange for previous and red for older.
That way it’s easier to find issues for people that answer.

2 Likes

This would be cool!
Maybe suggest it as a recommendation

If we just add our nabu casa account link in our profile then it could be fetched I guess.

1 Like

Slightly back on topic, using the template in the Dev Tools, the below gives me just the numbers.
(bad code I know)

 {{ states.sensor.utilita_test.state.split('£')[2]|replace("']", ' ')|trim  }}
 {{ states.sensor.utilita_test.state.split("£")[1]|replace("'", '')|replace(",", '')|trim }}

However, when I add them to a template, the following happens

  • sensor.utilita_test entity ‘disables’ in Home Assistant
  • ‘State’ on the two sensors created show ‘unavailable’
- platform: template
  sensors:
    utilita_gas:
       friendly_name: "Utilita Gas"
       unit_of_measurement: '£'
       value_template: >-
            {{ states.sensor.utilita_test.state.split("£")[1]|replace("'", '')|replace(",", '')|trim }}

image

Did you remove it from config?

And use {{ states('sensor.utilita_test').split("£")[1]|replace("'", '')|replace(",", '')|trim }} that way it won’t create errors when unavailable

1 Like

Ha, no, you have a good point there!

I noticed when I was rebooting HA to enable the new templates, I also had to run the API call to “create” the sensor.utilita_test entity

The below now works if I trigger the API call after I’ve rebooted HA.

- platform: template
  sensors:
    utilita_electric:
       friendly_name: "Utilita Electric"
       unit_of_measurement: '£'
       value_template: >-
              {{ states('sensor.utilita_test').split("£")[1]|replace("'", '')|replace(",", '')|trim }}
              
- platform: template
  sensors:
    utilita_gas:
       friendly_name: "Utilita Gas"
       unit_of_measurement: '£'
       value_template: >-
              {{ states('sensor.utilita_test').split("£")[2]|replace("'", '')|replace(",", '')|replace("]", '')|trim }}

I made a webpage that can give us a signature image with the version number we have installed.
If you can’t see the image then you need to enable the signatures in your profile.

You need three sensors:
Borrowing from tom_I, WTH don't I get notifications about available updates? - #3 by tom_l

sensor:
  - platform: version
    name: Installed Version
    image: intel-nuc # change for your system, see https://www.home-assistant.io/integrations/version/
    source: local
    
  - platform: version
    name: Latest Version
    image: raspberrypi4 # change for your system, see https://www.home-assistant.io/integrations/version/
    source: hassio

  - platform: rest
    name: forum_image
    resource_template: >-
                        http://www.hoppvader.nu/hass/version.php?version={{states('sensor.installed_version') }}&latest={{states('sensor.latest_version') }}&user=Hellis81
    value_template: '1'  # dummy value
    scan_interval: 86400     

You need to change the username in the rest URL.

This will then result in a image being created on my webpage that you can link to as a signature using:

<img src="http://www.hoppvader.nu/hass/version.php?user=Hellis81">

And obviously changing the username.

1 Like

Great idea, but would you mind sharing the php code?
I guess a lot of people (including me :wink: ) won’t feel at ease with injecting code from a unknown website :slight_smile:
Thanks

Sure…
But PHP has the advantage that it runs on the server thus the client is safe.

<?php

if($_GET["version"] !="" and $_GET["user"] !="" and $_GET["latest"] !=""){

    header('Content-Type: image/png');
    
    $img = imagecreatefrompng('HA.png');
    
    $white = imagecolorallocate($img, 255, 255, 255);
    $red = imagecolorallocate($img, 255, 0, 0);
    $orange = imagecolorallocate($img, 255,165,0);
    $green = imagecolorallocate($img, 255, 0, 0);
    
    $age = explode(".", $_GET["latest"])[1] - explode(".", $_GET["version"])[1];
    
    if($age == 0){
        $color = $green;
    }elseif($age == 1){
        $color = $orange;
    }else{
        $color = $red;
    }
    
    
    $font = 'arial.ttf';
    $x = 80;
    $y = 50;
    imagettftext($img, 18, 0, $x+1, $y+1, $white, $font, $_GET["version"]);
    imagettftext($img, 18, 0, $x, $y, $color, $font, $_GET["version"]);
   
    
    
    imagepng($img,  $_GET["user"] . ".png");
    imagedestroy($img); 
    
}elseif($_GET["version"] == "" and $_GET["user"] !="" ){
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header('Content-Type: image/png');
    $img = imagecreatefrompng($_GET["user"] . ".png");
    imagepng($img);
}
?> 

So the if() is if you have all variables set, then it creates a png with the version number you have and with the filename [username].png.

else, it returns the image [username].png, meaning when the forum wants the image.

1 Like

I just noticed the code to the signature had been replaced with an image in a code block.
edited-