Make a block of JSON available within HASS

Hey all

SOME BACKSTORY I am currently trying to write a front-end remote for my onkyo AVR. I have config working well enough, but there is a lot of variation in the commands and arguments sent to the AVR.

SO I wrote up a JSON object that stores all the commands and arguments for each component that I might wish to control - so that (when I want to template-up a service call) the commands and arguments can be gleaned by refeering to the JSON object via a friendly name (which are stored in an input_select drag-down)

My problem is: How can I store such a JSON object so that it is always available? I want to use these items and values within templates.

I tried creating a MQTT sensor to store it (sent as a MQTT message with retain=true), but the payload is too large and just gets dropped.

I am open to any way to store this JSON into config?

Thanks in advance - yes I have been googling but I don’t know the search terms :slight_smile: you don’t know, what you don’t know!

Matt

Not sure if I understood the problem, but if the problem is just with long codes on yaml, you may use anchors. Take a look at this page https://blog.daemonl.com/2016/02/yaml.html.

hey @clyra thanks for the link - interesting and good to know

But what I am actually after here, is to make my block of information (a number of objects, containing items and values) available for use within Home Assistant

I want to refer to this JSON object for building the request I will send to the AVR.

I really don’t know where to start… the only reason I ran into a problem with length, is because I tried to store it in a MQTT sensor…

I hope that helps, I am struggling to describe it too!

The only thing that comes to mind for me, if I’m understanding what you want to do, is to write a Python Script that builds the request to send to the AVR when called. You can pass it parameters to control exactly what it sends. Then you can create a Python dictionary in the script (instead of a JSON object.)

1 Like

Thanks @pnbruckner - yep good call, I will go down that route if need be. I am currently moving automations to NodeRed, so I would probably define an object in javascript to refer to.

I do like the efficiency of the Home Assistant templates though, to keep things snappy - I will hold out a bit longer in case someone knows something clever…!

I still don’t get why you can’t do with anchors, but my second thought was like @pnbruckner suggested. A third option which I’m less confident but maybe is just what you want is the variable custom component. Take a look at this: https://github.com/rogro82/hass-variables

Apologies @clyra I must have misunderstood the anchor documentation, I’ll take another look.

The Hass-variables also sounds interesting - good spotting.

I do think this kind of thing would be a lot of use - a central repository for setting variables, thanks to you both for your help

Whether you use custom variables, or an input_text, remember that an entity’s state is always a string, so store a JSON object into a state will turn it into a single string, meaning accessing the original elements will be difficult. Also, state strings are limited to 255 characters. If you want to save a JSON object you really need to save it to an attribute of an entity’s state, which is not very easy without using something like a Python Script.

1 Like

Hey Phil, yep good tip.

I made a wee function in Bash & Python that takes a few simple inputs and sends an object like {state: on, attributes {etc…}} waits a few seconds then sends {state: off, attributes {etc…}}. It might not be neccessary but it does mean I can set the retain to true, but automatons only kick off when it first applies (but I can always see the most recent sensor contents ).

It does surprise me a bit that we can’t make a static object available for a standardised references in your scripts. It would mean that if I have to change any aspect of my setup, then I only need to change that in one place.