JSON temp into REST sensor - I am failing badly

So here I am trying to get a better understanding of JSON REST sensors.

I have an OPNsense firewall. OPNsense has a API for returning a JSON for different things, like CPU temp.

So I thought I do something like:

rest:
  - resource: "https://x.x.x.1/widgets/api/get.php?load=temperature"
    scan_interval: 60
	sensor:
	  - name: "Core Temp"
        value_template: "{{ value_json[data.temperature[0].temperature] }}"
		device_class: temperature
        unit_of_measurement: "°C"

Well nothing - don’t know what I might even be doing wrong here - no notification and no entity to retrieve the information from.

If I query that IP from my personal PC I get this JSON in return:
{"system":"OPNsense","plugins":["system","temperature"],"data":{"temperature":[{"device":"dev.pchtherm.0.temperature","device_seq":"0","temperature":"41.5","type":"core","type_translated":"Core"},{"device":"hw.acpi.thermal.tz0.temperature","device_seq":"0","temperature":"27.9","type":"zone","type_translated":"Zone"},{"device":"hw.acpi.thermal.tz1.temperature","device_seq":"1","temperature":"29.9","type":"zone","type_translated":"Zone"}]}}

Wonder if I can get that in better format, let’s try this:

system	"OPNsense"
plugins	
0	"system"
1	"temperature"
data	
temperature	
0	
device	"dev.pchtherm.0.temperature"
device_seq	"0"
temperature	"41.5"
type	"core"
type_translated	"Core"
1	
device	"hw.acpi.thermal.tz0.temperature"
device_seq	"0"
temperature	"27.9"
type	"zone"
type_translated	"Zone"
2	
device	"hw.acpi.thermal.tz1.temperature"
device_seq	"1"
temperature	"29.9"
type	"zone"
type_translated	"Zone"

Anyway, I’m a bit out of ideas how to retreive this JSON from OPNSense into my HomeAssistant. I guess this could be a question of userid/pwd of course, but as I wrote, I get no notification about nothing. So…

Sido information: I have the HACS OPNSense integration loaded, and that integration does load all that information without problems. Now the OPNsense integration uses API keys for access, so I was kind of hoping I could ride onto that “logon” so to speak, maybe I am wrong here?

Your value_template is wrong. Try this in Developer Tools / Template:

{% set value_json = {"system":"OPNsense","plugins":["system","temperature"],"data":{"temperature":[{"device":"dev.pchtherm.0.temperature","device_seq":"0","temperature":"41.5","type":"core","type_translated":"Core"},{"device":"hw.acpi.thermal.tz0.temperature","device_seq":"0","temperature":"27.9","type":"zone","type_translated":"Zone"},{"device":"hw.acpi.thermal.tz1.temperature","device_seq":"1","temperature":"29.9","type":"zone","type_translated":"Zone"}]}} %}

{{ value_json['data']['temperature'][0]['temperature'] }}

Ahhhh!

Very Large Thanks! Both for the help with my problem, and also for the tip how to use Development tools to figure out how to retrieve JSON data from a JSON result!

1 Like

Nope - still don’t get it to work. I can get separate parts to work when emulating this. But the definition in configuration.yaml simply does not produce any entity. Weird.


rest:
  - resource: "https://x.x.1.1/widgets/api/get.php?load=temperature"
    username: xxx (OPNSense API Key)
    password: yyy (OPNSense API Secret)
    scan_interval: 60
    sensor:
      - name: "Core Temp"
        value_template: "{{ value_json['data']['temperature'][0]['temperature'] }}"
        device_class: temperature
        unit_of_measurement: "°C"

I have to say this is interesting, how I continue to stumble on the smallest of challenges :slight_smile:

If this is the first time you’ve included the rest: heading (integration), you will need to restart HA.

Also check there isn’t already another rest: heading.

Next debugging step is to remove device_class and unit_of_measurement, and change the value_template to "{{ value[:250] }}" which will show you the first bit of the response you’re actually getting back.

1 Like

Well, first time adding rest: - so I double checked that I did not write it twice. And No.

Here is two details: I actually “all of a sudden” (after changing an error in another integration… never mind that one) got the entity. But empty. So it at least created it - but seems not to populate it.

So I did the change you suggested above, and well that is a very long string as response that starts with "<!doctype html> <html … ". Since I have no idea what :250 does, I simply do not know how to interpret this answeer? But at least something is happening :slight_smile:

So now I need to figure out why/how to actually get a rest https resource to work - from what I gather I simply do not get the connection to work. And I have tried to find something in the core log - but it is a bit quite in that log (except for the OPNsense plugin that keeps repeating all sort of errors - however that integration (OPNsense) does not include temp - that is why I am trying this on the side… Until someone might have the time to figure out how to get temp to work inside the plugin.

Anyway, I will keep trying!

Very large thanks again!

Well if I learn to read someday this will be much easier:

change the value_template to "{{ value[:250] }}" which will show you the first bit of the response you’re actually getting back.

However that is a bit away from the expected JSON I say - I am getting a bit tired I think so I will let this rest until tomorrow and see what the H*ck is not happening…

1 Like

Yea so the result I get back is what one might call key and secret is missing kind of thing - so no I am not logged in. I need to figure that part out.

Right…

It turns out that the API call I am using does not support outside of browser execution or something:

A rather well know limitation it seems, and I did not know that.
So basically there is currently no way to get that kind of information from OPNsense - oh well I learned a bunch of stuff, I hope I can remember it all :wink:

Thanks for helping out!

Bizarre design choice. Try accessing the API URL in a browser with F12 DevTools turned on, and see what network resources are pulled in.

It may be as simple as setting some headers (User-Agent, Accept) in your request.

Well I tried your suggestion, and the only thing - and I do not know what I am looking at - that I can tell is that a cookie seems to be needed…

Now that would explain why this works on my workstation. If I first log into OPNsense, it works - if I am NOT logged in, it fails. And the only thing that can separate this, is a valid cookie…

You might be able to get the cookie via a command line sensor then use that in the rest sensor. I do something related with my printer:

Ah! Nice idea!

I’ll give it a go tomorrow!

(wonder if the login window can handle this - this will be very interesting…!)

Hello! I figured Id chime in another alternative, since I actually came across this yesterday while trying to solve the same thing by chance.
I could not get this to function for the life of me, perhaps its just my lack of skill.
In any case, I ended up using the Telegraf plugin in OPNSense, and output the data into MQTT, which is then added as a sensor in home assistant. It has been working well for me so far, and has consistently provided data.
I would have preferred to use rest, of course, but sometimes you have to pick your battles. Hope this helps!

Thanks!

I have kind of given up on this - never got it working. I might try your solution…

Sounds good! Let me know how it goes! I can share the configuration that needs to be created for telegraf to read the cpu temps. Telegraf on opnsense will autogenerate the default config file, so you need to create a telegraf.conf.d/*.conf file so it can import those parameters that arent available in gui. Its very simple.