I use a script to (successfully) get LLDP data from OpenWrt devices via Ubus, and want to make that data available to templates further down the line, so I’m attempting to create some templated entities with the LLDP data as attribute.
Passing the data from the script I get an error (full below), that would seem to indicate, that I should pass a dictionary
, so I changed the relevant line from
lldp: {{ (lldp.stdout | from_json).lldp.interface ))
to:
alias: Get LLDP from OpenWrt
sequence:
- data:
subsystem: file
method: exec
parameters:
command: /usr/sbin/lldpctl
params:
- "-f"
- json
response_variable: lldp
target:
entity_id: "{{ target }}"
action: openwrt.ubus
- variables:
lldp: >
{{ dict({}, interface=(lldp.stdout | from_json).lldp.interface ) }}
- stop: End
response_variable: lldp
mode: parallel
fields:
target:
selector:
entity: {}
name: Target OpenWrt Entity
required: true
, but I still get the error
Get LLDP from OpenWrt: Error executing script. Error for call_service at pos 1: Failed to process the returned action response data, expected a dictionary, but got <class 'NoneType'>
and then again, the same error with a different prefix:
Trigger Update Coordinator: Error executing script. Error for call_service at pos 3:
When I run this script separately in developer tools, the output sure does look like a valid dictionary with my wanted LLDP data.
The whole context this is happening in:
template:
- trigger:
platform: time_pattern
minutes: "/1"
action:
- service: script.get_lldp_from_openwrt
data:
target: binary_sensor.openwrt_wrsw
response_variable: lldp_wrsw
# Several more service calls like the above with different hostnames (wrsw = workroom switch)
binary_sensor:
- name: OpenWrt WRSW UBUS LLDP Topo
unique_id: openwrt_wrsw_ubus_lldp
state: "{{ states('binary_sensor.openwrt_wrsw') }}"
attributes:
data: "{{ lldp_wrsw }}"
# Several more binary sensors for different Openwrt Devices
I had built a prototype of something with similar value-passing a year ago, and got it working back then in my throw-away scratchpad installation, but can’t figure out anymore how I did it back then.
FINAL EDIT: Solved with help from the others:
The above works and is correct, but fails for all the sensors, if the service call for even one of the LLDP-Hosts fails. The logs don’t make that clear, but the Trace in the script menu adds more vital detail to the error in that case. I’m adding failure-resiliance mechanisms now. Thanks again, learnt something from everybody below!