Some help with command line and JSON

Hi All,

I have this json file:

calendar.json:

{
    "json_calendars": {
        "events": {
            "2/14":"Valentine's Day",
            "4/1":"April Fool's Day",
            "5/4":"Dodenherdenking",
            "5/5":"Bevrijdingsdag",
            "7/4":"Independence Day",
            "9/2":"Special Day P&K",
            "11/23":"Black Friday",
            "11/24":"Black Friday",
            "11/25":"Black Friday",
            "11/26":"Black Friday",
            "11/27":"Black Friday",
            "12/31":"New Year's Eve"
        },
        "birthday": {
            "2/14":"Not show on forum",
            "12/31":"Not show on forum"
        },
        "holidays": {
            "2/14":"Not show on forum",
            "12/31":"Not show on forum"
        }
    }
}

and those 3 sensors

- sensor:
    name: "Birthday test"
    unique_id: "93a52226-7ba7-494b-bfdf-817902920d70"
    command: "cat /config/package/json/calendar.json"
    scan_interval: 21600
    value_template: >-
      {% set today = now().month ~ '/' ~ now().day  %}
      {% set birthday = value_json.json_calendars.birthday[ today ] %}
        {% if birthday | trim == "" %}
          {% set birthday = "Nothing" %}
          {% endif %}
      {{ birthday }}

- sensor:
    name: "Holiday"
    unique_id: "490f80c3-7bde-47c0-a6fa-c2ed6d742926"
    command: "cat /config/package/json/calendar.json"
    scan_interval: 21600
    value_template: >-
      {% set today = now().month ~ '/' ~ now().day  %}
      {% set holiday = value_json.json_calendars.holiday[ today ] %}
        {% if holiday | trim == "" %}
          {% set holiday = "Nothing" %}
          {% endif %}
      {{ holiday }}

- sensor:
    name: "Events"
    unique_id: "716b7f41-ac6d-4df2-b01a-31b2ccf9e138"
    command: "cat /config/package/json/calendar.json"
    scan_interval: 21600
    value_template: >-
      {% set today = now().month ~ '/' ~ now().day  %}
      {% set events = value_json.json_calendars.events[ today ] %}
        {% if events | trim == "" %}
          {% set events = "Nothing" %}
          {% endif %}
      {{ events }}

They working. I can see the input from json file in the sensor when using them in UI.

Only when I restart HA I see those errors in the log:

2023-07-11 07:58:26.766 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute '7/11' when rendering '{% set today = now().month ~ '/' ~ now().day  %} {% set birthday = value_json.json_calendars.birthday[ today ] %}
  {% if birthday | trim == "" %}
    {% set birthday = "Nothing" %}
    {% endif %}
{{ birthday }}'
2023-07-11 07:58:26.804 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute '7/11' when rendering '{% set today = now().month ~ '/' ~ now().day  %} {% set holiday = value_json.json_calendars.holiday[ today ] %}
  {% if holiday | trim == "" %}
    {% set holiday = "Nothing" %}
    {% endif %}
{{ holiday }}'
2023-07-11 07:58:26.849 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute '7/11' when rendering '{% set today = now().month ~ '/' ~ now().day  %} {% set events = value_json.json_calendars.events[ today ] %}
  {% if events | trim == "" %}
    {% set events = "Nothing" %}
    {% endif %}
{{ events }}'

How can I solve this?

Thanks for helping!

You need to specify a default for when there is no matching date entry. Given your following line checks for an empty string, that’s what the default should be. So, for example:

{% set holiday = value_json.json_calendars.holiday[ today ] | default("") %}

As an aside, I’d use a REST sensor to load the whole json rather than multiple command line sensors, and then have a template sensor for each event. This would require the json being in “/config/www” and having http configured, but then the following would load everything once into the “json_calendars” attribute. No idea if this is a better approach or not though, as both work.

- resource: http://192.168.0.66:8123/local/json/mbtest.json
  scan_interval: 21600
  sensor:
    - name: MB Test Rest 1
      unique_id: mb_test_rest_1
      value_template: "OK"
      json_attributes:
        - json_calendars

Thank you for the information. The REST part I use for other things. This way is needed to control scenes in the house for lightning. When media is on (TV, AppleTV or Kodi) there are multiple light scenes for every holiday event :slight_smile: