Sensor yaml code stopped working

I have a sensor that I have been using for a few years.
It has now stopped working, in that it will not create the sensor.
What may have changed in HA recently or is there some other reason it might not work.
The sensor is located at /entities/sensors/

#https://home-assistant.io/components/sensor/
####################################################
#     HOLIDAYS / BIRTHDAYS 2023Mar28               #
####################################################
# sensor:
  - platform: command_line
    name: Holiday
    command: 'cat /config/data/days.json'
    value_template: >
      {% set today = now().month  ~ '/' ~ now().day  %}
      {% set holiday =  value_json.MAJOR_US.static[ today ] %}
      {% if holiday | trim == "" %}
        {% set today = now().month  ~ '/' ~ now().day ~ '/' ~ now().year %}
        {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
      {% endif %}
      {{ holiday }}
    scan_interval: 14400
####################################################
#  HOLIDAYS / BIRTHDAYS TOMORROW  2023Mar28        #
####################################################
  - platform: command_line
    name: Holidaytomorrow
    command: 'cat /config/data/days.json'
    value_template: >
      {% set today = now().month  ~ '/' ~ (now().day+1)  %}
      {% set holiday =  value_json.MAJOR_US.static[ today ] %}
      {% if holiday | trim == "" %}
        {% set today = now().month  ~ '/' ~ (now().day+1) ~ '/' ~ now().year %}
        {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
      {% endif %}
      {{ holiday }}
    scan_interval: 14400
####################################################
#  HOLIDAYS / BIRTHDAYS IN A WEEK 2023Mar28        #
####################################################
  - platform: command_line
    name: Holidayinaweek
    command: 'cat /config/data/days.json'
    value_template: >
      {% set today = now().month  ~ '/' ~ (now().day+7)  %}
      {% set holiday =  value_json.MAJOR_US.static[ today ] %}
      {% if holiday | trim == "" %}
        {% set today = now().month  ~ '/' ~ (now().day+7) ~ '/' ~ now().year %}
        {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
      {% endif %}
      {{ holiday }}
    scan_interval: 14400  
####################################################
#            END OF CONFIGURATION FILE             #
####################################################      

Thank you for the help

command line sensors are no longer part of the sensor platform key.

they are now configured under their own top level key.

So is this what it is supposed to look like. If it is it does not work for me.

####################################################
#  HOLIDAYS / BIRTHDAYS TOMORROW  2023Mar28        #
####################################################
command_line:
  - sensor:
      name: Holidaytomorrow
      command: "cat /opt/homeassistant/config/data/days.json"
      value_template: >
        {% set today = now().month  ~ "/" ~ (now().day+1)  %}
        {% set holiday =  value_json.MAJOR_US.static[ today ] %}
        {% if holiday | trim == "" %}
          {% set today = now().month  ~ "/" ~ (now().day+1) ~ "/" ~ now().year %}
          {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
        {% endif %}
        {{ holiday }}
      scan_interval: 14400

That looks right to me.

What do you mean by “it doesn’t work”?

Where did you put that code? is it directly in your configuration.yaml file or did you !include it in your “sensor:” include directive?

If you used an !include then it can’t be under “sensor.yaml” it has to be included by the command_line key. Like this:

command_line: !include command_line.yaml

then in the new “command_line.yaml” file you would put this (note - no “command_line:” entry at the top):

  - sensor:
      name: Holidaytomorrow
      command: "cat /opt/homeassistant/config/data/days.json"
      value_template: >
        {% set today = now().month  ~ "/" ~ (now().day+1)  %}
        {% set holiday =  value_json.MAJOR_US.static[ today ] %}
        {% if holiday | trim == "" %}
          {% set today = now().month  ~ "/" ~ (now().day+1) ~ "/" ~ now().year %}
          {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
        {% endif %}
        {{ holiday }}
      scan_interval: 14400

Thank you that solved the problem.

1 Like

Usually the solution tag goes to the post that actually helped you solve the problem. Not to your post saying it was solved.

If another user comes around later with the same question it helps them to find the post that solved it rather than the one saying it was solved.

2 Likes