Camera snapshot to base64: what do I need to change for this to work?

Hi everybody,


Update: I believe I’ve found what’s causing this; is there still a way to get this string into Home Assistant somehow, even though it is longer than 255 characters?

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.global_base64_cam_draussen_einfahrt. State max length is 255 characters.


I am trying to create a sensor that will return a base64 encoded string of an image so that I can send it via mqtt.

I created this script below, which will then be accessed via the sensor (below that)

#!/usr/bin/env bash
# set -x

URL_CAM_EINFAHRT='url'


img2b64 (){
    curl -s $1 | base64 -w 0
}

case "${1}" in
    cam_einfahrt)
        img2b64 $URL_CAM_EINFAHRT ;;
    *)
        echo "Hier stimmt was nicht.";;
esac

# vi: ft=bash

the -w 0 assures that the output is one single string, not a bunch of lines. I can run this script (./base64helper.sh cam_einfahrt) and it will return a valid base64 string; this string can be passed via, for example, mqtt.

sensor:
    - platform: command_line
       name: "Global/base64/Cam/Draussen/Einfahrt"
       command: "/config/packages/base64helper.sh cam_einfahrt"
       unique_id: 20230128085908

for some reason, the spacing looks weird in the browser when I paste it; but all my other - platform: command_line sensors within that yaml file are set up the same, and they work.

This sensor will appear in Home Assistant (sensor.global_base64_cam_draussen_einfahrt), be just be unavailable. I have tried restarting Home Assistant, I have ran service: command_line.reload. No difference, it stays the same.

When I ssh into my hassOS and run the script there, it will work (so it doesn’t just run on my desktop, but hassOS can positively access that camera and its snapshot URL as well).

I tried the script without the -w 0 after base64, and I removed the comments # set -x and # vi: ft=bash just because (I didn’t think it’d do anything, but somebody in the forums complained about all my comments in my yaml files before, so I thought I’d mention it doesn’t have anything to do with them :wink: ).

What I am trying to do is pass this base64 string to mqtt.publish, so that my desktop PC can accept it through notify2mqtt.sh, which will display the contents as a notification. I manually pasted the base64 that this script generates, and it works:

Basically, everything works:

  • generate the desired base64 string via bash script
  • mqtt.publish this data
  • show the notification


The only thing that doesn’t seem to work is automate this, so that I can use the script like this

variables:
  nachricht: >-
    {
      "title": "Home Assistant",
      "text": "Tor geöffnet",
      "iconB64": {{ states('sensor.global_base64_cam_draussen_einfahrt') }}
     }

The sensor will always be unavailable. What can I change to fix this?

Thank you in advance for your help :slight_smile: