#JSON data in script

1 messages · Page 1 of 1 (latest)

fading stump
#

I am trying to abstract away sending a notification into a script. I have created some fields like message and title which work fine. But what I want to do is create a field in the script called data, which can capture JSON, and feed that into the notification action in the data attribute. This however does not work...

I've tried this for a while and I can't see how to do this. If I'm unable to do this i'll end up having to recreate every attribute as a field, which I don't want to do.

Any suggestions?

mint willow
#

I would expect that you'd be able to send JSON in as a string and use a template with from_json to turn it into an object

#

What did you try?

#

Or you can just pass an object directly into the script from somewhere else

unreal jungle
mint willow
#

Could just be malformed JSON. It's very picky

fading stump
#

Ill show you what I mean.

#
  - if:
      - condition: template
        value_template: "{{target == 'All' or 'Phone 1'}}"
    then:
      - action: notify.mobile_app_pixel_7_pro
        metadata: {}
        data:
          message: "{{ message }}"
          title: "{{ title }}"
          data:
            NEED THE ADDITIONAL SETTINGS HERE: false
fields:
  message:
    selector:
      text: null
    name: Message
    description: Message in the notification
    required: true
  title:
    selector:
      text: null
    name: Title
    description: The title of the message
    required: false
  additional_settings:
    selector:
      text:
        multiline: true
    name: Additional Settings
    required: false
  target:
    selector:
      select:
        options:
          - All
          - Phone 1
          - Phone 2
          - Tablet
    default: All
    required: true
alias: Send Notifications
description: Send a notification
icon: mdi:message-badge
#

So im literlaly tying to accept the data attribute as a field in the script. The fild being called additional_data

#

I have tried multiple methods, but either the template isnt creating correct YAML or its just being ignored.

#

Any ideas @unreal jungle ?

mint willow
#

What are the "multiple methods" that you've tried, and are you expecting the user to type/paste the JSON into that field? Those field definitions are just description for the user, they have no impact on the functionality of the script itself

#

Templates shouldn't be creating any YAML. You can just use the data from the data structure directly, for instance to template that entire data: block

#

it's looking for a dict, so if you pass in a field containing a dict, you can use it to populate that block

#

for instance:

test_notify:
  sequence:
    - action: script.test_send_notification
      data:
        input_dict:
          title: "Test Notification"
          message: "This is a test notification from Home Assistant"

test_send_notification:
  sequence:
    - action: notify.mobile_app_robs_iphone_15
      data: "{{ input_dict }}"
sour zenith
#

as an aside, note that this condition won't do what you want:

  - if:
      - condition: template
        value_template: "{{target == 'All' or 'Phone 1'}}"

that will only be true when target == 'All' because it is OR'ing the result of that comparison with the string 'Phone 1' which is falsey

you want this instead: "{{ target in ['All', 'Phone 1'] }}"

fading stump
#

Let me give it a test @mint willow. I see what you have done. I was approaching it differently. I was accepting title and message as indiviual fields and then the data seperatly, and then trying to reconstruct it.

#

Hence I was trying to create YAML via tmplates.

mint willow
#

You can still use separate fields and then just specify the value of each field using the variable you passed in for each field

#

This started as quite the XY problem

brisk duneBOT
#

The XY problem is asking about your attempted solution rather than your actual problem.

fading stump
#

@mint willow - Ill give it a try, but Im sure I tried your soluton already. Let me give it another go.

mint willow
#

It just a standard use of passing variables into a script

fading stump
#

Fair point on the XY problem.

mint willow
fading stump
#

So i'm sure my input is wrong?

#

this is the script

    - if:
        - condition: template
          value_template: "{{target == 'All'}}"
      then:
        - action: notify.mobile_app_pixel_7_pro
          metadata: {}
          data:
            message: "{{ message }}"
            title: "{{ title }}"
            data: "{{ additional_data }}"```
#

I'm running it from the dev actions

#

OKay so this works

#

I suppose there no way to do it in the UI though is there

mint willow
#

Indeed. You didn't provide it in your first example

#

you can probably only specify individual fields there

fading stump
#

That's where I was going wrong. I was trying to do it via the UI, which was sending a string into the script.

#

Ive updated the field selector to "object" and now its rendering correctly! Horay

#

Thanks @mint willow for the guidence!

fallen shale
#

You should have used an object selector instead of a text selector